How can I use integer that I supply into tf.placeholder(tf.int32)?
<p>I need to initialize a dynamic list which has length given by an integer that I will put into a placeholder through feed_dict. </p>

<p>I set up the graph through this code:  </p>

<pre><code>num_placeholder = tf.placeholder(tf.int32) 
input_data = list() 
for _ in range(num_placeholder):
    input_data.append(1) 
</code></pre>

<p>But get the following error: <code>'Tensor' object cannot be interpreted as an integer</code></p>

<p>How can I use the <code>int</code> that I put into the <code>num_placeholder</code>? </p>

<p><code>num_placeholder</code> is a <code>Tensor</code> object, it can not be directly used as an <code>int</code>, it must be valuated first. See documentation on <a href="https://www.tensorflow.org/versions/r0.10/api_docs/python/io_ops.html#placeholder" rel="nofollow"><code>placeholder</code></a> method.</p>

<p>If you want to use <code>num_placeholder</code> object as <code>int</code> you must evaluate it as follows:</p>

<pre><code># create a placeholder       
num_placeholder = tf.placeholder(tf.int32)

# create an identity operation
# because same Tensor can not be both fed and fetched
num_op = tf.identity(num_placeholder)

with tf.Session() as sess:
    # feed value to placeholder and fetch it
    num = sess.run([num_op], feed_dict={num_placeholder: 5})
</code></pre>

<p>Now <code>num</code> will is a list <code>[5]</code> and you can use <code>num[0]</code> in your loop. I am not quite sure what you are trying to achieve, but if your goal is to create a vector of ones of some custom length that is fed during runtime, here is how it can be solved by means of Tensorflow:</p>

<pre><code>num_placeholder = tf.placeholder(tf.int32)
ones_op = tf.ones(shape=[num_placeholder], dtype=tf.int32)
with tf.Session() as sess:
    ones = sess.run([ones_op], feed_dict={num_placeholder: 5})
</code></pre>

<p><code>ones ==&gt; &lt;type 'list'&gt;: [array([1, 1, 1, 1, 1], dtype=int32)]</code>,
see <a href="https://www.tensorflow.org/versions/r0.10/api_docs/python/constant_op.html#ones" rel="nofollow"><code>ones</code></a> </p>

python machine-learning tensorflow