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 in_channels, output_channels]。
从输入张量中提取图像块,形成一个形状的虚拟张量[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.]]]]

tensorflow-tf.nn.conv2d卷积运算(2)

猜你喜欢

转载自blog.51cto.com/13959448/2336604