import tensorflow as tf
x = tf.constant([[1., 2., 3.,4., 5., 6.,7.,8.,9.],
[1., 2., 3.,4., 5., 6.,7.,8.,9.],
[1., 2., 3.,4., 5., 6.,7.,8.,9.],])
x = tf.reshape(x, [1, 3, 9, 1]) # give a shape accepted by tf.nn.max_pool
valid_pad = tf.nn.max_pool(x, [1, 3, 3, 1], [1, 1, 1, 1], padding='VALID')
same_pad = tf.nn.max_pool(x, [1, 3, 3, 1], [1, 1, 1, 1], padding='SAME')
average_pad = tf.nn.avg_pool(x, [1, 3, 3, 1], [1, 1, 1, 1], padding='SAME')
print(valid_pad.get_shape())
print(same_pad.get_shape())
输出结果为:
(1, 1, 7, 1)
(1, 3, 9, 1)
从结果可以看出,若 padding=‘VALID’,则采用以下计算公式、
n
e
w
w
i
d
t
h
=
[
o
l
d
w
i
d
t
h
–
f
i
l
t
e
r
w
i
d
t
h
+
1
s
t
e
p
]
newwidth=[\frac{oldwidth–filterwidth+1}{step}]
newwidth=[stepoldwidth–filterwidth+1]
若 padding=‘SAME’,则采用以下计算公式、
n
e
w
w
i
d
t
h
=
[
o
l
d
w
i
d
t
h
s
t
e
p
]
newwidth=[\frac{oldwidth}{step}]
newwidth=[stepoldwidth]
下面看一下在padding='SAME’时,是怎样对输入x怎样填充的。
with tf.Session() as sess:
print(sess.run(average_pad))
输出结果为:
[[[[ 1.5] [ 2. ] [ 3. ] [ 4. ] [ 5. ] [ 6. ] [ 7. ] [ 8. ] [ 8.5]]
[[ 1.5] [ 2. ] [ 3. ] [ 4. ] [ 5. ] [ 6. ] [ 7. ] [ 8. ] [ 8.5]]
[[ 1.5] [ 2. ] [ 3. ] [ 4. ] [ 5. ] [ 6. ] [ 7. ] [ 8. ] [ 8.5]]]]
1.5
=
1
+
1
+
2
+
2
4
1.5=\frac{1+1+2+2}{4}
1.5=41+1+2+2
8.5
=
8
+
8
+
9
+
9
4
8.5=\frac{8+8+9+9}{4}
8.5=48+8+9+9
分母并不包含填充边界,只是本步池化所包含原始数据的个数