Placeholder_2:0 is both fed and fetched

前端 未结 2 1097
醉话见心
醉话见心 2021-01-12 06:02

When I run this code:

x = tf.placeholder(tf.int32, shape=(None, 3))
with tf.Session() as sess: 
    feed_dict = dict()
    feed_dict[x] = np.array([[1,2,3],         


        
相关标签:
2条回答
  • 2021-01-12 06:44

    Are you sure this code covers what you are trying to achieve? You ask to read out whatever you pass through. This is not a valid call in tensorflow. If you want to pass through values and do nothing with it (what for?) you should have an identity operation.

    x = tf.placeholder(tf.int32, shape=(None, 3))
    y = tf.identity(x)
    
    with tf.Session() as sess: 
        feed_dict = dict()
        feed_dict[x] = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
        input = sess.run([y], feed_dict=feed_dict)
    

    The problem is "feeding" actually kind of overwrites whatever your op generates, thus you cannot fetch it at this moment (since there is nothing being really produced by this particular op anymore). If you add this identity op, you correctly feed (override x) do nothing with the result (identity) and fetch it (what identity produces, which is whatever you feeded as an output of x)

    0 讨论(0)
  • 2021-01-12 06:44

    I found out what I was doing wrong.

    x is a placeholder -- it holds information and evaluating x does not do anything. I forgot that vital piece of information and proceeded to attempt to run the Tensor x inside sess.run()

    Code similar to this would work if, say, there was another Tensor y that depended on x and I ran that like sess.run([y], feed_dict=feed_dict)

    0 讨论(0)
提交回复
热议问题