【发布时间】:2017-10-23 05:57:12
【问题描述】:
我正在尝试实现 CNN,但遇到了一个小问题。
x = tf.placeholder(tf.float32, [None, 28, 28, 1])
# 0-9 digits recognition => 10 classes.
y = tf.placeholder(tf.float32, [None, 10])
...code for layers...
...etc....
# Output has a shape of [batch_size, 10]
logits = tf.layers.dense(inputs=dropout, units=10)
# Softmax layer for deriving probabilities.
pred = tf.nn.softmax(logits, name="softmax_tensor")
# Convert labels to a one-hot encoding.
onehot_labels = tf.one_hot(indices=tf.cast(y, tf.int32), depth=10)
loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits)
可见,losses 函数将无法正常运行,因为logits 和onehot_labels 的形状不同。 logits 是 shape=(2,) 而 onehot_labels 是 shape=(3,) 这是因为它取决于 y 占位符 [batch_size, 10]。
我不确定如何解决这个问题。我需要改变其中任何一个变量的形状,但我不确定是哪一个。 CNN 是否要求标签 y 将 batch_size 作为参数?我哪里错了?
一些额外的信息,我打算在一个会话中运行 CNN。
# Assign the contents of `batch_xs` to variable `x`.
_, c = sess.run([train_op, loss], feed_dict={x:sess.run(batch_xs), y:batch_ys})
【问题讨论】:
标签: python-3.x tensorflow conv-neural-network