tf.nn.conv2d
计算给定的2-d卷积input和4- d filters张量。
tf.nn.conv2d(
input, filters, strides, padding, data_format='NHWC', dilations=None, name=None
)
参数 | 讲解 |
---|---|
input | 一个Tensor 。一定是以下类型之一: half , bfloat16 , float32 , float64 。至少为4的尺寸顺序是根据的值解释秩的张量data_format ;与所有 - 丁内3个维度作为批次的尺寸。详情请参阅下文。 |
filters | 一个Tensor 。必须具有相同类型input 。形状的4-d张量[filter_height, filter_width, in_channels, out_channels] |
strides | int或列表ints ,其具有长度1 , 2或4 。对于每个维度的滑动窗口的步幅input 。如果一个单一的值被赋予它在复制H和W尺寸。默认情况下, N和C尺寸被设置为1的尺寸顺序由的值确定data_format ,请参阅下面的详细资料。 |
padding | 任一string “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 | 一个可选的string :从"NHWC", “NCHW"默认为"NHWC” 。指定输入和输出数据的数据格式。与缺省格式“NHWC”时,数据被存储在的顺序: batch_shape + [height, width, channels] 。可替代地,该格式可以是“NCHW”的数据存储顺序为: batch_shape + [channels, height, width] 。 |
dilations | int或列表ints ,其具有长度1 , 2或4 ,缺省值为1的扩张因子的每一维input 。如果一个单一的值被赋予它在复制H和W尺寸。默认情况下, N和C尺寸被设定为1。如果设定为k> 1,将有k-1个该维度每个过滤器元件之间跳过细胞。的尺寸顺序由的值确定data_format ,详见上文。在间歇和深度尺寸胀缩如果一个4 d张量必须为1。 |
name | 一种操作(可选)的名称。 |
tf.nn.conv2d_transpose
转置conv2d
tf.nn.conv2d_transpose(
input, filters, output_shape, strides, padding='SAME', data_format='NHWC',
dilations=None, name=None
)
调用实例
import numpy as np
import tensorflow as tf
x_in = np.array([[
[[2], [1], [2], [0], [1]],
[[1], [3], [2], [2], [3]],
[[1], [1], [3], [3], [0]],
[[2], [2], [0], [1], [1]],
[[0], [0], [3], [1], [2]], ]])
kernel_in = np.array([
[ [[2, 0.1]], [[3, 0.2]] ],
[ [[0, 0.3]],[[1, 0.4]] ], ])
x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)
conv = tf.nn.conv2d(x, kernel, strides=[1, 1, 1, 1], padding='VALID')
init_op=tf.global_variables_initializer()
with tf.Session() as sess:
init_op.run()
print(conv)
print(sess.run(conv))
#结果输出
'''
Tensor("Conv2D_2:0", shape=(1, 4, 4, 2), dtype=float32)
[[[[10. 1.9 ]
[10. 2.2 ]
[ 6. 1.6 ]
[ 6. 2. ]]
[[12. 1.4 ]
[15. 2.2 ]
[13. 2.7 ]
[13. 1.7 ]]
[[ 7. 1.7 ]
[11. 1.3000001]
[16. 1.3 ]
[ 7. 1. ]]
[[10. 0.6 ]
[ 7. 1.4000001]
[ 4. 1.5 ]
[ 7. 1.4 ]]]]
'''
其他卷积函数:
tf.nn.conv1d(
input, filters, stride, padding, data_format='NWC', dilations=None, name=None
)
tf.nn.conv1d_transpose(
input, filters, output_shape, strides, padding='SAME', data_format='NWC',
dilations=None, name=None
)
tf.nn.conv3d(
input, filters, strides, padding, data_format='NDHWC', dilations=None, name=None
)
tf.nn.conv3d_transpose(
input, filters, output_shape, strides, padding='SAME', data_format='NDHWC',
dilations=None, name=None
)