Bunny Rabbit
Bunny Rabbit

Reputation: 8411

How to correctly set the value of placeholder in tensorflow?

I've written the following quick program in tensorflow to print fibonacci numbers. Initial fib numbers are initialized as placeholders x1 and x2 However when I try to feed the value of the placeholders in session.run it results in the error :

InvalidArgumentError: You must feed a value for placeholder tensor 'x2' with dtype int64 and shape [1]

Can you help me understand and fix the problem with my code?

import tensorflow as tf
session = tf.InteractiveSession()
import numpy as np

ones = tf.ones((1,))

n = 10
x1 = tf.placeholder(tf.int64, [1], name='x1')
x2 = tf.placeholder(tf.int64, [1], name='x2')
temp = tf.Variable(ones, name='temp')

tf.initialize_all_variables().run()
fib_nums = [x1, x2]
for i in range(100):
  temp = x1 + x2
  x1 = x2
  x2 = temp
  fib_nums.append(temp)

series = tf.stack(fib_nums)
print(np.ones(1).astype(np.int64))
session.run(series, feed_dict={x1:np.ones(1).astype(np.int64), x2:np.ones(1).astype(np.int64)})
print(series.eval())

Upvotes: 1

Views: 1304

Answers (3)

Mohan Radhakrishnan
Mohan Radhakrishnan

Reputation: 3207

I think another variation is this using tf.identity.

y = tf.identity(x1)
y1 = tf.identity(x2)
fib_nums = [y, y1]
for i in range(100):
  temp = y + y1
  y = y1
  y1 = temp
  fib_nums.append(temp)

This placeholder modification issue is also discussed here

And this is one more way to get the series.

def cond(i, x_next, x_prev):
    return x_next <= 100

def body( i, x_next, x_prev ):

    nextinseries = x_next + x_prev

    next = tf.Print(x_next, [x_next], message="Next is : ")
    prev = tf.Print(x_prev, [x_prev], message="Previous is : ")

    with tf.control_dependencies([nextinseries]):
        prev = tf.identity( next )


    return [i + 1, nextinseries , prev ]

sess = tf.Session()
sess.run(tf.global_variables_initializer())


sess.run(tf.while_loop(cond, body, [1, 1, 1]))

Upvotes: 1

javidcf
javidcf

Reputation: 59741

There are a couple of errors here. First, since you are reusing the Python names x1 and x2, when you give them in the feed_dict they no longer refer to the placeholders, but to the last results of the loop. So you should change your code so the keys that you give in feed_dict are truly the placeholders. Second, you first call session.run with the feed_dict, which is correct, but then you call series.eval(), which is essentially the same as the previous line, only you are not providing the feed_dict in this case so it is not going to work. You do not really need to call series.eval(), you can just take the value returned by session.run. Your fixed program could look something like this:

import tensorflow as tf
session = tf.InteractiveSession()
import numpy as np

ones = tf.ones((1,))

n = 10
# Reserve these Python names for the placeholders
x1_ph = tf.placeholder(tf.int64, [1], name='x1')
x2_ph = tf.placeholder(tf.int64, [1], name='x2')
temp = tf.Variable(ones, name='temp')

tf.initialize_all_variables().run()
# Use placeholders as initial values in the iterations
x1, x2 = x1_ph, x2_ph
fib_nums = [x1, x2]
for i in range(100):
  temp = x1 + x2
  x1 = x2
  x2 = temp
  fib_nums.append(temp)

series = tf.stack(fib_nums)
print(np.ones(1).astype(np.int64))
# You can just give lists as inputs and TensorFlow will convert their type
series_val = sess.run(series, feed_dict={x1_ph: [1], x2_ph: [1]})
print(series_val)

Output:

[1]
[[                   1]
 [                   1]
 [                   2]
 [                   3]
 [                   5]
 [                   8]
 [                  13]
 [                  21]
 [                  34]
 [                  55]
 [                  89]
 [                 144]
 [                 233]
 [                 377]
 [                 610]
 [                 987]
 [                1597]
 [                2584]
...

Upvotes: 2

MPękalski
MPękalski

Reputation: 7103

Just change tf.placeholder to a tensor and it will work

x1 = tf.ones(dtype=tf.int64, shape=1, name='x1') 
x2 = tf.ones(dtype=tf.int64, shape=1, name='x2') 

Upvotes: 0

Related Questions