1. 程式人生 > >tensorflow實現1維卷積

tensorflow實現1維卷積

函數 class amp lte .org same and ide result

官方參考文檔: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)

tensorflow實現1維卷積