版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23947237/article/details/82968619
TF 笔记:关于 conv1D
有了 2d 卷积的理解,看 1d 应该更容易理解。对一维来讲,卷积就是对一条线一小段一小段地叠加。
conv1d(
value,
filters,
stride,
padding,
use_cudnn_on_gpu=None,
data_format=None,
name=None
)
所以有,
- input 的形状:[batch, in_width, in_channels]
- filter 的形状:[filter_width, in_channels, out_channels]
- stride 的取值:一个整形数字。
- output 的形状:[batch, out_width, out_channels]
假设输入数据为 1 条语句,共 8 个词。每个词对应于 16 维向量。利用的 filter 也一定是对应于 16 维的,filter 数量为 1,如果选择长度为 5 的 filter,则经过卷积输出长度为
。由于只有 1 条输入,则输出的 shape 为
。
在这里,
batch = 1
in_width = 8
in_channels = 16
则根据输入的 shape,有
filter_width = 5
out_width = 4
out_channels = 1
总结
假设 input 为 ,filters 为 ,则 output 为 。
- 实验
import tensorflow as tf
import numpy as np
# 定义向量
input = np.array(np.arange(1, 1+10*8*16).reshape([10, 8, 16]), dtype=np.float32)
print(input.shape)
# 卷积核
kernel = np.array(np.arange(1, 1+5*16*3), dtype=np.float32).reshape([5, 16, 3])
print(kernel.shape)
# 进行conv1d卷积
conv1out = tf.nn.conv1d(input, kernel, 1, 'VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
# 初始化
sess.run(init)
# 输出卷积值
print(sess.run(conv1out).shape)
输出
(10, 8, 16)
(5, 16, 3)
(10, 4, 3)