tensorflow-tf.nn.conv2d卷積運算(2)
tf.nn.conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=True,
data_format='NHWC',
dilations=[1, 1, 1, 1],
name=None
)
計算給定4d輸入和過濾核張量的二維卷積。
給定形狀[batch,in_height, in_width, in_channels]的輸入張量和形狀[filter_height, filter_width, in_channels, out_channels]的篩選/核張量,此op執行如下操作:
將過濾核壓扁到一個二維矩陣,形狀為[filter_height filter_width
從輸入張量中提取影象塊,形成一個形狀的虛擬張量[batch, out_height, out_width, filter_height filter_width in_channels]。
對於每個patch,右乘濾波器矩陣和影象patch向量。
output[b, i, j, k] =
sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *
filter[di, dj, q, k]
必須有strides(步長)[0]=strides[3]= 1。對於相同水平和頂點的最常見情況,stride = [1, stride, stride, 1]。
Args:
input:一個張量。必須是以下型別之一:half,bfloat16, float32, float64。一個四維張量。dimension順序是根據data_format的值來解釋的。
filter: 必須具有與輸入相同的型別。形狀的4維張量[filter_height, filter_width, in_channels, out_channels]。
strides: int型列表。長度為4的一維張量。每個輸入維度的滑動視窗的跨步。順序是根據data_format的值來解釋的。
padding: 來自:“SAME”、“VALID”的字串。要使用的填充演算法的型別。
use_cudnn_on_gpu: 可選bool,預設為True.
data_format: 一個可選的字串:“NHWC”、“NCHW”。預設為“NHWC”。指定輸入和輸出資料的資料格式。使用預設格式“NHWC”,資料按以下順序儲存:[批處理、高度、寬度、通道]。或者,格式可以是“NCHW”,資料儲存順序為:[批處理,通道,高度,寬度]。
dilations:int的可選列表。預設為[1,1,1,1]。長度為4的一維張量。每個輸入維度的膨脹係數。如果設定為k > 1,則該維度上的每個過濾器元素之間將有k-1跳過單元格。維度順序由data_format的值決定,詳細資訊請參閱上面的內容。批次的膨脹和深度尺寸必須為1。
name: 可選 名字
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 2 13:23:27 2018
@author: myhaspl
@email:[email protected]
tf.nn.conv2d
"""
import tensorflow as tf
g=tf.Graph()
with g.as_default():
x=tf.constant([[
[[1.,2.],[3.,4.],[5.,6.]],
[[10.,20.],[30.,40.],[50.,60.]],
]])
kernel=tf.constant([[[[10.],[2.]]]])
y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="SAME")
with tf.Session(graph=g) as sess:
print sess.run(x)
print sess.run(kernel)
print kernel.get_shape()
print x.get_shape()
print sess.run(y)
[[[[ 1. 2.]
[ 3. 4.]
[ 5. 6.]]
[[10. 20.]
[30. 40.]
[50. 60.]]]]
[[[[10.]
[ 2.]]]]
(1, 1, 2, 1)
(1, 2, 3, 2)
[[[[ 14.]
[ 38.]
[ 62.]]
[[140.]
[380.]
[620.]]]]