3

I am trying to code a neural network which can recognize handwritten digits. I am using the MNIST dataset and the tensor flow library. For now, I am only trying to train the network but it throws a huge error whenever I run it. I am a beginner, so I am very sorry if the code looks bad.

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/tmp/data", one_hot = True)
numNodesH1 = 600
numNodesH2 = 500
numNodesH3 = 500
numNodesOut = 10
sizeOfBatch = 150

y = tf.placeholder("float")
x = tf.placeholder("float", [None, 784])

def neuralNetwork(value):
    H1 = {'weights': tf.Variable(tf.random_normal([784, numNodesH1])),
         "biases": tf.Variable(tf.random_normal([numNodesH1]))}

    H2 = {'weights': tf.Variable(tf.random_normal([numNodesH1, 
         numNodesH2])),
         "biases": tf.Variable(tf.random_normal([numNodesH2]))}

    H3 = {"weights": tf.Variable(tf.random_normal([numNodesH2, 
    numNodesH3])),
    "biases": tf.Variable(tf.random_normal([numNodesH3]))}

    output = {"weights": tf.Variable(tf.random_normal([numNodesH3, 
    numNodesOut])),
    "biases": tf.Variable(tf.random_normal([numNodesOut]))}

    FinalH1 = tf.add(tf.matmul(value, H1["weights"]), H1["biases"])
    FinalH1 = tf.nn.relu(FinalH1)
    FinalH2 = tf.add(tf.matmul(H1, H2["weights"]), H2["biases"])
    FinalH2 = tf.nn.relu(FinalH2)
    FinalH3 = tf.add(tf.matmul(H2, H3["weights"]), H3["biases"])
    FinalH3 = tf.nn.relu(FinalH3)
    FinalOut = tf.matmul(H3, output["weights"]) + output["biases"]

    return FinalOut

def train(inputdata):
    prediction = neuralNetwork(inputdata)
    cost=tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
    optimizingTool = tf.train.AdamOptimizer().minimize(cost)
    epochsNum = 10

    with tf.Session as sess:
        sess.run(tf.global_variables_initializer())

        for i in range(epochsNum):
            lostEpochs = 0
            for o in range(int(mnist.train.num_examples / sizeOfBatch)):
                ex, ey = mnist.train.next_batch(sizeOfBatch)
                _, c = sess.run([optimizer, cost], feed_dict = {x: ex, y: 
                                ey})
                lostEpochs = lostEpochs + c

            print("Epochs completed = ", i, " / ", epochsNum, " epoch loss = 
            ", lostEpochs)

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
    neuralAccuracy = tf.reduce_mean(tf.cast(correct, "float"))
    print(neuralAccuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

train(x)

Every time I run this code, it gives me this error:

Traceback (most recent call last):
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\tensor_util.py", line 468, in 
make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\tensor_util.py", line 468, in 
<listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\util\compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got {'weights': <tf.Variable 
'Variable:0' shape=(784, 600) dtype=float32_ref>, 'biases': <tf.Variable 
'Variable_1:0' shape=(600,) dtype=float32_ref>}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Msi-
\AppData\Local\Programs\Python\Python36\neuralnetworktest.py", line 45, in 
<module>
train(x)
File "C:\Users\Msi-
\AppData\Local\Programs\Python\Python36\neuralnetworktest.py", line 29, in 
train
prediction = neuralNetwork(inputdata)
File "C:\Users\Msi-
\AppData\Local\Programs\Python\Python36\neuralnetworktest.py", line 22, in 
neuralNetwork
FinalH2 = tf.add(tf.matmul(H1, H2["weights"]), H2["biases"])
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\ops\math_ops.py", line 1844, in matmul
a = ops.convert_to_tensor(a, name="a")
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\ops.py", line 836, in convert_to_tensor
as_ref=False)
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\ops.py", line 926, in 
internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\constant_op.py", line 229, in 
_constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\tensor_util.py", line 472, in 
make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'dict'> to Tensor. 
Contents: 
{'weights': <tf.Variable 'Variable:0' shape=(784, 600) dtype=float32_ref>, 
'biases': <tf.Variable 'Variable_1:0' shape=(600,) dtype=float32_ref>}. 
Consider 
casting elements to a supported type.
1
  • As a side note. If you are using the standard NMIST dataset you have 60000 images for training. Now you have a huge network, and you are dealing with roughly with one million (rounded) parameters. I would check overfitting... Why such a big network? With just the softmax function as output you get already over 92%... Just saying... Commented Jan 9, 2018 at 14:02

1 Answer 1

3

I think you meant

FinalH1 = tf.add(tf.matmul(value, H1["weights"]), H1["biases"])
FinalH1 = tf.nn.relu(FinalH1)
FinalH2 = tf.add(tf.matmul(FinalH1, H2["weights"]), H2["biases"])
FinalH2 = tf.nn.relu(FinalH2)
FinalH3 = tf.add(tf.matmul(FinalH2, H3["weights"]), H3["biases"])
FinalH3 = tf.nn.relu(FinalH3)
FinalOut = tf.matmul(FinalH3, output["weights"]) + output["biases"]

Note FinalH1 instead of H1 and that same for H2 and H3.

Sign up to request clarification or add additional context in comments.

2 Comments

Now it says: Traceback (most recent call last): File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\neuralnetworktest.py", line 44, in <module> train(x) File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\neuralnetworktest.py", line 28, in train prediction = neuralNetwork(inputdata) File "C:\Users\Msi-\AppData\Local\Programs\Python\Python36\neuralnetworktest.py", line 20, in neuralNetwork FinalH1 = tf.nn.relu(FinalH1) UnboundLocalError: local variable 'FinalH1' referenced before assignment
You've removed the first line: FinalH1 = tf.add(tf.matmul(value, H1["weights"]), H1["biases"]). It is correct, so I didn't include it into the snippet. Return it back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.