TensorFlow feed an integer
<p>I am trying to do a convolution over variable input sizes. To achieve that, I am using a batch size of 1. However, one of the nodes is a max pooling node which needs the shape of the input as a list <code>ksize</code>:</p>

<pre><code> pooled = tf.nn.max_pool(
                h,
                ksize=[1, self.input_size - filter_size + 1, 1, 1],
                strides=[1, 1, 1, 1],
                padding='VALID',
                name="pool")
</code></pre>

<p>Now, clearly the input_size can be inferred from the input (which is a placeholder):</p>

<pre><code>self.input_x = tf.placeholder(tf.int32, [None, None], name="input_x")
</code></pre>

<p>But I can't use <code>self.input_x.get_shape()[0]</code> because the shape is dynamic. So I intend to pass the input size as feed_dict at each step. However, I can't figure out how to pass an integer in feed_dict. Every placeholder is a tensor, so if I do:</p>

<pre><code>self.input_size = tf.placeholder(tf.int32, shape=(), name="input_size")
</code></pre>

<p>I would have to do <code>self.input_size.eval()</code> to get the int value, which gives me an error that I need to feed <code>input_size</code>. I guess it happens because eval triggers the computation BEFORE the training step happens, at which point there is no value for input_size.</p>

<p>Is there a way I can dynamically get an op that calculates the shape of the input or a way to pass an integer to a training step?</p>

<p>I'm not sure that it's the best way, but you can get dynamically the shape of <code>self.input_x</code> in a list with :</p>

<pre><code>input_shape = tf.unpack(tf.shape(self.input_x))
</code></pre>

<p><code>tf.shape(self.input_x)</code> give you a Tensor representing the shape of self.input_x and <code>f.unpack</code> convert it to a list of Tensor.</p>

<p>Now you can create your max pooling node with :</p>

<pre><code>pooled = tf.nn.max_pool(
                h,
                ksize=tf.pack([1, input_size[1] - filter_size + 1, 1, 1]),
                strides=[1, 1, 1, 1],
                padding='VALID',
                name="pool")
</code></pre>

<p>(if you needed the 2nd dimension of input_x)</p>

python neural-network tensorflow convolution conv-neural-network