conv2d
二维卷积函数
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
input的形状:[batch, in_height ,in_width, in_channels]
[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
filter的形状:[filter_height, filter_width, in_channels, out_channels]
[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,filter的通道数要求与input的in_channels一致,有一个地方需要注意,第三维in_channels,就是参数input的第四维
strides:[1,stride_h,stride_w,1]
步长,即卷积核每次移动的步长,这是一个一维的向量,长度4,strides[0]=strides[3]=1
padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式
use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true 结果返回一个Tensor,这个输出,就是我们常说的feature map
import tensorflow as tf input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,1])) op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID') with tf.Session() as sess: sess.run(tf.initialize_all_variables()) res = (sess.run(op)) print (res.shape)
filter的参数个数为3*3*5*1,也即对于输入的每个通道数都对应于一个3*3的滤波器,然后共5个通道数,conv2d的过程就是对5个输入进行点积然后求和,得到一张feature map
最大池化函数
tf.nn.max_pool(value, ksize, strides, padding, name=None)
value: [batch, height, width, channels]需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map
ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
strides: 和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride, stride, 1]
padding: 和卷积类似,可以取'VALID' 或者'SAME'
返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式
import tensorflow as tf a=tf.constant([ [[1.0,2.0,3.0,4.0], [5.0,6.0,7.0,8.0], [8.0,7.0,6.0,5.0], [4.0,3.0,2.0,1.0]], [[4.0,3.0,2.0,1.0], [8.0,7.0,6.0,5.0], [1.0,2.0,3.0,4.0], [5.0,6.0,7.0,8.0]] ]) a=tf.reshape(a,[1,4,4,2]) pooling=tf.nn.max_pool(a,[1,2,2,1],[1,1,1,1],padding='VALID') with tf.Session() as sess: print("image:") image=sess.run(a) print (image) print("reslut:") result=sess.run(pooling) print (result)
平均池化函数

浙公网安备 33010602011771号