cnn进行手写数字识别

-- coding: utf-8 --

“”"
@Time : 19-10-8 下午4:12
@Author : lei
@Site :
@File : CNN3_mnist.py
@Software: PyCharm
“”"

CNN实现mnist识别

import tensorflow as tf
from tensorflow.contrib.layers import fully_connected
from tensorflow.examples.tutorials.mnist import input_data

hidden1_num = 800
hidden2_num = 100
output_num = 10
learning_rate = 0.001
batchs = 20000

输入[None, 784]

定义占位符,输入数据

with tf.name_scope(“data”):
x = tf.placeholder(tf.float32, [None, 784])
y_true = tf.placeholder(tf.float32, [None, 10])
# 将图片从一维转换为4为 个数 宽 高 通道
x_image = tf.reshape(x, [-1, 28, 28, 1])

定义第一个卷积层

with tf.name_scope(“conv1”):
# 定义卷积核
weight1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))
bias1 = tf.Variable(tf.truncated_normal([32], stddev=0.1))
# 进行卷积层 并将结果使用relu函数进行激活
conv1 = tf.nn.relu(tf.nn.conv2d(x_image, weight1, strides=[1, 1, 1, 1], padding=“SAME”) + bias1)
# 定义池化层 28 * 28 – > 14*14
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=“SAME”)

定义第二个卷积核

with tf.name_scope(“conv2”):
# 定义64个卷积核 因为上一层卷积层产生了32个卷积核 所以开始是32 而在这一层定义的卷积核的个数需要不小于上一层的特征图
weight2 = tf.Variable(tf.truncated_normal([3, 3, 32, 64], stddev=0.1))
bias2 = tf.Variable(tf.truncated_normal([64], stddev=0.1))
conv2 = tf.nn.relu(tf.nn.conv2d(pool1, weight2, strides=[1, 1, 1, 1], padding=“SAME”) + bias2)
# 14 * 14 --> 7 * 7 * 64
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=“SAME”)

定义全连接层 隐藏层 [None, 7764] [800, 100] [100, 10]

with tf.name_scope(“hidden”):
h_pool = tf.reshape(pool2, [-1, 7 * 7 * 64])
hidden1 = fully_connected(h_pool, hidden1_num, scope=“hidden1”)
# 定义随机失活
keep_prob = tf.placeholder(tf.float32)
hidden1_dp = tf.nn.dropout(hidden1, keep_prob)
hidden2 = fully_connected(hidden1_dp, hidden2_num, scope=“hidden2”)
hidden2_dp = tf.nn.dropout(hidden2, keep_prob)
# 输出层不使用relu 而使用softmax作为激活函数
y_predict = fully_connected(hidden2_dp, output_num, activation_fn=None, scope=“output”)

求平均损失,开始进行梯度下降

with tf.name_scope(“loss”):
x_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict)
loss = tf.reduce_mean(x_entropy)

with tf.name_scope(“train_step”):
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

进行评估

with tf.name_scope(“acc”):
equal = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
accuarcy = tf.reduce_mean(tf.cast(equal, tf.float32))

mnist = input_data.read_data_sets("./mldata/", one_hot=True)
init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)

for batch in range(batchs):
    x_batch, y_batch = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: x_batch, y_true: y_batch, keep_prob: 0.5})
    # print(batch)
    # if batch / 10 == 0:
    train_acc = accuarcy.eval({x: x_batch, y_true: y_batch, keep_prob: 0.5})
    test_acc = accuarcy.eval({x: mnist.test.images, y_true: mnist.test.labels, keep_prob: 1.0})
    print("第{}轮训练集准确率{},测试集准确率{}".format(batch+1, train_acc, test_acc))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值