Understand tf.layers.max_pooling1d(): Max Pooling Layer for 1D Inputs – TensorFlow Tutorial

By | March 16, 2022

In this tutorial,we will use some examples to show you how to use tf.layers.max_pooling1d() function in tensorflow.

Syntax

tf.layers.max_pooling1d() is defined as:

tf.layers.max_pooling1d(
    inputs,
    pool_size,
    strides,
    padding='valid',
    data_format='channels_last',
    name=None
)

It is used for max pooling layer for 1D inputs on length axis.

Parameters

inputs: tensor with rank 3, [batch_size, length, channels]

pool_size: the size of the pooling window, it can be integer or tuple, list

strides: the strides of the pooling operation, integer or tuple/list

padding: valid or same

It will also return a tensor with rank 3.

How to use tf.layers.max_pooling1d()?

We will use an example to show you how to do.

For example:

import tensorflow as tf
a=tf.constant([[[0.1,0.2,0.5,0.2],
                [0.11,0.1,0.1,0.7]],

                [[0.2,0.33,0.21,0.43],
                [0.1,0.12,0.22,0.4]],

               [[0.2,0.3,0.2,0.3],
                [0.12,0.2,0.2,0.4]]])
print(a)
v = tf.layers.max_pooling1d(a, pool_size=2, strides=1, padding='same')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    x = sess.run(v)
    print(x.shape)
    print(x)

Here a is [3, 2, 4], when pool_size=2, strides=1, padding=’same’, we will get:

(3, 2, 4)
[[[0.11 0.2  0.5  0.7 ]
  [0.11 0.2  0.5  0.7 ]]

 [[0.2  0.33 0.22 0.43]
  [0.2  0.33 0.22 0.43]]

 [[0.2  0.3  0.2  0.4 ]
  [0.2  0.3  0.2  0.4 ]]]

From the above, we can find length = 2, it is equal to pool_size, length = pool_size = 2.

How about pool_size > length?

v = tf.layers.max_pooling1d(a, pool_size=3, strides=1, padding='same')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    x = sess.run(v)
    print(x.shape)
    print(x)

Here pool_size = 3 > length = 2, we will see:

(3, 2, 4)
[[[0.11 0.2  0.5  0.7 ]
  [0.11 0.2  0.5  0.7 ]]

 [[0.2  0.33 0.22 0.43]
  [0.2  0.33 0.22 0.43]]

 [[0.2  0.3  0.2  0.4 ]
  [0.2  0.3  0.2  0.4 ]]]

The result is same to pool_size = 2.

How about pool_size > length when padding = ‘valid’?

Look at this example:

v = tf.layers.max_pooling1d(a, pool_size=3, strides=1, padding='valid')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    x = sess.run(v)
    print(x.shape)
    print(x)

Run this code, we will get: ValueError: Negative dimension size caused by subtracting 3 from 2 for ‘max_pooling1d/MaxPool’ (op: ‘MaxPool’) with input shapes: [3,2,1,4].

tf.layers.max_pooling1d() vs tf.nn.max_pool()?

tf.nn.max_pool() also can implement max pool in tensorflow. Here is the tutorial:

Understand TensorFlow tf.nn.max_pool(): Implement Max Pooling for Convolutional Network – TensorFlow Tutorial

tf.nn.max_pool() also can work as tf.layers.max_pooling1d(), for example:

import tensorflow as tf
import numpy as np

a=tf.constant([[[0.1,0.2,0.5,0.2],
                [0.11,0.1,0.1,0.7],
                [0.01,0.12,0.25,0.12]],

                [[0.2,0.33,0.21,0.43],
                [0.1,0.23,0.11,0.53],
                [0.1,0.12,0.22,0.4]],

               [[0.2,0.3,0.2,0.3],
                [0.22,0.13,0.12,0.13],
                [0.12,0.2,0.2,0.4]]])
print(a)
v = tf.layers.max_pooling1d(a, pool_size=2, strides=1, padding='SAME')
a1 = tf.reshape(a,[3, 3, 1, 4])
v2 = tf.nn.max_pool(a1, ksize = [1, 2, 1, 1], strides = [1, 1, 1, 1], padding = 'SAME', data_format='NHWC')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    x = sess.run(v)
    print(x.shape)
    print(x)

    x2 = sess.run(v2)
    x2 = np.reshape(x2, [3, 3, 4])
    print(x2.shape)
    print(x2)

Run this code, we will get:

(3, 3, 4)
[[[0.11 0.2  0.5  0.7 ]
  [0.11 0.12 0.25 0.7 ]
  [0.01 0.12 0.25 0.12]]

 [[0.2  0.33 0.21 0.53]
  [0.1  0.23 0.22 0.53]
  [0.1  0.12 0.22 0.4 ]]

 [[0.22 0.3  0.2  0.3 ]
  [0.22 0.2  0.2  0.4 ]
  [0.12 0.2  0.2  0.4 ]]]
(3, 3, 4)
[[[0.11 0.2  0.5  0.7 ]
  [0.11 0.12 0.25 0.7 ]
  [0.01 0.12 0.25 0.12]]

 [[0.2  0.33 0.21 0.53]
  [0.1  0.23 0.22 0.53]
  [0.1  0.12 0.22 0.4 ]]

 [[0.22 0.3  0.2  0.3 ]
  [0.22 0.2  0.2  0.4 ]
  [0.12 0.2  0.2  0.4 ]]]

x and x2 are the same.