tf.conv1d 和 tf.conv2d 的區別

tf.conv1d是實現一維卷積,tf.conv2d是實現二維卷積。

當tf.conv2d的輸入第二個或第三個維度爲1時就等同於一維卷積了,詳細見以下代碼。

import tensorflow as tf
import numpy as np

input = tf.constant([[[1], [7], [3], [2], [5], [6], [1]], [[11], [17], [13], [12], [15], [16], [11]]], dtype=tf.float32)

kernel = tf.constant([[1, 1, 1], [2, 2, 2]], dtype=tf.float32)

#  Use tf.nn.conv1d
input_con1 = tf.reshape(input, [2, 7, 1])
kernel1 = tf.reshape(kernel, [3, 1, 2])
con1 = tf.nn.conv1d(input_con1, kernel1, 2, 'VALID')

# Use tf.nn.conv2d
input_con2 = tf.reshape(input, [2, 1, 7, 1])
kernel2 = tf.reshape(kernel, [1, 3, 1, 2])
con2 = tf.nn.conv2d(input_con2, kernel2, [1, 1, 2, 1], 'VALID')

with tf.Session() as sess:
    print('Out put of conv1d\n', sess.run(con1))
    print('Out put of conv2d\n', sess.run(con2))

輸出結果:

Out put of conv1d
 [[[14. 21.]
  [15. 17.]
  [13. 19.]]

 [[54. 71.]
  [55. 67.]
  [53. 69.]]]
Out put of conv2d
 [[[[14. 21.]
   [15. 17.]
   [13. 19.]]]


 [[[54. 71.]
   [55. 67.]
   [53. 69.]]]]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章