- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 tensorflow 的新手,正在学习 senddex 的教程。我不断收到错误 -
ValueError: Dimensions must be equal, but are 784 and 500 for
'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [500,500].
我认为导致问题的代码段是 -
l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']),
hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']),
hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(data, hidden_3_layer['weights']),
hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.add(tf.matmul(l3, output_layer['weights']),
output_layer['biases'])
return output
虽然我是菜鸟而且可能是错的。我的整个代码是 -
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):
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]))}
l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']),
hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']),
hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(data, hidden_3_layer['weights']),
hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.add(tf.matmul(l3, output_layer['weights']),
output_layer['biases'])
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits
(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples / batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x,
y: epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out of', hm_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)
请帮忙。顺便说一句,我在 Mac 上的虚拟环境中运行 Python 3.6.1 和 Tensorflow 1.2。我正在使用 IDE Pycharm CE。如果任何信息有用。
最佳答案
问题是您引用的是 data
而不是 l1
。而不是
l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']),
hidden_2_layer['biases'])
你的代码应该是这样的
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']),
hidden_2_layer['biases'])
l3
也是如此。而不是
l3 = tf.add(tf.matmul(data, hidden_3_layer['weights']),
hidden_3_layer['biases'])
你应该有
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']),
hidden_3_layer['biases'])
下面的代码对我来说没有错误:
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 print_shape(obj):
print(obj.get_shape().as_list())
def neural_network_model(data):
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]))}
print_shape(data)
l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']),
hidden_1_layer['biases'])
print_shape(l1)
l1 = tf.nn.relu(l1)
print_shape(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']),
hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']),
hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.add(tf.matmul(l3, output_layer['weights']),
output_layer['biases'])
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits
(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples / batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x,
y: epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out of', hm_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)
关于python - ValueError : Dimensions must be equal, 但对于 'MatMul_1' 是 784 和 500 (op : 'MatMul' ) with input shapes: [? ,784), [500,500],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44956460/
在 Sitecore 中,我尝试在线路管理器中创建分段列表。但是,当我选择条件时,我对“等于”、“不区分大小写等于”和“不不区分大小写等于”感到非常困惑? 谁能给我解释一下其中的区别吗? 谢谢! 最佳
基本上,我想知道在这种情况下我是否应该听 ReSharper... 您认为与字符进行比较应该使用 Char.Equals(char),因为它可以避免拆箱,但 Resharper 建议使用 Object
假设 equals() 是可传递的;我知道如果 x 和 y 有平等的双边协议(protocol),那么其中一个,比如 y,不会单独与第三类 z 签订协议(protocol)。 但是如果我们遇到 x.e
我是 Haskell 新手,正在阅读: http://www.seas.upenn.edu/~cis194/spring13/lectures/01-intro.html 它指出“在 Haskell
阅读有关 C# 中的字符串比较的文章,我发现有很多方法可以比较 2 个字符串以查看它们是否相等。 我习惯了来自 C++ 的 == 但我了解到,如果你将一个对象与一个字符串进行比较,那么 == 默认为引
我有一个 Point 类和一个 MinesweeperSquare 类,后者是前者的子类。如果我重写后者的 equals 方法,就像这样: if (!(obj instanceof Minesweep
我想知道,如果我们有 if-else 条件,那么检查什么在计算上更有效:使用等于运算符或不等于给运营商?有什么区别吗? 例如,以下哪一项在计算上是高效的,下面的两种情况都会做同样的事情,但哪一种更好(
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
某些框架(例如 guice )在某些情况下需要创建 注解接口(interface)的实现类 . 好像有一个区别 Annotation.equals(Object) 之间和 Object.equals(
从三个变量开始,都是System.DateTime。 a: 10/2/2009 2:30:00 PM b: 10/2/2009 2:30:00 PM c: 10/2/2009 2:30:00 PM 相
我实现了一个 PagedModel 类来包装 IEnumerable,为我的 MVC 应用程序中的网格提供分页数据。我使用 Resharper 自动生成的 Equality 代码告诉它检查数据、总行数
正如问题所述。理想情况下,答案应该是 false,因为它将使用 Object#equal,这只是引用比较。 String cat = new String("cat"); String
我想知道以下两个选项中哪一个在速度方面最有效。它们之间可能只有很小的区别(或者根本没有区别?)但是由于我每天使用该代码片段 30 次,所以我想知道这里的“最佳实践”是什么 :) 选项 1: if (s
我有一个以年龄和姓名作为实例成员的基类,以及带有奖金的派生类。我在派生类中重写 equals 。我知道 Java 中只有一个基类时 equals 是如何工作的。但我无法理解继承的情况下它是如何工作的。
==之间的区别和 ===是前者仅检查值(1 == "1" 将返回 true),后者是否检查值并另外检查类型(1 === "1" 将返回 false,因为 number 不是字符串)。 比较对象意味着比
这是一个理论问题。我有一个我自己设计的对象,其中包含一堆变量、方法等。我覆盖了 toString 方法,主要用于记录目的,以返回变量的值。在我看来,比较此对象实例的最简单和最直接的方法是比较从 toS
我是 Java 编程的初学者。目前我正在 this 阅读关于继承和 equals 方法的内容。页。到目前为止,我理解解释: Compare the classes of this and otherO
当 IntelliJ 建议我更正以下内容时,我正在编写代码: objectOne.equals(objectTwo); 告诉我方法调用 equals 可能会产生旧的 java.lang.NullPoi
我尝试创建一个允许在 Java 中使用类似元组的结构的元组类。元组中两个元素的一般类型分别是 X 和 Y。我尝试为此类覆盖正确的等号。 事情是,我知道 Object.equals 属于默认值,它仍然根
可以用和比较字符串类似的方式来比较序列。如果两个序列的长度相同,并且对应元素都相等,equal() 算法会返回 true。有 4 个版本的 equal() 算法,其中两个用 == 运算符来比较元素,另
我是一名优秀的程序员,十分优秀!