2

I'm new to Tensorflow. I'm trying to add a max pooling layer after a 1D convolution layer:

import tensorflow as tf
import math
sess = tf.InteractiveSession()

length=458
# These will be inputs
## Input pixels, image with one channel (gray)
x = tf.placeholder("float", [None, length])
# Note that -1 is for reshaping
x_im = tf.reshape(x, [-1,length,1])
## Known labels
# None works during variable creation to be
# unspecified size
y_ = tf.placeholder("float", [None,2])

# Conv layer 1
num_filters1 = 2
winx1 = 3
W1 = tf.Variable(tf.truncated_normal(
    [winx1, 1 , num_filters1],
    stddev=1./math.sqrt(winx1)))
b1 = tf.Variable(tf.constant(0.1,
                shape=[num_filters1]))
#  convolution, pad with zeros on edges
xw = tf.nn.conv1d(x_im, W1,
                  stride=5,
                  padding='SAME')
h1 = tf.nn.relu(xw + b1)
#  Max pooling, no padding on edges
p1 = tf.nn.max_pool(h1, ksize=[1, 1, 2, 1],
        strides=[1, 1, 1, 1], padding='VALID')

But I get errors which I figure out why is that?

Maxim
  • 52,561
  • 27
  • 155
  • 209
H.H
  • 188
  • 1
  • 13

1 Answers1

6

tf.nn.max_pool is for 2D pooling, i.e., it expects input tensor of rank 4 (yours is rank 3). You should either expand the dimensions of the input or simply use tf.layers.max_pooling1d:

p1 = tf.layers.max_pooling1d(h1, pool_size=2, strides=1, padding='VALID')
Maxim
  • 52,561
  • 27
  • 155
  • 209