【TensorFlow】tf.nn.conv2d是怎樣實現卷積的?
文章出處:http://blog.csdn.net/mao_xiao_feng/article/details/53444333
tf.nn.conv2d是TensorFlow裡面實現卷積的函式,參考文件對它的介紹並不是很詳細,實際上這是搭建卷積神經網路比較核心的一個方法,非常重要
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
除去name引數用以指定該操作的name,與方法有關的一共五個引數:
第一個引數input:指需要做卷積的輸入影象,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是
[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 影象通道數],注意
這是一個4維的Tensor,要求型別為float32和float64其中之一
第二個引數filter:相當於CNN中的卷積核,
它要求是一個Tensor,具有
[filter_height, filter_width, in_channels, out_channels]這樣的shape
,具體含義是[卷積核的高度,
],要求型別與引數input相同,有一個地方需要注意,第三維卷積核的寬度,影象通道數,卷積核個數
,就是引數input的第四維in_channels
第三個引數strides:卷積時在影象每一維的步長,這是一個一維的向量,長度4
第四個引數padding:string型別的量,只能是"SAME","VALID"其中之一,這個值決定了不同的卷積方式(後面會介紹)
第五個引數:use_cudnn_on_gpu:bool型別,是否使用cudnn加速,預設為true
結果返回一個Tensor,這個輸出,就是我們常說的feature map
那麼TensorFlow的卷積具體是怎樣實現的呢,用一些例子去解釋它:
1.考慮一種最簡單的情況,現在有一張3×3單通道的影象(對應的shape:[1,3,3,1]),用一個1×1的卷積核(
)去做卷積,最後會得到一張3×3的feature map對應的shape:[1,1,1,1]
2.增加圖片的通道數,使用
一張3×3五通道的影象
(對應的shape:[1,3,3,5]),
用一個1×1的卷積核(
)去做卷積,仍然是一張3×3對應的shape:[1,1,1,1]
的feature map,這就相當於每一個畫素點,卷積核都與該畫素點的每一個通道做點積
- input = tf.Variable(tf.random_normal([1,3,3,5]))
- filter = tf.Variable(tf.random_normal([1,1,5,1]))
- op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
- input = tf.Variable(tf.random_normal([1,3,3,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,1]))
- op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
- input = tf.Variable(tf.random_normal([1,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,1]))
- op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
- .....
- .xxx.
- .xxx.
- .xxx.
- .....
5.上面我們一直令引數padding的值為‘VALID’,當其為‘SAME’時,表示卷積核可以停留在影象邊緣,如下,輸出5×5的feature map
- input = tf.Variable(tf.random_normal([1,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,1]))
- op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
- xxxxx
- xxxxx
- xxxxx
- xxxxx
- xxxxx
- input = tf.Variable(tf.random_normal([1,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,7]))
- op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
7.步長不為1的情況,文件裡說了對於圖片,因為只有兩維,通常strides取[1,stride,stride,1]
- input = tf.Variable(tf.random_normal([1,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,7]))
- op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
- x.x.x
- .....
- x.x.x
- .....
- x.x.x
- input = tf.Variable(tf.random_normal([10,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,7]))
- op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
最後,把程式總結一下:
- import tensorflow as tf
- #case 2
- input = tf.Variable(tf.random_normal([1,3,3,5]))
- filter = tf.Variable(tf.random_normal([1,1,5,1]))
- op2 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
- #case 3
- input = tf.Variable(tf.random_normal([1,3,3,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,1]))
- op3 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
- #case 4
- input = tf.Variable(tf.random_normal([1,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,1]))
- op4 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
- #case 5
- input = tf.Variable(tf.random_normal([1,5,5,5]))
- filter = tf.Variable(tf.random_normal([3,3,5,1]))
- op5 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
- #case 6
-
input = tf.Variable(tf.random_normal([1,5,5,5
相關推薦
【TensorFlow】tf.nn.conv2d是怎樣實現卷積的?
文章出處:http://blog.csdn.net/mao_xiao_feng/article/details/53444333 tf.nn.conv2d是TensorFlow裡面實現卷積的函式,參考文件對它的介紹並不是很詳細,實際上這是搭建卷積神經網路比較
【TensorFlow】tf.nn.conv2d是怎樣實現卷積的?有1*1(1×1)卷積介紹
除去name引數用以指定該操作的name,與方法有關的一共五個引數: 第一個引數input:指需要做卷積的輸入影象,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片
【TensorFlow】tf.nn.max_pool實現池化操作
版權宣告:本文為博主原創文章,轉載請註明出處。 https://blog.csdn.net/mao_xiao_feng/article/details/53453926 max pooling是CNN當中的最大值池化操作,其實用法和卷積很類似 有些地方可以從卷積去參考
【TensorFlow】tf.nn.conv2d_transpose是怎樣實現反捲積的?
三個月沒更新了啊,回來更一發~~ csdn上主要講一些coding過程中遇到的函式,問題,解決方案。偏實踐 另外,如果你想看一些理論方面的東西,歡迎加我的知乎知乎主頁 csdn私信幾乎不看,有問題交流可以發郵箱:[email protected]或者知乎私
【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
white 交叉 none padding tomat ros true const cross 在計算loss的時候,最常見的一句話就是 tf.nn.softmax_cross_entropy_with_logits ,那麽它到底是怎麽做的呢? 首先明確一點,loss是代
【Tensorflow】tf.nn.separable_conv2d
【Tensorflow】tf.nn.separable_conv2d實現深度可分卷積 出處:https://blog.csdn.net/mao_xiao_feng/article/details/78002811 實驗環境:tensorflow版本1.2.0,python2
【Tensorflow】tf.nn.dropout函式
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 此函式是為了防止在訓練中過擬合的操作,將訓練輸出按一定規則進行變
個人總結:關於tf.nn.conv2d(卷積)與tf.nn.conv2d_transpose(反捲積)的區別
官網中對於卷積tf.nn.conv2d的描述 tf.nn.conv2d( input, filter, strides, padding, use_cudn
Tensorflow】tf.nn.atrous_conv2d如何實現空洞卷積?
value: 指需要做卷積的輸入影象,要求是一個4維Tensor,具有[batch, height, width, channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 影象通道數] filters:
tensorflow隨筆-tf.nn.conv2d卷積運算(2)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myh
tensorflow隨筆-tf.nn.conv2d卷積運算(3)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myha
tensorflow隨筆-tf.nn.conv2d卷積運算(5)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myha
tensorflow隨筆-tf.nn.conv2d卷積運算(1)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author: myha
tensorflow隨筆-tf.nn.conv2d卷積運算(10)
運動模糊 #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Oct 2 13:23:27 2018 @author:
Tensorflow中tf.nn.conv2d的實現原理
先解釋一下tf.nn.conv2d函式:tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None):給定一個input的張量[batch,
【tensorFlow】tf.reshape()報錯信息 - TypeError: Expected binary or unicode string
bject port cas inpu dimen div nts sof expec 今天在使用tensoflow跑cifar10的CNN分類時候,download一個源碼,但是報錯 TypeError: Failed to convert object of type
【tensorflow】tf.identity()
常與tf.control_dependencies(self, control_inputs)配合使用,只有當這個上下文管理器中的是一個操作時,control_inputs才會執行。 x = tf.Variable(0.0) x_plus_1 = tf.assign_add(x, 1)
【tensorflow】tf.get_variable()和tf.Variable()的區別
1.tf.Variable() tf.Variable(initial_value, trainable=True, collections=None, validate_shape=True, name=None) ##initial_value為變數的初始值 tf.get
【Tensorflow】tf.import_graph_def
tf.import_graph_def tf.import_graph_def( graph_def, input_map=None, return_elements=None, name=None, op_dict=None,
tensorflow隨筆-tf.nn.conv2d
基於騰訊雲開發者實驗室 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