python-3.xtensorflow

Tensorflow populating placeholder with an array


I am having difficulty populating a placeholder with a tensor. I've searched but can only find values with single values being supplied into the computation graph.

Here's my code:

import tensorflow as tf
import numpy as np

features = [ [0,0], [0,1], [1,0], [1,1] ]
labels = [ 0, 1, 1, 0]

x = tf.placeholder(tf.float32, [2, 1], name="inputs")
W = tf.Variable(np.random.rand(1, 4), dtype=tf.float32, name="hidden1")
b = tf.Variable(np.ones((2,4)), name="b", dtype=tf.float32)



with tf.Session() as sess:
  input_data = np.random.rand(2, 1)

  feed_dict = {
      x:input_data
  }

  sess.run(tf.global_variables_initializer())
  sess.run(feed_dict)

  print(x.eval(session=sess))

gives me the following error:

has invalid type <class 'numpy.ndarray'>, must be a string or Tensor.

I have also tried defining the placeholder as a tf.Constant:

input_data = tf.constant([[0],[1]], dtype=tf.float32)

This gives me a different error:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'inputs_54' with dtype float and shape [2,1]
 [[Node: inputs_54 = Placeholder[dtype=DT_FLOAT, shape=[2,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

I just want to supply a 2 x 1 matrix like [[0],[1]] but it's proving difficult.


Solution

  • In order to evaluate the tensor x, it has to be part of the sess.run,

    You need to call,

     print(sess.run(x,{x:np.array([[0],[1]])}))
     #x is the tensor you want to execute, 
     #feed_dict: feeds the input to the placeholder