tf.truncated_normal(shape, stddev=0.1) tf.constant(0.1, shape=shape) tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')tf.truncated_normal(shape, mean, stddev) :shape表示生成张量的维度,mean是均值,stddev是标准差。这个函数产生正太分布,均值和标准差自己设定。
initial = tf.truncated_normal ( shape, stddev=0.1 ) # 正态分布,标准差为 0.1,默认最大为 1,最小为 -1,均值为 0
initial = tf.constant ( 0.1, shape=shape ) # 创建一个结构为 shape 矩阵也可以说是数组 shape 声明其行列,初始化所有值为 0.1
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
- input: A Tensor. type必须是以下几种类型之一: half, float32, float64.
- filter: A Tensor. type和input必须相同
- strides: A list of ints.一维,长度4, 在input上切片采样时,每个方向上的滑窗步长,必须和format指定的维度同阶
- padding: A string from: “SAME”, “VALID”. padding 算法的类型
- use_cudnn_on_gpu: An optional bool. Defaults to True.
- data_format: An optional string from: “NHWC”, “NCHW”, 默认为”NHWC”。
指定输入输出数据格式,默认格式为”NHWC”, 数据按这样的顺序存储:[batch, in_height, in_width, in_channels]
也可以用这种方式:”NCHW”, 数据按这样的顺序存储:[batch, in_channels, in_height, in_width]
- name: 操作名,可选.
tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') # 卷积遍历各方向步数为 1,SAME:边缘外自动补 0,遍历相乘
tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
# 池化卷积结果(conv2d)池化层采用kernel大小为 2*2,步数也为2,周围补0,取最大值。数据量缩小了4倍
tf.nn.max_pool(value, ksize, strides, padding, name=None)
参数是四个,和卷积很类似:
第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape
第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在
batch和
channels
上做池化,所以这两个维度设为了1
第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,
stride
, 1]
第四个参数padding:和卷积类似,可以取'VALID' 或者'SAME'
返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]
这种形式
x_image = tf.reshape(x, [-1,28,28,1])