0%

tf 中的’SAME’和’VALID’填充有什么区别?

如题所示。

参考资料

python – 张量流的tf.nn.max_pool中的’SAME’和’VALID’填充有什么区别?

正解

直接看代码

图象是 2 * 3

1
2
3
4
5
6
7
8
9
10
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')

valid_pad.get_shape() == [1, 1, 1, 1] # valid_pad is [5.]
same_pad.get_shape() == [1, 1, 2, 1] # same_pad is [5., 6.]

图象是 3 * 2

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

x = tf.reshape(x, [1, 3, 2, 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')

valid_pad.get_shape() == [1, 1, 1, 1] # valid_pad is [5.]
same_pad.get_shape() == [1, 2, 1, 1] # same_pad is [5., 6.]

图象是 3 * 3

1
2
3
4
5
6
7
8
9
10
11
12
import tensorflow as tf

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

x = tf.reshape(x, [1, 3, 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')
print(valid_pad.get_shape()) # (1,1,1,1)
print(same_pad.get_shape()) # (1,2,2,1)
请我喝杯咖啡吧~