how to change the size of placeholder in tensforflow

1,546 views
Skip to first unread message

Kezhi Li

unread,
Jul 5, 2016, 12:41:08 PM7/5/16
to Discuss
Hi guys 

I am a learner of tensorflow. I met a problem with the placeholder. The key here is that, I want to change the size of data to feed the 'feed_dict', but it fails all the time. 

In detail, at first I give
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_output])
then after many steps, I run
res = sess.run(optimizer, feed_dict = {x: batch_x, y : batch_y })
where batch_x and batch_y's values are given previously. 

Now I want to change the size of placehold to, eg. 
x = tf.placeholder("float", [None, m_input])
and update the weights matrix W accordingly. However, 
res = sess.run(optimizer, feed_dict = {x: batch_x, y : batch_y })
cannot be run. it says
InvalidArgumentError: you must feed a value for placeholder tensor 'Placeholder_5' with dtype float
where 'Placeholder_5' is the x that firstly defined (not the updated one).

I read some article saying that I can use  
x = tf.placeholder("float", [None, None])
as open-size variable and feed it without re-define another x placeholder.  However, it still does not work

Is there anyone knowing that how to change the size of placeholder? 

Thanks

Sam A

unread,
Jul 5, 2016, 2:20:51 PM7/5/16
to Kezhi Li, Discuss
There are two things going on here:

First, it's important to note that Operations, placeholders, Tensors, etc. are immutable, and thus you cannot change them once they are defined. When you first define:

`x = tf.placeholder("float", [None, n_input])`

It creates a placeholder node inside the graph and assigns a handle to that node to the variable `x`. When you later add in the following:

`x = tf.placeholder("float", [None, m_input])`

It doesn't change the first placeholder- rather, it creates a brand new placeholder and assigns a new handle to `x`. The first placeholder still exists, but there is no Python variable that has access to it anymore. If you want to be able to change the size of your input data to be any size tensor, the advice you received is correct, `x = tf.placeholder("float", [None, None])` will let you put any size tf.float32. That said, depending on your exact application, having a completely variable sized input may not be what you're looking for, but for now I'll assume it is what you want.

The second thing to look at: If you read the error that you're receiving, you'll notice that it isn't complaining about the shape of the input you're providing, but rather the data-type of `batch_x`. That is, TensorFlow thinks that the data you're reading in isn't of type "float" (or tf.float32); that is what is causing the error, not the variable sized placeholders.

How are you reading in your `batch_x` and `batch_y` values?

As a final aside, I recommend changing the `dtype` values you're using from `"float"` to `tf.float32`, like this: 

`x = tf.placeholder(tf.float32, [None, None])`

Under the hood they run the same, but the specificity will help out with debugging in the future :D

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/8fa38d59-444b-437f-bc2e-134e80d1048f%40tensorflow.org.

Reply all
Reply to author
Forward
0 new messages