<p dir="auto">Here is the problem:<br>
I would like to train a network with two different shapes of input tensor. Each epoch chooses one type. Here I write a small code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import tensorflow as tf
import numpy as np


with tf.Session() as sess:

    imgs1 = tf.placeholder(tf.float32, [4, 224, 224, 3], name = 'input_imgs1')
    imgs2 = tf.placeholder(tf.float32, [4, 180, 180, 3], name = 'input_imgs2')
    epoch_num_tf = tf.placeholder(tf.int32, [], name = 'input_epoch_num')

    imgs = tf.cond(tf.equal(tf.mod(epoch_num_tf, 2), 0), 
                lambda: tf.Print(imgs2, [imgs2.get_shape()], message='(even number) input epoch number is '),
                lambda: tf.Print(imgs1, [imgs1.get_shape()], message='(odd number) input epoch number is'))


    print(imgs.get_shape())


    for epoch in range(10):
        epoch_num = np.array(epoch).astype(np.int32)
        imgs1_input = np.ones([4, 224, 224, 3], dtype = np.float32)
        imgs2_input = np.ones([4, 180, 180, 3], dtype = np.float32)

        output = sess.run(imgs, feed_dict = {epoch_num_tf: epoch_num,
                                          imgs1: imgs1_input,
                                          imgs2: imgs2_input})"><pre class="notranslate"><code class="notranslate">import tensorflow as tf
import numpy as np


with tf.Session() as sess:

    imgs1 = tf.placeholder(tf.float32, [4, 224, 224, 3], name = 'input_imgs1')
    imgs2 = tf.placeholder(tf.float32, [4, 180, 180, 3], name = 'input_imgs2')
    epoch_num_tf = tf.placeholder(tf.int32, [], name = 'input_epoch_num')

    imgs = tf.cond(tf.equal(tf.mod(epoch_num_tf, 2), 0), 
                lambda: tf.Print(imgs2, [imgs2.get_shape()], message='(even number) input epoch number is '),
                lambda: tf.Print(imgs1, [imgs1.get_shape()], message='(odd number) input epoch number is'))


    print(imgs.get_shape())


    for epoch in range(10):
        epoch_num = np.array(epoch).astype(np.int32)
        imgs1_input = np.ones([4, 224, 224, 3], dtype = np.float32)
        imgs2_input = np.ones([4, 180, 180, 3], dtype = np.float32)

        output = sess.run(imgs, feed_dict = {epoch_num_tf: epoch_num,
                                          imgs1: imgs1_input,
                                          imgs2: imgs2_input})
</code></pre></div>
<p dir="auto">When I execute it, the output of <code class="notranslate">imgs.get_shape()</code> is <code class="notranslate">(4, ?, ?, 3)</code> i.e. <code class="notranslate">imgs.get_shape()[1]=None, imgs.get_shape()[2]=None</code>.</p>
<p dir="auto">But I will use the value of the output of <code class="notranslate">imgs.get_shape()</code> to define the kernel (<code class="notranslate">ksize</code>) and strides size (<code class="notranslate">strides</code>) of the <code class="notranslate">tf.nn.max_pool()</code> e.g. <code class="notranslate">ksize=[1,imgs.get_shape()[1]/6, imgs.get_shape()[2]/6, 1]</code> in the future code. I think ksize and strides cannot support <code class="notranslate">tf.Tensor</code> value.</p>
<p dir="auto">How to solve this problem?</p>
<p dir="auto">I also asked in the <a href="http://stackoverflow.com/questions/40932002/tensorflow-how-to-set-the-shape-of-tensor-with-different-conditional-statements" rel="nofollow">stackoverflow</a>.</p>
<p dir="auto">I am trying to build a convolution (followed by max_pool) for variable length input size. Since the length of the input in a batch should be the same, I set the batch size to 1. However, for some reason ksize in tf.nn.max_pool is a list attribute required at run-time. This would prevent any neural network that uses variable input size to apply max pool. Is there a workaround for this? Should ksize be an input tensor otherwise?</p>