tf.nn.conv2d用法

tf.nn.conv2d:给定input和4D filters张量计算2D卷积。

tf.nn.conv2d(
    input, filters, strides, padding, data_format='NHWC', dilations=None, name=None
)

输入input张量可以具有秩4或更高,其中,形状维度[:-3] 被认为是批量维度(batch_shape)。

给定shape的输入张量 batch_shape + [in_height, in_width, in_channels]和shape的过滤器/内核张量[filter_height, filter_width, in_channels, out_channels]batch_shape 为图片的数量,in_height为图片高度,in_width为图片宽度,in_channels为图片的通道数,灰度图该值为1,彩色图为3;其中 filter_height为卷积核高度,filter_width为卷积核宽度,in_channels是输入通道数 ,和 inputin_channels要保持一致,out_channels是输出通道数,即卷积核数量。此op执行以下操作:

  1. 将过滤器展平为形状为[filter_height * filter_width * in_channels, output_channels]的二维矩阵 。
  2. 从输入张量中提取图像补丁以形成形状为[batch, out_height, out_width, filter_height * filter_width * in_channels]虚拟 张量。
  3. 对于每个补丁,将滤波器矩阵和图像补丁向量右乘。

详细而言,使用默认的NHWC格式,

output[b, i, j, k] =
    sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *
                    filter[di, dj, q, k]

必须有strides[0] = strides[3] = 1。对于最常见的水平和垂直步幅相同的情况,strides = [1, stride, stride, 1]

Args

input 一个Tensor。必须是下列类型之一: halfbfloat16float32float64。等级至少为4的张量。维度顺序根据data_format的值进行解释。所有内部的3个维度都用作批次维度。有关详情,请参见下文。 
filters 一个Tensor。必须具有与input相同的类型。形状为[filter_height, filter_width, in_channels, out_channels]的4D张量 
strides 一个int或ints列表具有长度124input的每个维度的滑动窗口的步幅。如果给出单个值,则将其复制到HW维度中。默认情况下,NC维度设置为1。维度顺序由data_format的值确定,有关详细信息,请参见下文。 
padding 任一string "SAME""VALID"表示使用的填充算法来类型,或一个列表表示在每个维度的开始和结束的明确填充。如果使用显式填充,而data_format是"NHWC",则应采用形式 [[0, 0], [pad_top,pad_bottom], [pad_left, pad_right], [0, 0]]。如果使用显式填充,并且data_format是"NCHW",则应采用形式[[0, 0], [0, 0],[pad_top, pad_bottom], [pad_left, pad_right]]。表示的是卷积的形式,是否考虑边界。"SAME"是考虑边界,不足的时候用0去填充周围,"VALID"则不考虑
data_format string来自"NHWC","NCHW"的可选内容。默认为 "NHWC"。指定输入和输出数据的数据格式。使用默认格式“ NHWC”,数据按以下顺序存储:batch_shape + [height, width, channels] 。或者,格式可以是“ NCHW”,数据存储顺序为:batch_shape + [channels, height, width] 。
dilations 一个int或ints列表具有长度12或者4,默认为1。对input的各个维度的扩张因子。如果给出单个值,则将其复制到HW维度中。默认情况下,NC维度设置为1。如果设置为k> 1,则该维度上每个过滤器元素之间将又k-1个跳过单元格。维度顺序由data_format的值确定,有关详细信息,请参见上文。如果4-d张量必须为1,则批量和深度维度的膨胀。 
name 操作的名称(可选)。

Returns

一个Tensor。具有和input相同的类型和相同的外部批处理形状。

示例程序:

import tensorflow as tf
import numpy as np

x_in = np.array([[
    [[2], [1], [2], [0], [1]],
    [[1], [3], [2], [2], [3]],
    [[1], [1], [3], [3], [0]],
    [[2], [2], [0], [1], [1]],
    [[0], [0], [3], [1], [2]], ]])

kernel_in = np.array([
    [[[2, 0.1]], [[3, 0.2]]],
    [[[0, 0.3]], [[1, 0.4]]], ])

x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)

# out = tf.nn.conv2d(x, kernel, strides=[1, 1, 1, 1], padding='VALID')
# 输出结果是一个[1, 4, 4, 2]的张量:
# Tensor("Conv2D:0", shape=(1, 4, 4, 2), dtype=float32)
# [[[[10.         1.9      ]
#    [10.         2.2      ]
#    [ 6.         1.6000001]
#    [ 6.         2.       ]]
#
#   [[12.         1.4000001]
#    [15.         2.2      ]
#    [13.         2.7000003]
#    [13.         1.7      ]]
#
#   [[ 7.         1.7      ]
#    [11.         1.3000001]
#    [16.         1.3000001]
#    [ 7.         1.       ]]
#
#   [[10.         0.6      ]
#    [ 7.         1.4000001]
#    [ 4.         1.5000001]
#    [ 7.         1.4000001]]]]

out = tf.nn.conv2d(x, kernel, strides=[1, 1, 1, 1], padding='SAME')
# 输出结果是一个[1, 5, 5, 2]的张量:
# Tensor("Conv2D:0", shape=(1, 5, 5, 2), dtype=float32)
# [[[[10.          1.9       ]
#    [10.          2.2       ]
#    [ 6.          1.6000001 ]
#    [ 6.          2.        ]
#    [ 2.          1.        ]]
#
#   [[12.          1.4000001 ]
#    [15.          2.2       ]
#    [13.          2.7000003 ]
#    [13.          1.7       ]
#    [ 6.          0.3       ]]
#
#   [[ 7.          1.7       ]
#    [11.          1.3000001 ]
#    [16.          1.3000001 ]
#    [ 7.          1.        ]
#    [ 0.          0.3       ]]
#
#   [[10.          0.6       ]
#    [ 7.          1.4000001 ]
#    [ 4.          1.5000001 ]
#    [ 7.          1.4000001 ]
#    [ 2.          0.70000005]]
#
#   [[ 0.          0.        ]
#    [ 9.          0.6       ]
#    [ 9.          0.5       ]
#    [ 8.          0.5       ]
#    [ 4.          0.2       ]]]]

with tf.Session() as sess:
    print(out)
    print(sess.run(out))



import tensorflow as tf

# case 1
# 输入是1个 3*3 大小的张量,输入张量通道数是5,卷积核是 1*1 大小,数量是1
# 步长是[1,1,1,1]最后得到一个 3*3 的feature map
# 最后输出就是一个 shape为[1,3,3,1] 的张量
input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
filter = tf.Variable(tf.random_normal([1, 1, 5, 1]))
op1 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# 输出结果:
# ******************** op2 ********************
# [[[[ 2.1436303 ]
#    [ 2.0738544 ]
#    [-0.17929938]]
#
#   [[-2.0726924 ]
#    [-1.5222    ]
#    [ 1.3480619 ]]
#
#   [[ 0.59065   ]
#    [ 4.6908927 ]
#    [-2.7029667 ]]]]

# case 2
# 输入是1个 3*3 大小的张量,输入张量通道数是5,卷积核是 2*2 大小,数量是1
# 步长是[1,1,1,1]最后得到一个 3*3 的feature map
# 最后输出就是一个 shape为[1,3,3,1] 的张量
input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
filter = tf.Variable(tf.random_normal([2, 2, 5, 1]))
op2 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# 输出结果:
# ******************** op2 ********************
# [[[[-2.726498  ]
#    [ 4.578858  ]
#    [-4.247362  ]]
#
#   [[-4.46906   ]
#    [ 2.0851839 ]
#    [ 0.12348425]]
#
#   [[-0.16621417]
#    [-3.6783643 ]
#    [ 1.403814  ]]]]

# case 3
# 输入是1个 3*3 大小的张量,输入张量通道数是5,卷积核是 3*3 大小,数量是1
# 步长是[1,1,1,1]最后得到一个 1*1 的feature map (不考虑边界)
# 最后输出就是一个 shape为[1,1,1,1] 的张量
input = tf.Variable(tf.random_normal([1, 3, 3, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 1]))
op3 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
# 输出结果:
# ******************** op3 ********************
# [[[[-14.692696]]]]


# case 4
# 输入是1个 5*5 大小的张量,输入张量通道数是5,卷积核是 3*3 大小,数量是1
# 步长是[1,1,1,1]最后得到一个 3*3 的feature map (不考虑边界)
# 最后输出就是一个 shape为[1,3,3,1] 的张量
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 1]))
op4 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
# 输出结果:
# ******************** op4 ********************
# [[[[  7.8252077]
#    [ -6.8241425]
#    [ -4.5439234]]
#
#   [[  5.2957616]
#    [ -2.3116708]
#    [-10.82259  ]]
#
#   [[ -2.663844 ]
#    [  2.8965201]
#    [  3.1595905]]]]

# case 5
# 输入是1个 5*5 大小的张量,输入张量通道数是5,卷积核是 3*3 大小,数量是1
# 步长是[1,1,1,1]最后得到一个 5*5 的feature map (考虑边界)
# 最后输出就是一个 shape为[1,5,5,1] 的张量
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 1]))
op5 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# 输出结果:
# ******************** op5 ********************
# [[[[ 3.446119  ]
#    [ 6.889248  ]
#    [-0.74263513]
#    [-5.5886226 ]
#    [-4.0011005 ]]
#
#   [[-7.598516  ]
#    [-2.5811138 ]
#    [-5.9887714 ]
#    [-2.423468  ]
#    [ 3.302377  ]]
#
#   [[-4.1364555 ]
#    [-1.5923785 ]
#    [-0.2860764 ]
#    [ 1.7675163 ]
#    [ 7.9709225 ]]
#
#   [[ 4.975916  ]
#    [ 3.2795277 ]
#    [-1.8256065 ]
#    [-4.5424066 ]
#    [ 3.1844525 ]]
#
#   [[-0.63261276]
#    [-3.9194276 ]
#    [-6.0611115 ]
#    [-6.9680305 ]
#    [-2.8860292 ]]]]

# case 6
# 输入是1个 5*5 大小的张量,输入张量通道数是5,卷积核是 3*3 大小,数量是7
# 步长是[1,1,1,1]最后得到一个 5*5 的feature map (考虑边界)
# 最后输出就是一个 shape为[1,5,5,7] 的张量
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 7]))
op6 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
# 输出结果:
# ******************** op6 ********************
# [[[[-3.0840011e+00  1.2335920e-01 -4.5059223e+00 -2.1750627e+00
#     -2.7304339e+00 -5.1604855e-01  1.3690400e+00]
#    [ 4.7401037e+00 -6.0819392e+00 -3.0767975e+00  2.6486878e+00
#     -6.2960935e+00 -1.0540801e+01  3.0819988e+00]
#    [ 1.7505054e+00  5.4914937e+00 -1.0307097e+00 -4.4227147e+00
#      4.4376817e-01  3.7327261e+00 -2.4275784e+00]
#    [-4.3396387e+00  3.7363658e+00 -1.0957268e+00  4.5229203e-01
#      2.0793560e+00  9.6609974e-01 -5.0022750e+00]
#    [ 1.1345127e+00  1.9144246e-01 -3.1545307e-02 -4.1797557e-01
#     -1.0004331e+00  6.3729405e-01  4.8672795e-02]]
#
#   [[ 4.9904194e+00 -2.6558199e+00 -3.3613873e-01  3.5926490e+00
#      2.0895982e+00  2.3332577e+00  5.3478036e+00]
#    [-4.6558094e+00  3.3834887e+00 -2.0608113e+00 -7.6377983e+00
#      5.3955240e+00  7.0295696e+00 -7.2131968e+00]
#    [-1.7864041e+00 -7.0538968e-01  1.5037076e+01  2.4281028e-01
#     -4.6876159e+00 -1.1438875e+00  2.5164890e+00]
#    [ 2.8490059e+00 -3.0830842e-01  1.1872043e-01  6.8919549e+00
#      1.7080204e+00  1.7034137e+00  2.3760687e-01]
#    [ 2.2561960e+00  3.4825525e+00 -1.4772111e+01 -1.0685290e+01
#     -6.5574465e+00  7.2971749e+00 -1.4437375e+00]]
#
#   [[ 4.2684340e+00 -4.3361559e+00  1.0803232e+00  3.5441415e+00
#      8.9037914e+00  7.2594132e+00  7.2638750e+00]
#    [-6.8646002e-01 -8.9739448e-01  5.8036337e+00  7.4364100e+00
#      1.1704000e+01  4.4205704e+00  3.0446523e-01]
#    [-2.8831110e+00 -5.2022009e+00  7.9378448e+00 -3.2306526e+00
#      7.3811979e+00  6.2627382e+00 -4.6261063e+00]
#    [-6.3880229e-01 -1.0363024e+01  7.0129766e+00  8.6191149e+00
#      1.2183407e+01  7.0540304e+00 -2.1471415e+00]
#    [ 1.6519511e-01  9.4482350e-01  3.3437994e+00  1.7440913e+00
#      1.0958947e+01 -6.3905849e+00  7.7824841e+00]]
#
#   [[-5.9248290e+00  7.9262257e-03 -3.4675770e+00  2.5212393e+00
#      4.6964250e+00 -1.2980299e+01  6.6482816e+00]
#    [ 1.6509128e-01  1.3398497e+01 -1.6516523e+01 -5.0966377e+00
#      3.5735250e+00  4.7559333e+00 -1.3719524e+00]
#    [ 5.6323524e+00 -1.7552935e+01  6.5047069e+00  2.4060578e+00
#     -6.7384124e+00 -4.1142387e+00  8.3369007e+00]
#    [ 1.1545516e+01  4.2000041e+00  3.6326356e+00  3.3518002e+00
#     -5.8180132e+00 -8.6836344e-01  6.1310697e+00]
#    [-6.1742949e+00  2.8111699e+00  4.4471732e-01 -1.3193527e+00
#     -5.4388676e+00 -1.3804424e+00 -4.0689611e-01]]
#
#   [[-1.9953527e+00  3.3288798e+00 -4.4504986e+00  6.2108145e+00
#      4.2928514e+00 -1.0003451e+01 -3.9065666e+00]
#    [ 1.3419192e+00  7.2219830e+00 -6.9047012e+00  3.3905752e+00
#     -5.0909300e+00  1.3293836e+01 -7.6556101e+00]
#    [-7.3027096e+00  4.5334468e+00 -2.6074278e-01 -1.0191909e+01
#      3.3845627e+00 -7.1441424e-01 -1.0066732e+00]
#    [ 2.2974596e+00 -4.5104642e+00  9.2605171e+00  3.9580934e+00
#      3.9759703e+00 -7.4361342e-01  1.2278461e+00]
#    [ 3.2552500e+00 -7.5348072e+00 -9.5567679e-01  2.1697264e+00
#     -3.3733122e+00  2.1065104e+00 -2.6742821e+00]]]]

# case 7
# 输入是1个 5*5 大小的张量,输入张量通道数是5,卷积核是 3*3 大小,数量是7
# 步长是[1,2,2,1]最后得到7个 3*3 的feature map (考虑边界)
# 最后输出就是一个 shape为[1,3,3,7] 的张量
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 7]))
op7 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
# 输出结果:
# ******************** op7 ********************
# [[[[  0.293367    -6.8127875    0.72176373  -4.609955     0.29551882
#       4.215855    -0.9913437 ]
#    [  0.36416888  11.682545     4.806132    -7.0474143    4.167954
#       2.4354155   -2.5111024 ]
#    [ -0.55445254  -6.615763    -2.0677602   -1.5091647   -7.727282
#     -11.306014     2.890195  ]]
#
#   [[ -0.28828835  -0.80325335   1.7683787    6.4272923   -4.341469
#       0.8893982    0.39250994]
#    [  3.596493    -0.8119854   -3.97007     -2.0908122   -1.1287336
#      -0.3859545   -7.195946  ]
#    [ -5.793783     6.1878567   15.109923     0.6127636   -3.6183724
#       1.4538065  -10.928911  ]]
#
#   [[  7.3411665   -4.1733685   -3.3012686   -3.3117685   -0.5984683
#       0.8338492   -1.9214034 ]
#    [ -0.5107512  -12.7436285    1.8505284   -1.0571606    0.7512574
#       4.2915535    9.583618  ]
#    [  2.5063615    5.879011     4.90127     -0.07179046   2.8028073
#      -0.05491097  -5.3217688 ]]]]

# case 8
# 输入是10 个 5*5 大小的张量,输入张量通道数是5,卷积核是 3*3 大小,数量是7
# 步长是[1,2,2,1]最后每张图得到7个 3*3 的feature map (考虑边界)
# 最后输出就是一个 shape为[10,3,3,7] 的张量
input = tf.Variable(tf.random_normal([10, 5, 5, 5]))
filter = tf.Variable(tf.random_normal([3, 3, 5, 7]))
op8 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
# 输出结果:
# ******************** op8 ********************
# [[[[-1.50972009e+00  3.28846717e+00  2.56372422e-01 -2.70064521e+00
#      4.40395069e+00  8.26167464e-01 -7.68952465e+00]
#    [-1.46069360e+00  1.66591525e+00  4.58909464e+00  2.48840261e+00
#      1.63206520e+01 -5.99188614e+00  1.35770631e+00]
#    [ 4.75995207e+00  7.77311897e+00 -2.12295747e+00  4.07630301e+00
#     -3.84346151e+00 -1.19016916e-01  1.87570047e+00]]
#
#   [[ 8.78928661e+00  8.35301638e-01 -7.33191681e+00  4.09586620e+00
#      8.46557617e-01  8.00785637e+00 -2.03714895e+00]
#    [ 5.73713017e+00 -1.49388247e+01  3.70609355e+00  9.50882626e+00
#      1.17435322e+01  5.17303848e+00 -1.73004842e+00]
#    [-4.42001724e+00 -3.54912615e+00  7.96449041e+00 -3.40062141e-01
#      1.23415983e+00 -6.74519539e-02 -7.09112930e+00]]
#
#   [[ 6.89682722e-01  1.01104822e+01 -2.62892461e+00  6.52079821e+00
#     -1.58322823e+00 -6.27171707e+00  3.94091272e+00]
#    [ 5.16624117e+00  2.95724177e+00 -3.22336292e+00 -3.29455805e+00
#      7.71984863e+00  3.12455177e-01  1.52014565e+00]
#    [ 3.40628886e+00 -7.10564077e-01  7.25713825e+00 -2.20483243e-01
#      1.51156664e-01 -2.01525712e+00 -8.09532547e+00]]]
#
#
#  [[[ 2.34457016e+00 -2.26403069e+00  3.01330864e-01 -4.59966421e+00
#     -5.70768118e-01  3.28723526e+00  1.81965542e+00]
#    [-2.02637410e+00 -2.98199654e+00 -2.61881232e+00 -7.22932386e+00
#      3.42993617e+00  2.40504479e+00 -5.41764259e+00]
#    [ 2.87678647e+00  1.45507336e+00 -1.83620012e+00  2.49595642e+00
#     -5.10922480e+00  2.40312648e+00  1.78781736e+00]]
#
#   [[-1.33651924e+01  3.00249600e+00  9.26462364e+00  2.58863783e+00
#      3.29223633e+00 -5.51135063e+00  4.08538818e+00]
#    [ 6.96175003e+00  6.51313400e+00 -2.43331528e+00  3.99290252e+00
#      3.79648089e+00 -3.50887942e+00  3.35554075e+00]
#    [-2.33746815e+00  2.42192769e+00  3.05796742e+00 -2.99886394e+00
#     -4.59325254e-01 -9.66714096e+00 -3.06445456e+00]]
#
#   [[-4.99330187e+00 -3.59256339e+00  3.12972355e+00 -1.05485499e+00
#     -2.55326033e+00  4.25835180e+00 -6.87904644e+00]
#    [-6.78382349e+00  1.99727798e+00 -5.68853617e+00  1.16159916e+00
#     -6.48815918e+00  1.27502060e+00 -1.44755840e-02]
#    [-6.61731911e+00 -2.98836422e+00 -1.89204836e+00 -5.07599735e+00
#      1.98823047e+00  4.22331476e+00 -3.81494570e+00]]]
#
#
#  [[[ 6.55350590e+00  1.52602494e+00  2.64053488e+00  3.48391485e+00
#      1.15554178e+00  5.93114519e+00  8.92291641e+00]
#    [-7.22469378e+00  1.97034907e+00  2.99531627e+00 -4.34610367e+00
#     -3.67712796e-01 -3.64595604e+00 -7.84149694e+00]
#    [-7.76820278e+00  5.99960327e+00 -8.41420841e+00  2.29520798e+00
#      4.24104357e+00  9.19977474e+00 -4.61686659e+00]]
#
#   [[ 8.27398205e+00  2.88631511e+00 -5.95147419e+00  7.13959980e+00
#     -2.54925585e+00 -6.65015578e-01 -7.24622011e+00]
#    [ 5.36609936e+00 -2.25370550e+00 -1.00714719e+00  6.40559196e+00
#     -3.44093418e+00 -1.10686178e+01 -1.93581736e+00]
#    [ 6.08330536e+00  1.16270723e+01 -6.12922239e+00  1.02644575e+00
#     -8.62347031e+00 -4.47479963e+00  6.06679058e+00]]
#
#   [[-6.74393320e+00 -2.22659349e-01 -3.77906322e-01 -4.90764093e+00
#      2.15560746e+00  6.62665665e-01 -8.02735519e+00]
#    [ 6.33290350e-01  1.03738439e+00  1.54830575e-01 -1.13407755e+00
#      5.91823578e+00  1.70822430e+01 -4.38055634e-01]
#    [-2.77781868e+00 -5.48576641e+00  2.56255603e+00  6.71352196e+00
#      6.03837252e+00  3.98823690e+00  8.05403328e+00]]]
#
#
#  [[[-6.27049685e-01  1.15633488e+01 -6.36027515e-01  5.66315889e+00
#      2.28375840e+00  9.08275545e-01  1.16598463e+00]
#    [ 2.53564405e+00  8.72430134e+00 -1.99402618e+00  8.17446709e+00
#      2.74033046e+00  6.30941105e+00  9.17519188e+00]
#    [-3.74645138e+00 -8.57678294e-01 -2.83845067e+00 -3.27615452e+00
#      2.56140089e+00  5.98653507e+00 -1.09049511e+01]]
#
#   [[ 3.24537659e+00 -3.93446064e+00 -1.52721763e+00  5.86781311e+00
#     -1.97805035e+00  1.28334188e+00 -2.98240376e+00]
#    [-1.21900101e+01  1.25004463e+01  4.92444181e+00 -2.70350194e+00
#      7.81507492e+00 -9.05650043e+00  6.47177219e+00]
#    [-2.17206478e+00  2.71480703e+00 -9.16565228e+00  4.69869089e+00
#     -4.38394928e+00  5.74612093e+00  3.59565282e+00]]
#
#   [[-3.55511594e+00  2.76028395e+00 -3.60184312e+00  7.99856138e+00
#     -4.38458920e+00 -8.98869276e-01 -8.10075521e-01]
#    [ 8.20520210e+00 -2.68970108e+00 -9.29989719e+00 -1.22854757e+00
#     -5.82088470e+00 -5.44573784e+00 -8.23349571e+00]
#    [ 3.67080784e+00  1.18026161e+00 -2.92942858e+00 -7.73343849e+00
#      2.77626085e+00 -4.28008842e+00 -2.14514184e+00]]]
#
#
#  [[[ 2.12814093e+00 -7.68777609e-01 -8.84735203e+00  1.01067667e+01
#      4.76778126e+00  4.78147745e+00 -1.67699254e+00]
#    [ 4.97244644e+00  1.94252181e+00 -2.71501064e+00 -4.14783287e+00
#     -7.91691661e-01  3.18883848e+00 -7.51171255e+00]
#    [-2.92162299e+00 -2.16129565e+00  3.80590534e+00  5.97961855e+00
#      4.99147081e+00 -5.57082367e+00 -1.89473879e+00]]
#
#   [[ 2.64872313e+00 -6.40528393e+00 -1.62670207e+00  2.38367653e+00
#      3.88705778e+00  1.39000535e-01  1.55138087e+00]
#    [ 1.25824089e+01  4.76570749e+00 -7.49335003e+00  6.50031090e+00
#      1.96111858e+00 -4.04969454e+00  7.39542055e+00]
#    [ 1.88336353e+01  3.54755116e+00  7.74576569e+00  2.64947295e+00
#     -3.44345450e+00  7.32074857e-01  1.50061584e+00]]
#
#   [[-3.47981930e+00 -2.59158301e+00 -3.34343934e+00  8.46221507e-01
#      2.33314896e+00  3.76661897e+00 -2.47455430e+00]
#    [ 7.20970154e-01 -5.69754934e+00 -3.22284698e+00 -2.73109198e+00
#     -3.16789818e+00 -2.03302526e+00 -2.95625877e+00]
#    [-5.52691269e+00  4.82660437e+00  1.01005960e+00  2.21910906e+00
#      1.57971847e+00  3.68322039e+00  3.75630736e+00]]]
#
#
#  [[[ 5.41193104e+00 -5.60384941e+00  2.29562593e+00 -9.02732134e-01
#      5.91344452e+00 -2.84974337e+00 -8.05228114e-01]
#    [ 3.65591240e+00 -4.20973110e+00 -2.51142955e+00  3.83922720e+00
#     -4.78865433e+00  1.13573515e+00  4.27588224e+00]
#    [-7.49597371e-01 -8.19201112e-01 -2.69533873e+00 -2.92425513e-01
#     -9.61732864e-01 -1.60479331e+00 -2.03666830e+00]]
#
#   [[-1.37315083e+00 -9.93513107e+00 -9.46694183e+00 -4.27486229e+00
#      1.70914292e-01  4.13093662e+00 -1.33161507e+01]
#    [ 1.42857313e+00 -4.43079710e+00  1.07908468e+01  3.77378225e+00
#      3.78649712e+00 -6.18113041e+00 -4.32539082e+00]
#    [-3.15826833e-01 -3.38590169e+00 -6.04250371e-01 -4.27673054e+00
#     -2.64095235e+00  2.44551945e+00 -7.75257301e+00]]
#
#   [[ 1.18270099e+00 -8.82114220e+00 -1.65353990e+00 -7.77074385e+00
#      5.09965706e+00  1.26115513e+00  5.37919807e+00]
#    [-1.93771791e+00 -3.69820595e+00  9.32795525e+00  5.11645746e+00
#      7.01560497e+00  8.65833187e+00 -5.40101194e+00]
#    [-3.98796463e+00 -2.34203637e-02 -2.60023904e+00  1.02625144e+00
#      4.13492680e+00  1.92184746e+00  4.07373190e+00]]]
#
#
#  [[[-6.84364319e-01 -4.76772118e+00 -5.59659576e+00  2.59804058e+00
#     -4.45792150e+00  2.85404205e+00  4.37403154e+00]
#    [ 9.87957764e+00  4.24151230e+00 -5.65920019e+00  3.64386129e+00
#      6.02909446e-01  1.11303949e+01 -3.01928926e+00]
#    [-7.46744573e-01 -5.60856485e+00  1.99480343e+00 -9.92031753e-01
#      1.71692371e+00 -5.59550583e-01 -1.56595182e+00]]
#
#   [[ 9.75834179e+00  8.77900183e-01 -4.63732815e+00  4.03338528e+00
#      1.47323322e+00  1.23197660e+01  3.10974097e+00]
#    [-4.69103575e-01 -1.42769079e+01  4.73733187e+00  9.42406559e+00
#     -7.56288624e+00 -1.26075115e+01 -2.31584907e+00]
#    [ 6.94194674e-01 -3.53304219e+00  8.35210919e-01 -5.16879797e+00
#      5.44092989e+00 -5.92492199e+00  5.38006544e+00]]
#
#   [[-5.09272289e+00  5.69651365e-01  9.49377251e+00  4.91886139e+00
#     -4.38747263e+00 -1.27646389e+01  5.52901745e+00]
#    [-7.93955231e+00 -4.50405884e+00  1.59659004e+00 -8.24533939e+00
#     -1.65851283e+00 -5.41553068e+00 -8.03626060e+00]
#    [-3.18145752e+00  2.66282582e+00  9.36366320e-01 -1.10870361e+00
#     -4.80783844e+00 -2.30249023e+00 -5.55814934e+00]]]
#
#
#  [[[ 5.35861683e+00  2.81997848e+00 -2.70675421e-01 -1.49736440e+00
#     -2.72707272e+00  9.93852735e-01  2.10917950e+00]
#    [ 9.54258060e+00 -3.62865114e+00 -7.98306227e-01 -1.77635348e+00
#     -4.55513382e+00  6.12996149e+00 -7.45813084e+00]
#    [-3.70084548e+00  2.80985504e-01 -1.28691113e+00  5.09892893e+00
#      2.69848919e+00  4.83566880e-01  4.12019444e+00]]
#
#   [[-3.95883560e-01  1.04449043e+01  5.16901970e+00  5.41939878e+00
#      1.82462847e+00 -8.01707745e+00  2.00431228e+00]
#    [-1.88117683e-01  2.92954755e+00  4.14290667e+00  8.43845749e+00
#     -3.81140685e+00 -3.14104676e+00 -7.85914612e+00]
#    [-8.65340996e+00  1.11263824e+00  5.96140480e+00  1.93812943e+00
#      4.93798304e+00 -1.07395992e+01 -6.05933189e-01]]
#
#   [[ 1.68662715e+00  1.91131592e-01 -5.49581814e+00 -2.99100828e+00
#      3.26321900e-01  2.22558951e+00 -7.21933961e-01]
#    [-3.62809539e+00 -1.14094102e+00  7.86778629e-01 -3.37758040e+00
#      9.88929462e+00  1.51389647e+00 -7.54234600e+00]
#    [ 3.16409206e+00 -5.93904066e+00  5.72136593e+00  2.06540251e+00
#      1.19954090e+01  4.82101727e+00 -2.20609307e+00]]]
#
#
#  [[[ 1.04300761e+00  1.63646793e+00  6.86399341e-01  1.34852600e+00
#     -4.03470707e+00 -2.25256681e+00  3.33122182e+00]
#    [-1.46150219e+00  6.01696110e+00 -8.95973873e+00 -5.05144358e+00
#     -5.28098297e+00  6.84933281e+00 -3.63140082e+00]
#    [ 3.74540508e-01 -7.87882507e-01  3.23215270e+00 -2.20066023e+00
#     -4.33330297e-01  5.46882105e+00 -5.77409935e+00]]
#
#   [[ 6.64719820e-01  1.83429480e+00 -2.47371340e+00 -5.44579744e+00
#     -2.51808548e+00 -3.36716509e+00  7.91948617e-01]
#    [ 1.18216543e+01  7.04637825e-01  3.57614374e+00  3.26182246e-02
#     -7.08487892e+00 -1.53310990e+00  3.27570367e+00]
#    [-1.55577135e+00  2.11997604e+00 -4.86663532e+00  1.29599333e+00
#     -4.90606833e+00 -1.16319454e+00  2.90553045e+00]]
#
#   [[ 6.70130253e+00  3.28602600e+00  5.35130310e+00  1.09373245e+01
#     -1.68153787e+00 -5.31269217e+00  1.27796793e+01]
#    [ 2.71093917e+00  4.41750097e+00 -2.81792474e+00 -5.20114708e+00
#     -4.55729961e+00 -7.18441391e+00 -5.07527828e+00]
#    [ 1.90964675e+00  3.77511740e+00  2.33712316e+00 -7.08522856e-01
#      3.72151232e+00 -1.05427623e-01 -1.03531027e+00]]]
#
#
#  [[[ 2.04985380e+00 -6.48390770e+00  1.95798242e+00  6.57322884e+00
#      4.12863731e-01 -4.32278967e+00  4.89580631e+00]
#    [ 9.95611787e-01 -2.08505154e+00  5.77722263e+00 -4.75273275e+00
#      4.74745226e+00  2.42263794e+00  2.28105402e+00]
#    [ 4.28825855e+00 -8.90439606e+00  1.48928604e+01 -8.79860461e-01
#      7.60016537e+00 -3.02103829e+00  3.51416063e+00]]
#
#   [[-4.18446255e+00 -6.44563854e-01 -1.60029554e+00 -9.40447903e+00
#      2.96056366e+00  4.05623341e+00 -3.08270931e+00]
#    [ 5.90721655e+00  6.26168966e+00 -1.17052495e+00  1.20180407e+01
#     -2.22426128e+00  1.84180009e+00  5.88260651e+00]
#    [ 1.56474435e+00  5.05135679e+00 -3.33587337e+00  2.66880488e+00
#     -4.89722109e+00  7.00591683e-01  7.88860321e-01]]
#
#   [[ 3.55771160e+00 -8.34005165e+00  6.57881498e+00 -5.94304085e+00
#     -1.29626989e-02 -7.68445730e+00 -8.56293917e-01]
#    [-7.78887987e+00  2.45494366e+00  5.37120914e+00 -2.09734797e-01
#     -7.03242493e+00 -2.25485846e-01  3.39174938e+00]
#    [-5.31516409e+00 -4.23801708e+00  2.69230103e+00 -6.37099981e+00
#     -2.49476051e+00  8.04008389e+00 -7.84046292e-01]]]]


init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    print('*' * 20 + ' op1 ' + '*' * 20)
    print(sess.run(op1))
    print('*' * 20 + ' op2 ' + '*' * 20)
    print(sess.run(op2))
    print('*' * 20 + ' op3 ' + '*' * 20)
    print(sess.run(op3))
    print('*' * 20 + ' op4 ' + '*' * 20)
    print(sess.run(op4))
    print('*' * 20 + ' op5 ' + '*' * 20)
    print(sess.run(op5))
    print('*' * 20 + ' op6 ' + '*' * 20)
    print(sess.run(op6))
    print('*' * 20 + ' op7 ' + '*' * 20)
    print(sess.run(op7))
    print('*' * 20 + ' op8 ' + '*' * 20)
    print(sess.run(op8))

		
import tensorflow as tf

x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])

x = tf.reshape(x, [1, 2, 3, 1])  # give a shape accepted by tf.nn.max_pool

valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

with tf.Session() as sess:
    print(valid_pad)
    print(same_pad)
    # Tensor("MaxPool:0", shape=(1, 1, 1, 1), dtype=float32)
    # Tensor("MaxPool_1:0", shape=(1, 1, 2, 1), dtype=float32)

    print(valid_pad.get_shape())
    print(same_pad.get_shape())
    # (1, 1, 1, 1)
    # (1, 1, 2, 1)

    print(sess.run(valid_pad))
    print(sess.run(same_pad))
    # [[[[5.]]]]
    # [[[[5.]
    #    [6.]]]]

max pooling窗口为2×2,两个维度的步长strides=2。第一次由于窗口可以覆盖,橙色区域做max pooling,没什么问题,如下:

                                                                                   

接下来就是SAMEVALID的区别所在:由于步长为2,当向右滑动两步之后,VALID方式发现余下的窗口不到2×2,所以直接将第三列舍弃,而SAME方式并不会把多出的一列丢弃,但是只有一列了不够2×2,即进行填充!

                                                                              

如上图所示,SAME会增加第四列以保证可以达到2×2,但为了不影响原始信息,一般以0来填充。

按原理来说,还有一种填充方式,即FULL填充方式,该方式为卷积后输出特征大小与输入特征大小相同,但是填充方式多种多样,故没有准确的填充方法,了解即可。

 

 

你可能感兴趣的:(TensorFlow,Python,python,tensorflow)