tensorflow 错误: Failed to convert object of type <class 'dict' > to Tensor

标签 tensorflow machine-learning neural-network deep-learning feed-forward

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

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.

最佳答案

我想你的意思是

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而不是H1H2H3也是如此。

关于 tensorflow 错误: Failed to convert object of type <class 'dict' > to Tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48149954/

相关文章:

tensorflow - 将 keras 与 tensorflow 服务一起使用

python - 使用、准备用于回归的词袋数据

android - 如何使用机器学习和神经网络快速构建图像识别应用程序原型(prototype)?

c++ - FANN:使用从多个文件读取的数据训练 ANN 时发生内存泄漏

tensorflow - 当 mAP 不稳定时,我应该什么时候停止对象检测模型训练?

python - Tensorflow Mnist 出错

python - Tensorflow 磁贴抛出 TypeError : List of Tensors when single Tensor expected

machine-learning - 如何提高机器学习的分类精度

python - 类型错误 : object of type 'Tensor' has no len() when using a custom metric in Tensorflow

image - 创建神经网络结构的图像