CNN 卷积神经网络 代码实现
首先导入数据
import tensorflow as tf
python from tensorflow.examples.tutorials.mnist import input_data
#导入tensorflow里的mnist数据集
mnist=input_data.read_data_sets('MNIST_data',one_hot=true)
下面呢。
1.定义Weight变量,输入shape,返回变量的参数。其中我们使用tf.truncted_normal产生随机变量来进行初始化:定义此的作用是下一步建立conv的定义初始值,W_conv1=weight_variable([5,5,1,32])
代码为:
def weight_variable(shape):
inital=tf.truncted_normal(shape,stddev=0.1)
return tf.Variable(initial)
2.定义biase变量,输入shape ,返回变量的一些参数。其中我们使用tf.constant常量函数来进行初始化:
def bias_variable(shape):
initial=tf.constant(0.1,shape=shape)
return tf.Variable(initial)
3.定义卷积,tf.nn.conv2d函数是tensoflow里面的二维的卷积函数,x是图片的所有参数,W是此卷积层的权重,然后定义步长stri