首页 文章

Tensorflow 'feed_dict':对键值对使用相同的符号'TypeError: Cannot interpret feed_dict key as Tensor'

提问于
浏览
8

我正在玩Tensorflow Build 线性回归的例子,我的代码如下:

import numpy as np
import tensorflow as tf

train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3])

n_samples = train_X.shape[0]
batch_size = 100

total_epochs = 50

X = tf.placeholder('float')
y = tf.placeholder('float')

W = tf.Variable(np.random.randn(), name="weights")
b = tf.Variable(np.random.randn(), name="bias")

y_pred = tf.add(tf.mul(X, W), b)

cost = tf.reduce_sum(tf.pow(y_pred-y, 2))/(2*n_samples) #L2 loss
optimizer = tf.train.AdamOptimizer().minimize(cost) #Gradient 

init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    print("Initia values for W and b: ", W.eval(), b.eval())
    for _ in range(total_epochs):
        sess.run(optimizer, feed_dict={X: x, y: y})
    print("Value for W and b after GD: ", W.eval(), b.eval())

但是,运行上面的代码会给我这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-185d8e05cbcd> in <module>()
     28     for _ in range(total_epochs):
     29         for (x, y) in zip(train_X, train_Y):
---> 30             sess.run(optimizer, feed_dict={X: x, y: y})
     31         print("Value for W and b after GD: ", W.eval(), b.eval())

/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    338     try:
    339       result = self._run(None, fetches, feed_dict, options_ptr,
--> 340                          run_metadata_ptr)
    341       if run_metadata:
    342         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    540           except Exception as e:
    541             raise TypeError('Cannot interpret feed_dict key as Tensor: '
--> 542                             + e.args[0])
    543 
    544           if isinstance(subfeed_val, ops.Tensor):

TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a float64 into a Tensor.

经过深入挖掘后,我意识到错误在这里:

feed_dict={X: x, y: y}

键值对使用的是相同的('y'和'y') . 如果我将其更改为Y:y,并相应地修改其余部分:

Y = tf.placeholder('float')
cost = tf.reduce_sum(tf.pow(y_pred-Y, 2))/(2*n_samples) #L2 loss
sess.run(optimizer, feed_dict={X: x, Y: y})

代码将完美运行 .

我很想知道为什么我不能在feed_dict中使用相同的符号作为键值对?左边的“y”(键)是否应该参考上面的成本函数中的“y”?

3 回答

  • 21

    sess.run(optimizer, feed_dict={X: x, y: y}) 更改为 sess.run(optimizer, feed_dict={X: train_x, y: train_y}) . fedd_dict的值是您要提供给优化器的实际输入 .

  • 0

    有时您会反复检查和编辑,但会再次出错 .

    您可以从顶部运行代码(将tensorflow导入为tf)再到最后,也许您可以解决问题

  • 0

    feed_dict 参数是一个需要Tensor作为键的字典 . 在您更正的示例中, XY 是那些张量 .

    但是,如果您使用 XY 作为另一个变量的名称,您将覆盖初始张量, XY 将不再对应于图表中的张量 . Tensorflow无法理解您是否已覆盖图中的节点 .

    简而言之,您尝试对两个不同的变量使用相同的名称,这是不可能的 .

相关问题