tensorflow2.3 通过卷积神经网络_mnist数据手写数字识别功能

  1 #定义初始化和偏置
  2 def weight_varibale(shape):
  3     # 随机初始化权重和偏置,权重和偏置后面会跟着训练自动优化
  4     #weight = tf.Variable(tf.compat.v1.random_normal([784, 10], mean=0.0, stddev=1.0), name='weight')
  5     weight = tf.Variable(tf.compat.v1.random_normal(shape=shape, mean=0.0, stddev=1.0), name='weight')
  6     return weight
  7 def bias_varible(shape):
  8     #bias = tf.Variable(tf.constant([0], shape=[10]))
  9     bias = tf.Variable(tf.constant([0.0], shape=shape))
 10     return bias
 11     # 预测Nonew个样本的输出结果matrix [None,784]*[784*10]+[10]=[None,10],即矩阵[None,784]样本的特征*权重[784,10]+偏置[10]=预测结果[None,10]
 12 
 13 
 14 #卷积模型
 15 def model():
 16     '''
 17     自定义总卷积模型
 18     :return:
 19     '''
 20     # 1、建立数据的占位符 ,X[None,784] y_true [None,10]
 21     with tf.compat.v1.variable_scope('data'):
 22         x = tf.compat.v1.placeholder(tf.float32, [None, 784])
 23         y_true = tf.compat.v1.placeholder(tf.int32, [None, 10])
 24     #2、卷积层一,卷积、激活、池化
 25     # 初始化权重和偏重--由于这三个层都要用到权重和偏重,唯一的区别的形状不同,故我们设计一下函数专门用于初始化权重和偏置
 26     with tf.compat.v1.variable_scope('conv1'):
 27         #因为卷积接收的input格式为[batch,heigth,width,channel],即要对其进进行形状的改变。这里784开平方等于28,故单个样本从二维转换为四维为[None,784]--[None,28,28,1]
 28         #假设卷积神经有32个filter,5*5,stribes=1,padding='SAME', 则输入[None,28,28,32]--输出为[None,28,28,32]
 29         #假设池化2*2,shribes2,padding='SAME',则[None,28,28,32]-->[None,14,14,32]
 30         w_conv1=weight_varibale([5,5,1,32])  #随机初始化权重,输入32个filter,5*5,shrides步长=1,32个样本。。。即权重的形状
 31         b_conv1=bias_varible([32])
 32         x_reshape=tf.reshape(x,[-1,28,28,1])#这里的负一,相当于None
 33 
 34         #卷积从[none,28,28,1]转成了[None,28,28,32]再加上偏置
 35         conv1=tf.compat.v1.nn.conv2d(x_reshape,w_conv1,strides=[1,1,1,1],padding='SAME')+b_conv1
 36         #激活
 37         x_relu=tf.compat.v1.nn.relu(conv1)
 38         #池化层 2*2,strides 2 [none,28,28,32]->[None,14,14,32]
 39         x_pool1=tf.compat.v1.nn.max_pool(x_relu,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
 40 
 41     with tf.compat.v1.variable_scope('conv2'):
 42         #3、卷积层二  假设卷积神经有64个filter,5*5*32,stribes=1,padding='SAME', 则输入[None,14,14,32]--输出为[None,14,14,64]
 43         #假设池化2 * 2,shribes2, padding = 'SAME', 则[None,14,14,64]-->[None, 7, 7, 64]
 44         w_conv2 = weight_varibale([5, 5, 32, 64])  # 随机初始化权重,输入64个filter,5*5*32,shrides步长=1,32个样本。。。即权重的形状..这里的32代表输入的通道数,64代表输出通道数
 45         b_conv2 = bias_varible([64])
 46         # 卷积从[none,14,14,32]转成了[None,14,14,64]再加上偏置
 47         conv2 = tf.compat.v1.nn.conv2d(x_pool1, w_conv2, strides=[1, 1, 1, 1], padding='SAME') + b_conv2
 48         # 激活
 49         x_relu2 = tf.compat.v1.nn.relu(conv2)
 50         # 池化层 2*2,strides 2 [None,14,14,64]->[None,7,7,64]
 51         x_pool2 = tf.compat.v1.nn.max_pool(x_relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 52 
 53     #4、全连接层[None,7,7,64],变成二维数组[none,[7*7*64]*偏置[[7*7*64,10]+[10]=[None,10]
 54     with tf.compat.v1.variable_scope('conv_full'):
 55         w_fc=weight_varibale([7*7*64,10])
 56         b_fc=bias_varible([10])
 57         #修改形状态[None,7,7,64]--》[none,7*7*64]
 58         x_fc_reshape=tf.reshape(x_pool2,[-1,7*7*64])
 59         #进行矩阵运算得出每个样本的10个结果
 60         y_predict=tf.matmul(x_fc_reshape,w_fc)+b_fc
 61     return x,y_true, y_predict
 62 def con_fc():
 63     # 使用占位符时,tersorflow2.X以上会出现tf.placeholder() is not compatible with eager execution报错,需要加下面这段语,避免程序报此错误。
 64     tf.compat.v1.disable_eager_execution()
 65     #获取真实的数据
 66     mnist = input_data.read_data_sets("./tmp/mnist/", one_hot=True)
 67 
 68     #定义模型,得出输出--即卷积输出
 69     x,y_true, y_predict=model()
 70     #进行交叉熵损失计算
 71     with tf.compat.v1.variable_scope('soft_cross'):
 72         # 返回交叉熵的列表结果,对交叉熵求平均值
 73         loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
 74 
 75     # 梯度下降求出损失
 76     with tf.compat.v1.variable_scope('optimizer'):
 77         train_op = tf.compat.v1.train.GradientDescentOptimizer(0.0001).minimize(loss)
 78     # 5、计算准确率,预测准确置为1
 79     with tf.compat.v1.variable_scope('acc'):
 80         # equal_list None个样本[1,0,1,1,.....]
 81         equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
 82         accuray = tf.reduce_mean(tf.cast(equal_list, tf.float32))
 83 
 84 
 85     # 因为有变量,故要定义初始化变量的op
 86     init_op = tf.compat.v1.global_variables_initializer()
 87     # 开启回话去训练
 88     with tf.compat.v1.Session() as sess:
 89         # 初始化变量
 90         sess.run(init_op)
 91         for i in range(1000):
 92             mnist_x, mnist_y = mnist.train.next_batch(50)
 93             # feed_dict实时提供的数据 x训练集,y为真实的目标值
 94             # 运行op训练
 95             #train_op 一个张量
 96             #feed_dict
 97             sess.run(train_op, feed_dict={x: mnist_x, y_true: mnist_y})
 98             print('训练第%d步,准确率为:%f' % (i, sess.run(accuray, feed_dict={x: mnist_x, y_true: mnist_y})))
 99     return None
100 
101 
102 if __name__ == '__main__':
103     con_fc()

 

posted @ 2020-11-14 17:05  hisweetyGirl  阅读(303)  评论(0编辑  收藏  举报