TensorFlow Basics (f) - tf.nn.max_pool () and tf.nn.avg_pool ()

tf.nn.max_pool () and tf.nn.avg_pool () is a function of the maximum TensorFlow pool and pooling the average, in the method of comparison core convolutional neural network.

Some convolution and is very similar, you can refer TensorFlow Basics (seven) - tf.nn.conv2d () (with code Comments)

Two function calls the same way, in order to explain the lower tf.nn.max_pool () be applied as an example to explain.


Function format:

tf.nn.max_pool(value, ksize, strides, padding, name=None)

Parameter Description:

The first parameter value: input image needs to be done pooled, enter feaure map, because the pool of the convolution behind. shape is [batch, in_height, in_width, in_channels]:

batch: batch a number of training images
in_height: height of input image
in_width: width of the input image
in_channels: number of input feature map of the
second parameter ksize: similar to the convolution filter, the window size of the pool, is a one-dimensional array of length 4, but the first and last of the array must be 1, i.e. [1, height, width, 1 ]. This means that the pool filter layer that can not be done on a batch pooling and channels. In practice, the most used filter size is [1, 2, 2, 1] or [1, 3, 3, 1].

height: The height of the filter
width: the width of the filter
third argument strides: step on the different dimensions, is a one-dimensional vector of length 4, [1, strides, strides, 1], and the last a first dimension dimensional numerical requirements must be 1. Because the convolution step only the layer length and width of the active matrix.

The fourth parameter padding: string type, whether to consider the boundary value of "SAME" and "VALID", "SAME" boundary is considered, insufficient time is filled with the surrounding, "VALID" boundary is not considered. .

 Return tensor, shape still [batch, height, width, channels ] in this form.

Below to illustrate the usage tf.nn.max_pool () function by way of example:

Input is a 3 * 3 sized feature map, the number is 1, the size of the filter is [1, 2, 2, 1], the step size is [1,1,1,1], when the padding value 'VALID', the last feature map to get a 2 * 2. FIG 1 is a shape for the final output [1,2,2,1] of the tensor.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
import tensorflow as tf
 
value = tf.Variable(tf.random_normal([1, 3, 3, 1]))
ksize = [1, 2, 2, 1]
 
pool = tf.nn.max_pool(value, ksize, strides=[1, 1, 1, 1], padding='VALID')
 
with tf.Session() as sess:
    # 初始化变量
    op_init = tf.global_variables_initializer()
    sess.run(op_init)
 
    print("value的值为:")
    print(sess.run(value))
    print("池化值为:")
    print(sess.run(pool))


The implementation process is as follows:

operation result:
 

Published 352 original articles · won praise 115 · views 130 000 +

Guess you like

Origin blog.csdn.net/Aidam_Bo/article/details/103190011