gpt4 book ai didi

python - ValueError : Dimensions must be equal, 但对于 'Mul' 是 784 和 500 (op : 'Mul' ) with input shapes: [? ,784), [784,500]

转载 作者:太空狗 更新时间:2023-10-29 22:18:05 26 4
gpt4 key购买 nike

我正在尝试学习 Tensor Flow,因此我遵循了 https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/ 的神经网络教程

我正在尝试运行代码,但即使我的尺寸看起来正确,也会不断出现相同的尺寸错误。

我是 Tensor Flow 的新手,所以我不确定我做错了什么。

我会发布代码和错误。

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

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)



n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

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

def neural_network_model(data):
#(input_data * weights) + biases


hidden_1_layer = {
'weights' : tf.Variable(tf.random_normal([784,n_nodes_hl1])),
'biases' : tf.Variable(tf.random_normal([n_nodes_hl1]))
}


hidden_2_layer = {
'weights' : tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
'biases' : tf.Variable(tf.random_normal([n_nodes_hl2]))
}
hidden_3_layer = {
'weights' : tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
'biases' : tf.Variable(tf.random_normal([n_nodes_hl3]))
}
output_layer = {
'weights' : tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),
'biases' : tf.Variable(tf.random_normal([n_classes]))
}



net_Layer1 = tf.add(tf.multiply(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
output_layer1 = tf.nn.relu(net_Layer1)

net_Layer2 = tf.add(tf.multiply(output_layer1, hidden_2_layer['weights']), hidden_2_layer['biases'])
output_layer2 = tf.nn.relu(net_Layer2)

net_Layer3 = tf.add(tf.multiply(output_layer2, hidden_3_layer['weights']), hidden_3_layer['biases'])
output_layer3 = tf.nn.relu(net_Layer3)


output = tf.add(tf.multiply(output_layer3, output_layer['weights']), output_layer['biases'])

return output


def train_neural_network(input):
prediction = neural_network_model(input)
error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = prediction,labels = y))

optimizer = tf.train.AdamOptimizer().minimize(error)

epochs = 10

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

for epoch in epochs:
epoch_loss = 0
for _ in range(int(mnist.train.num_examples/batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, e = sess.run([optimizer, error], feed_dict={x:epoch_x, y:epoch_y})
epoch_loss += e

print('Epoch', epoch, 'completed out of', epochs, 'loss :', epoch_loss)


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


train_neural_network(x)

我得到的错误如下-

  net_Layer1 = tf.add(tf.multiply(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 357, in multiply
return gen_math_ops._mul(x, y, name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1625, in _mul
result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2397, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1757, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1707, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 784 and 500 for 'Mul' (op: 'Mul') with input shapes: [?,784], [784,500].

最佳答案

错误出现是因为你使用了“multiply”

在你使用的所有行中

tf.add(tf.multiply(.....))

使用:

tf.add(tf.matmul(......))

因为这是矩阵乘法。

关于python - ValueError : Dimensions must be equal, 但对于 'Mul' 是 784 和 500 (op : 'Mul' ) with input shapes: [? ,784), [784,500],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42407628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com