2017-04-23 5 views
3

TensorFlowの畳み込みレイヤーには次のコードがあります。この層は、より大きな計算グラフの一部です。Tensorflow maxpool with dynamic ksize

# 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") 

実行しようとすると、私はエラーを取得:

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

tf.nn.max_poolダイナミックksizeを処理することができませんか?

答えて

4

動的サイズの可能性があるディメンションのうち、最大の値を見つけたいと思うようです。 その場合は、tf.nn.max_pool()の代わりにtf.reduce_max()関数を使用する方がよいでしょう。

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

それが最大のプールが働いていた場合、あなたはなるだろうものに対応していますが、keep_dims=Falseを設定した場合、結果で動作するように、おそらく簡単ですので、私はkeep_dims=Trueを設定します。

3

問題は「動的ksize」ではありません。 tf.nn.max_poolだからあなたを、セッションでこの値を評価し、それからint型を抽出し、それだけで後にすべきであるあなたがリストを持っていますが、3-RD要素がnot an integerある

a list of ints that has length >= 4

かかりますが、

A Tensor of type tf.int32.

それを使用することができます。

関連する問題