使用占位符作为tensorflow中池的形状

时间:2017-06-15 23:58:09

标签: python tensorflow

我正在使用tensorflow中的神经网络,我有一个名为CNN_model的函数,它将三个占位符作为输入:

X = tf.placeholder(tf.float32, [1, None, 13])
Y = tf.placeholder(tf.int32, [None])

pool_shape = tf.placeholder(tf.int32, [1])

我使用值运行会话:

feed_dict={X: x, Y: y, MFCCS: x.shape[0]}

在会话中,我想做一个最大池层,它的形状由占位符定义,如下所示:

pool_window_size = [pool_shape, pool_shape]

pool = tf.layers.max_pooling2d(
        inputs = conv,
        pool_size = pool_window_size,
        strides = pool_window_size,
        name = "pool"
)

但是我收到了错误:

TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'Tensor'

我假设是因为在那一点上“pool_shape”是一个张量而不仅仅是一个整数,是否有办法获得该点张量中的任何值?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用当前会话sess以及pool_shapefeed_dict所需的相关输入

dimension = pool_shape.eval(sess, feed_dict = {inputs})[0]
pool_window_size = [dimension , dimension]

希望这有帮助!

修改 我假设这是您目前的代码:

pool_shape = tf.placeholder(tf.int32, [1])
pool_window_size = [pool_shape, pool_shape]

#...

for i in range(iterations):
    sess.run(Optimizer, feed_dict = {X:x, Y:y, pool_shape:value} 
    #value is the length/width you want to set for the pool_window_size

相反,我认为这可能会奏效。

pool_window_size = [pool_shape, pool_shape]

#...

for i in range(iterations):
    pool_shape = value
    sess.run(Optimizer, feed_dict = {X:x, Y:y} 

我不太确定maxpooling层是否会更新pool_size,你可以尝试告诉我。