3

我正在尝试编写一个可以识别手写数字的神经网络。我正在使用 MNIST 数据集和张量流库。目前,我只是在尝试训练网络,但每当我运行它时它都会抛出一个巨大的错误。我是初学者,所以如果代码看起来不好,我很抱歉。

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)

每次我运行这段代码时,它都会给我这个错误:

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.
4

1 回答 1

3

我想你的意思是

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"]

注意FinalH1代替and 与andH1相同。H2H3

于 2018-01-08T12:14:24.260 回答