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)

转载于:https://www.cnblogs.com/HITSZ/p/8710979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值