我正在尝试学习 tensorflow,但出现以下错误:logits 和标签必须是可广播的:logits_size=[32,1] labels_size=[16,1]
当我将其作为输入时,代码运行良好:
self.input = np.ones((500, 784))
self.y = np.ones((500, 1))
Run Code Online (Sandbox Code Playgroud)
但是,当我添加额外的维度时,会抛出错误:
self.input = np.ones((500, 2, 784))
self.y = np.ones((500, 1))
Run Code Online (Sandbox Code Playgroud)
构建图的代码
self.x = tf.placeholder(tf.float32, shape=[None] + self.config.state_size)
self.y = tf.placeholder(tf.float32, shape=[None, 1])
# network architecture
d1 = tf.layers.dense(self.x, 512, activation=tf.nn.relu, name="dense1")
d2 = tf.layers.dense(d1, 1, name="dense2")
with tf.name_scope("loss"):
self.cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=d2))
self.train_step = tf.train.AdamOptimizer(self.config.learning_rate).minimize(self.cross_entropy,
global_step=self.global_step_tensor)
correct_prediction = tf.equal(tf.argmax(d2, 1), tf.argmax(self.y, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么会发生这种情况以及如何解决这个问题吗?
Dav*_*rks 11
logits是通常赋予网络输出的名称,这些是您的预测。一个大小[32, 10]告诉我你有一个 32 的批量大小和 10 个输出,就像你似乎正在使用的 mnist 一样。
您的标签是 sized [16, 10],也就是说,您提供 16 个标签/大小为 10 的向量。您提供的标签数量与网络的输出冲突,它们应该相同。
我不太清楚你对输入中的额外维度做了什么,但我想你一定不小心以某种方式将样本加倍。也许[500, 2, 784]形状正在[1000, 784]沿途的某个地方被自动重塑,然后与 500 个标签不匹配。此外,您self.y应该形[500, 10]不是[500, 1],您的标签需要在一个热编码格式。例如[1, 10],数字 3的单个形状标签将是[[0,0,0,1,0,0,0,0,0,0,0]],而不是数字表示,例如[3],您似乎在此处的健全性测试中设置了它。