它确实使用零:
import tensorflow as tf
inputs = tf.ones((1, 10, 1))
想象步长为 2,过滤器为 3(仅由 1 组成)
<tf.Tensor: shape=(1, 10, 1), dtype=float32, numpy=
array([[[1.], |
[1.], | 3
[1.], | |
[1.], | 3
[1.], | |
[1.], | 3
[1.], | |
[1.], | 3
[1.], | |
[1.] | 2
# (will add a zero here)
]], dtype=float32)>
操作中的所有元素都将生成sum(1, 1, 1)。如果最后一个用零填充,结果将是sum(1, 1, 0),它是:
conv = tf.keras.layers.Conv1D(filters=1,
kernel_size=3,
strides=2,
padding='SAME',
kernel_initializer=tf.keras.initializers.Ones)
tf.squeeze(conv(inputs))
<tf.Tensor: shape=(5,), dtype=float32, numpy=array([3., 3., 3., 3., 2.], dtype=float32)>
您也可以使用tf.nn.conv1d 来查看/演示这一点:
import tensorflow as tf
inputs = tf.ones((1, 10, 1))
result = tf.nn.conv1d(input=inputs,
filters=tf.ones((3, 1, 1)),
stride=2,
padding='SAME')
tf.squeeze(result)
<tf.Tensor: shape=(5,), dtype=float32, numpy=array([3., 3., 3., 3., 2.], dtype=float32)>