tensorflow学习基础篇1——conv2d 函数说明

来源:互联网 发布:淘宝商城运营客服 编辑:程序博客网 时间:2024/05/19 05:02

每次学习新知识总是走马观花,理解不深刻,从现在开始坚持将学习到的东西记录下来,一来方便以后回顾,二来加深理解

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

tensorflow ——conv2d

tf.nn.conv2d(input, w, strides, padding)

其中 input 为输入,格式为[batch, height, width, channels], 分别为【输入的批次数量、图像的高(行数)、宽(列数)、通道(彩色为3,灰色为1)】

w 为卷积矩阵,二维、分别为[高,宽】

strides 为滑动窗口尺寸,分别为[1, height, width, 1], 通常 strides[0]=strdes[3]=1,因为一般不会在一个个图像,一个个通道之间滑动

padding 为扩展方式,有两种 vaild 和 same

1)不同的padding方式,VALID是采用丢弃的方式,比如上述的input_width=13,只允许滑动2次,多余的元素全部丢掉

2)SAME的方式,采用的是补全的方式,对于上述的情况,允许滑动3次,但是需要补3个元素,左奇右偶,在左边补一个0,右边补2个0

3) For the SAME padding, the output height and width are computed as:

  • out_height = ceil(float(in_height) / float(strides[1]))

    out_width = ceil(float(in_width) / float(strides[2]))

  • For the VALID padding, the output height and width are computed as:

    out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))

    out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))


tensorflow 中源码如下所示,D:\python3.5\Lib\site-packages\tensorflow\python\ops\gen_nn_ops.py

def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,           data_format=None, name=None):  r"""Computes a 2-D convolution given 4-D `input` and `filter` tensors.  Given an input tensor of shape `[batch, in_height, in_width, in_channels]`  and a filter / kernel tensor of shape  `[filter_height, filter_width, in_channels, out_channels]`, this op  performs the following:  1. Flattens the filter to a 2-D matrix with shape     `[filter_height * filter_width * in_channels, output_channels]`.  2. Extracts image patches from the input tensor to form a *virtual*     tensor of shape `[batch, out_height, out_width,     filter_height * filter_width * in_channels]`.  3. For each patch, right-multiplies the filter matrix and the image patch     vector.  In detail, with the default NHWC format,      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]  Must have `strides[0] = strides[3] = 1`.  For the most common case of the same  horizontal and vertices strides, `strides = [1, stride, stride, 1]`.  Args:    input: A `Tensor`. Must be one of the following types: `half`, `float32`, `float64`.    filter: A `Tensor`. Must have the same type as `input`.    strides: A list of `ints`.      1-D of length 4.  The stride of the sliding window for each dimension      of `input`. Must be in the same order as the dimension specified with format.    padding: A `string` from: `"SAME", "VALID"`.      The type of padding algorithm to use.    use_cudnn_on_gpu: An optional `bool`. Defaults to `True`.    data_format: An optional `string` from: `"NHWC", "NCHW"`. Defaults to `"NHWC"`.      Specify the data format of the input and output data. With the      default format "NHWC", the data is stored in the order of:          [batch, in_height, in_width, in_channels].      Alternatively, the format could be "NCHW", the data storage order of:          [batch, in_channels, in_height, in_width].    name: A name for the operation (optional).  Returns:    A `Tensor`. Has the same type as `input`.  """  result = _op_def_lib.apply_op("Conv2D", input=input, filter=filter,                                strides=strides, padding=padding,                                use_cudnn_on_gpu=use_cudnn_on_gpu,                                data_format=data_format, name=name)  return result
参考网页:http://stackoverflow.com/questions/34642595/tensorflow-strides-argument
0 0