tensorflow实现1维卷积
官方参考文档:https://www.tensorflow.org/api_docs/python/tf/nn/conv1d
参考网页:
http://www.riptutorial.com/tensorflow/example/19385/basic-example
http://www.riptutorial.com/tensorflow/example/30750/math-behind-1d-convolution-with-advanced-examples-in-tf
tensorflow从版本r0.11起,开始支持1维卷积操作:tf.nn.conv1d.
input的维度设置:[batch_size, 每个input的元素个数, input的通道数]
卷积核(filter)的维度设置: [卷积核长度, input的通道数,输出通道数]
最简单的1维卷积法:使用tf的conv1d函数,设置padding=0,stride=1,举例说明:
input = [1, 0, 2, 3, 0, 1, 1]
and kernel = [2, 1, 3]
the result of the convolution is [8, 11, 7, 9, 4],
which is calculated in the following way:
- 8 = 1 * 2 + 0 * 1 + 2 * 3
- 11 = 0 * 2 + 2 * 1 + 3 * 3
- 7 = 2 * 2 + 3 * 1 + 0 * 3
- 9 = 3 * 2 + 0 * 1 + 1 * 3
- 4 = 0 * 2 + 1 * 1 + 1 * 3
tensorflow支持2种扩展模式(padding),第一种是'VALID',第二种是'SAME‘
'VALID'方式表示不进行扩展; ‘SAME‘表示添加0;比如,对input进行‘SAME‘的扩展时,得到的是:input = [0, 1, 0, 2, 3, 0, 1, 1, 0], 卷积后的output= [1, 8, 11, 7, 9, 4, 3]
设置卷积核每一次滑动2次:
res = tf.squeeze(tf.nn.conv1d(data, kernel, 2, 'SAME'))
with tf.Session() as sess:
print sess.run(res)