CNN 卷积神经网络 1
定义卷积层的 weight bias
mnist=input_data.read_data_sets('MNIST_data',one_hot=true)
输入shape,返回变量的参数。
tf.truncted_normal产生随机变量来进行初始化:
def weight_variable(shape):
inital=tf.truncted_normal(shape,stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial=tf.constant(0.1,shape=shape)
return tf.Variable(initial)
定义卷积
tf.nn.conv2d函数是tensoflow里面的二维的卷积函数,x是图片的所有参数,W是此卷积层的权重
步长strides的值:
strides[0]和strides[3]的两个1是默认值,中间两个1代表padding时在x方向运动一步,y方向运动一步
padding采用的方式是SAME
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
定义 pooling
采用池化pooling来稀疏化参数
一种是最大值池化,一种是平均值池化
池化的核函数大小为2x2,因此ksize=[1,2,2,1]:
def max_poo_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1])
Last updated
Was this helpful?