tf.nn.conv2d别名tf.compat.v1.nn.conv2d
1. 函数功能
根据给的的4维输入张量和4维filter张量计算2维卷积。
若input是默认的NHWC格式,即[batch, in_height, in_width, in_channels]的输入张量input,形状为[filter_height, filter_width, in_channels, out_channels]的滤波器filter(内核张量),此操作执行以下操作:
step1. 将filter展平为[filter_height * filter_width * in_channels, output_channels]的二维矩阵.
step2 .从input中提取图像像素块,以形成[batch, out_height, out_width, filter_height * filter_width * in_channels]的虚拟张量.
step3. 对于每个像素块右乘filter矩阵。
延伸
二维卷积 1
给定一个图像𝑿 ∈ ℝ𝑀×𝑁 和一个滤波器𝑾 ∈ ℝ𝑈×𝑉 ,一般𝑈 << 𝑀, 𝑉 << 𝑁,其卷积为
输入信息𝑿 和滤波器𝑾 的二维卷积定义为:
2.入参
tf.nn.conv2d(
input,
filter=None,
strides=None,
padding=None,
use_cudnn_on_gpu=True,
data_format='NHWC',
dilations=[1, 1, 1, 1],
name=None,
filters=None
)
参数 | 含义 |
---|---|
input | 4D张量,数值类型必须是half, bfloat16, float32, float64之一。 张量的秩至少是4。该张量的维度由data_format指定。 |
filters | 滤波器,4维张量[filter_height, filter_width, in_channels, out_channels],数值类型与intput输入类型一样。相当于CNN中的卷积核,各维度[filter_height, filter_width, in_channels, out_channels]卷积核的含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]。需要注意:in_channel 是图像通道数 ,和 input 的 in_channel 要保持一致 |
strides | 卷积时filter(卷积核)在图像每一维的步长,int整型或者含有1、2或4个元素的int列表。 |
padding | 字符串类型表示采用 “SAME” 或 “VALID” 填充 算法, 也可以用一个二维张量指示如何填充,例如当data_format="NHWC"时[[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]],当data_format=“NCHW” 时 [[0, 0], [0, 0], [pad_top, pad_bottom], [pad_left, pad_right]]. |
data_format | 数据格式,指示输入维度的顺序,可以是 “NHWC"或 “NCHW”,默认是"NHWC”。"NHWC"格式[batch_shape, height, width, channels], "NCHW"格式是[batch_shape, channels, height, width]。 |
dilations | 膨胀因子,可以是一个整数或者是长度为1,2或4的int列表。默认是整数1。 |
name | 操作名字(可选) |
use_cudnn_on_gpu | bool类型,是否使用cudnn加速,默认为true |
3.返回值
返回值是Tensor,数值类型与输入保持一致。
4.可能引发的异常
ValueError,如data_format无效。
神经网络与深度学习-邱锡鹏 ↩︎