TensorFlow提要一个整数 [英] TensorFlow feed an integer
问题描述
我正在尝试对可变输入大小进行卷积.为此,我使用1的批处理大小.但是,节点之一是最大池节点,它需要将输入的形状作为列表ksize
:
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")
现在,很明显可以从输入(占位符)推断出input_size:
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")
但是我不能使用self.input_x.get_shape()[0]
,因为形状是动态的.因此,我打算在每个步骤中将输入大小作为feed_dict传递.但是,我不知道如何在feed_dict中传递一个整数.每个占位符都是张量,所以如果我这样做:
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")
我必须做self.input_size.eval()
才能得到int值,这给了我一个错误,我需要输入input_size
.我猜这是因为eval在训练步骤发生之前触发了计算,此时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?
推荐答案
我不确定这是最好的方法,但是您可以使用:
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)
为您提供一个表示self形状的张量.input_x和f.unpack
将其转换为张量列表.
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")
(如果需要input_x的第二维)
(if you needed the 2nd dimension of input_x)
这篇关于TensorFlow提要一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!