问题
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 ksize:
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")
Now, clearly the input_size can be inferred from the input (which is a placeholder):
self.input_x = tf.placeholder(tf.int32, [None, None], name="input_x")
But I can't use self.input_x.get_shape()[0] 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:
self.input_size = tf.placeholder(tf.int32, shape=(), name="input_size")
I would have to do self.input_size.eval() to get the int value, which gives me an error that I need to feed input_size. I guess it happens because eval triggers the computation BEFORE the training step happens, at which point there is no value for input_size.
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?
回答1:
I'm not sure that it's the best way, but you can get dynamically the shape of self.input_x in a list with :
input_shape = tf.unpack(tf.shape(self.input_x))
tf.shape(self.input_x) give you a Tensor representing the shape of self.input_x and f.unpack convert it to a list of Tensor.
Now you can create your max pooling node with :
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")
(if you needed the 2nd dimension of input_x)
来源:https://stackoverflow.com/questions/39821942/tensorflow-feed-an-integer