TensorFlow-卷積

卷積:ide

conv2d (spa

   input,code

   filter,orm

   strides,blog

   padding,圖片

   use_cudnn_on_gpu=True,ci

   data_format='NHWC',開發

   name=Noneinput

)string

參數名

必選

類型

說明

input

tensor

是一個 4 維的 tensor,即 [ batch, in_height, in_width, in_channels ](若 input 是圖像,[ 訓練時一個 batch 的圖片數量, 圖片高度, 圖片寬度, 圖像通道數 ]

filter

tensor

是一個 4 維的 tensor,即 [ filter_height, filter_width, in_channels, out_channels ](若 input 是圖像,[ 卷積核的高度,卷積核的寬度,圖像通道數,卷積核個數 ],filter in_channels 必須和 input in_channels 相等

strides

列表

長度爲 4 list,卷積時候在 input 上每一維的步長,通常 strides[0] = strides[3] = 1

padding

string

只能爲 " VALID "" SAME " 中之一,這個值決定了不一樣的卷積方式。VALID 丟棄方式;SAME:補全方式

use_cudnn_on_gpu

bool

是否使用 cudnn 加速,默認爲 true

data_format

string

只能是 " NHWC ", " NCHW ",默認 " NHWC "

name

string

運算名稱

 實例代碼:

import tensorflow as tf

a = tf.constant([1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,1,1,0,0],

  dtype=tf.float32,shape=[1,5,5,1])

b = tf.constant([1,0,1,0,1,0,1,0,1],

  dtype=tf.float32,shape=[3,3,1,1])

c = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1], padding='VALID')

d = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1], padding='SAME')

with tf.Session() as sess:

    print ("c shape:")

    print (c.shape)

    print ("c value:")

    print (sess.run(c))

    print ("d shape:")

    print (d.shape)

    print ("d value:")

print (sess.run(d))

不一樣padding參數的不通運行方式與結果:

實驗來源於 騰訊雲 - 開發者實驗室 中TensorFlow API的相關實驗;

有意思的是,上述實驗結果與給出的參考結果徹底不一樣,有感興趣的好同志不妨試試看究竟是誰出錯了。

相關文章
相關標籤/搜索