我正在尝试转换 tutorial从 Keras 到 TF。 我收到以下错误:
Traceback (most recent call last):
File "/Users/spicyramen/Documents/Development/google/python/machine_learning/deep_learning/exercise1_tf.py", line 64, in <module>
sess.run(train_step, feed_dict=train_data)
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 975, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (768,) for Tensor u'Placeholder_1:0', which has shape '(?, 1)'
这似乎与我传递目标标签的方式以及占位符值的声明方式有关。 当我返回标签时,我有这个:
>>> dataset[:, 8].shape
(768,)
>>> dataset[:, 0:8].shape
(768, 8)
代码
import tensorflow as tf
import numpy as np
print("Tensorflow version: " + tf.__version__)
tf.set_random_seed(0)
FILENAME = 'pima-indians-diabetes.csv'
_LEARNING_RATE = 0.003
_NUM_FEATURES = 8
_NUM_LABELS = 1
_NUM_EPOCHS = 150
_BATCH_SIZE = 10
def import_data(filename):
if filename:
dataset = np.loadtxt(filename, delimiter=",")
return dataset[:, 0:8], dataset[:, 8]
# create placeholder. Dataset contains _NUM_FEATURES features:
X = tf.placeholder(tf.float32, [None, _NUM_FEATURES])
Y_ = tf.placeholder(tf.float32,[None, _NUM_LABELS]) # Placeholder for correct answers
# weights and biases
W = tf.Variable(tf.random_normal([_NUM_FEATURES, _NUM_LABELS],
mean=0,
stddev=0.1,
name='weights'))
b = tf.Variable(tf.random_normal([1, _NUM_LABELS],
mean=0,
stddev=0.1,
name='bias'))
# activation function
Y = tf.nn.relu(tf.matmul(X, W) + b, name='activation')
# cost function i.e. sigmoid_cross_entropy_with_logits
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=Y_, logits=Y, name='loss_function')
optimizer = tf.train.AdamOptimizer(_LEARNING_RATE) # Formal derivation
train_step = optimizer.minimize(cross_entropy)
# cost function i.e. RMSE
# cross_entropy = tf.nn.l2_loss(Y - Y_, name="squared_error_cost")
# optimizer = tf.train.GradientDescentOptimizer(_LEARNING_RATE)
# train_step = optimizer.minimize(cross_entropy)
is_correct = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
# init
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(_NUM_EPOCHS):
# data
batch_X, batch_Y = import_data(FILENAME)
# train
train_data = {X: batch_X, Y_: batch_Y}
sess.run(train_step, feed_dict=train_data)
a, c = sess.run([accuracy, cross_entropy], feed_dict=train_data)
print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c))
最佳答案
这是你的问题:
>>> dataset[:, 8].shape
(768,)
TensorFlow 需要一个形状为 (768,1)
的数组,而不是 (768,)
作为错误引用:
Cannot feed value of shape (768,) for Tensor u'Placeholder_1:0', which has shape '(?, 1)'
这两种形状之间的差异有点小,Numpy 通常会在许多情况下为您广播这些差异,但 TF 不会。看看这两种形状之间的区别in this question with a great answer .
幸运的是,对于您的情况,解决方案非常简单。您可以使用np.expand_dims()
将您的 (768,)
向量转换为 (768,1)
向量,如下所示:
>>> np.array([5,5,5]).shape
(3,)
>>> np.expand_dims(np.array([5,5,5]), axis=1).shape
(3, 1)
在您的 import_data
函数中,只需将返回行更改为
return dataset[:, 0:8], np.expand_dims(dataset[:, 8], axis=1)
编辑:我喜欢上面的方法,因为 np.expand_dims
更明确一点,但还有另一种同样简单的方法,其他人可能认为它更清晰——仅取决于您的内容已经习惯了为了完整起见,我想将其包括在内。 (N,)
和 (N,1)
数组之间的区别在于,第一个数组保存 0 维数组中的值 np.array([5, 5, 5])
,而第二个则在一维数组 np.array([[5],[5],[5]])
中保存值。您可以通过在 0 维数组周围添加括号将其转换为 1 维数组;但它是一行而不是列,所以需要转置。所以这是两种建议的方法; B
是新建议,C
是上面的建议:
>>> A = np.array([5,5,5])
>>> B = np.array([A]).T
>>> C = np.expand_dims(A, axis=1)
>>> A; A.shape
array([5, 5, 5])
(3,)
>>> B; B.shape
array([[5],
[5],
[5]])
(3, 1)
>>> C; C.shape
array([[5],
[5],
[5]])
(3, 1)
Edit2:TensorFlow 本身也有一个 tf.expand_dims()
功能。
关于python - tensorflow 占位符声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44618887/