Filipe Aleixo
Filipe Aleixo

Reputation: 4244

Tensorflow maxpool with dynamic ksize

I have the following code for a convolutional layer on TensorFlow. This layer is part of a larger computational graph.

# Define the shape of the filter
filter_shape = [1,
                config.char_filter_size,
                config.dim_char,
                config.dim_char]

# Define the convolutional layer weights and biases
W_conv = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1),
                     name="W_conv")
b_conv = tf.Variable(tf.constant(0.1, shape=[config.dim_char]),
                     name="b_conv")
# Do 2d convolution
conv = tf.nn.conv2d(char_embeddings,
                    W_conv,
                    strides=[1, 1, 1, 1],
                    padding="VALID",
                    name="conv")
# Apply nonlinearity
# h_conv has the same shape as conv
h_conv = tf.nn.relu(tf.nn.bias_add(conv, b_conv),
                    name="conv_relu")
# Maxpooling h_conv over dim 2 (char dim)

# ERROR HERE
conv_pooled = tf.nn.max_pool(h_conv,
                             ksize=[1, 1, tf.shape(h_conv)[-2], 1],
                             strides=[1, 1, 1, 1],
                             padding='VALID',
                             name="conv_max_pool")

When trying to run, I get the error:

TypeError: Expected int for argument 'ksize' not tf.Tensor shape=() dtype=int32.

is tf.nn.max_pool unable to handle dynamic ksize?

Upvotes: 6

Views: 3789

Answers (3)

user3606057
user3606057

Reputation: 25

As previously stated, k_size expects a list of integers not a list of Tensors. However, there is a fix applied. You can use:

from tensorflow.python.ops import gen_nn_ops
conv_pooled = gen_nn_ops.max_pool_v2(
conv,
ksize=[1,1, tf.shape(h_conv)[-2], 1],
strides=[1, 1, 1, 1],
padding='VALID',
name="pool")

Upvotes: 1

Styrke
Styrke

Reputation: 2664

It seems like you simply want to find the largest value over one of the dimensions which may be of dynamic size. If that is the case, you are probably better off using the tf.reduce_max() function instead of tf.nn.max_pool().

tf.reduce_max(
    h_conv,
    axis=2,
    keep_dims=True
)

I set keep_dims=True because it corresponds to what you would get if max pooling worked, but it is probably easier to work with the result if you set keep_dims=False.

Upvotes: 6

Salvador Dali
Salvador Dali

Reputation: 222491

The problem is not with 'dynamic ksize`. tf.nn.max_pool takes

a list of ints that has length >= 4

You have a list, but the 3-rd element is not an integer but a

A Tensor of type tf.int32.

So you should evaluate this value in session, extract the int from it and only after that you can use it.

Upvotes: 3

Related Questions