Tensorflow使用卷积神经网络操作对MNIST数据集进行预测

# 对MNIST数据集做卷积操作

#-*- codeing = utf-8 -*-
#@Time :2021/5/17 21:10
#@Author :Onion
#@File :ConMnistDemo.py
#@Software :PyCharm

# 对MNIST数据集做卷积操作

import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/data/", one_hot=True)
tf.compat.v1.disable_eager_execution()
tf.compat.v1.disable_v2_behavior()
tf.compat.v1.disable_eager_execution()
tf.compat.v1.reset_default_graph()

# 封装定义权重变量的函数(对于权重,统一使用函数truncated_normal来生成标准差为0.1的随机数为其初始胡)
def weight_variable(shape):
    initial = tf.compat.v1.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
# 封装定义偏置变量的函数
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

# 封装卷积操作
def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')

# 封装最大池化操作
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
# 封装平均池化操作
def avg_pool_7x7(x):
    return tf.nn.avg_pool(x, ksize=[1,7,7,1], strides=[1,7,7,1], padding='SAME')
# 定义参数
learning_rate = 0.001
training_epochs = 3000
batch_size = 50
display_step = 1

# 定义网络结构
n_input = 784
n_labels = 10

# 输入数据x和y,y是图片的标签数据
x = tf.compat.v1.placeholder("float", [None, n_input])
y = tf.compat.v1.placeholder("float", [None, n_labels])

# 定义权重和偏置参数(在卷积神经网络中,滤波器或者说卷积核就是神经元的权重参数)
# ①定义权重
weight = {
    'W_conv1': weight_variable([5, 5, 1, 32]),
    'W_conv2': weight_variable([5, 5, 32, 64]),
    'W_conv3': weight_variable([5, 5, 64, 10])
}
# ②定义偏置
bias = {
    'b_conv1': bias_variable([32]),
    'b_conv2': bias_variable([64]),
    'b_conv3': bias_variable([10])
}


# 定义卷积层和池化层(向前传播)
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x,[-1,28,28,1])

h_conv1 = tf.nn.relu(conv2d(x_image,weight['W_conv1'])+bias['b_conv1'])
h_pool1 = max_pool_2x2(h_conv1)

h_conv2 = tf.nn.relu(conv2d(h_pool1,weight['W_conv2'])+bias['b_conv2'])
h_pool2 = max_pool_2x2(h_conv2)

h_conv3 = tf.nn.relu(conv2d(h_pool2,weight['W_conv3'])+bias['b_conv3'])
h_pool3 = avg_pool_7x7(h_conv3)

# 对最后的池化输出结果进行sotfmax映射
h_pool3_flat = tf.reshape(h_pool3,[-1,10])
y_output = tf.nn.softmax(h_pool3_flat)

# 定义损失函数和优化器(交叉熵损失函数),Adam优化器
loss = -tf.reduce_sum(y*tf.compat.v1.log(y_output))
train_step = tf.compat.v1.train.AdamOptimizer(1e-4).minimize(loss)
# 计算平均错误率
correct_prediction = tf.equal(tf.argmax(y_output,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

# 运行卷积操作
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    sess.run(init)
    # 总共训练3000次
    for i in range(training_epochs):
        batch = mnist.train.next_batch(batch_size=batch_size)  # 50
        # 每训练20次打印一次准确率
        if i % 20 == 0:
            train_accuracy = accuracy.eval(feed_dict={
                x: batch[0], y: batch[1]})
            print("step %d, training accuracy %g" % (i, train_accuracy))
        train_step.run(feed_dict={x: batch[0], y: batch[1]})

    # 使用测试集对模型的准确率进行测试
    print("test accuracy %g" % accuracy.eval(feed_dict={
        x: mnist.test.images, y: mnist.test.labels}))

结果:训练了3000次之后,使用测试集测试的结果准确率为0.9215
在这里插入图片描述
下面对模型使用多卷积和操作和退化学习率来进行优化

#-*- codeing = utf-8 -*-
#@Time :2021/5/17 21:10
#@Author :Onion
#@File :ConMnistDemo.py
#@Software :PyCharm

# 对MNIST数据集做卷积操作

import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/data/", one_hot=True)
tf.compat.v1.disable_eager_execution()
tf.compat.v1.disable_v2_behavior()
tf.compat.v1.disable_eager_execution()
tf.compat.v1.reset_default_graph()

# 封装定义权重变量的函数(对于权重,统一使用函数truncated_normal来生成标准差为0.1的随机数为其初始胡)
def weight_variable(shape):
    initial = tf.compat.v1.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
# 封装定义偏置变量的函数
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

# 封装卷积操作
def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')

# 封装最大池化操作
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
# 封装平均池化操作
def avg_pool_7x7(x):
    return tf.nn.avg_pool(x, ksize=[1,7,7,1], strides=[1,7,7,1], padding='SAME')
# 定义参数
learning_rate = 0.001
# 当前训练的步数
global_step = tf.compat.v1.Variable(0,trainable=False)
# 退化学习率
decaylearning_rate = tf.compat.v1.train.exponential_decay(learning_rate,global_step,1000,0.9)
training_epochs = 3000
batch_size = 50
display_step = 1


# 定义网络结构
n_input = 784
n_labels = 10

# 输入数据x和y,y是图片的标签数据
x = tf.compat.v1.placeholder("float", [None, n_input])
y = tf.compat.v1.placeholder("float", [None, n_labels])

# 定义权重和偏置参数(在卷积神经网络中,滤波器或者说卷积核就是神经元的权重参数)
# ①定义权重,使用多卷积核(将5x5卷积核扩展为5x5和7x7双重卷积核)
weight = {
    'W_conv1': weight_variable([5, 5, 1, 32]),
    # 'W_conv2': weight_variable([5, 5, 32, 64]),
    'W_conv2_5x5':weight_variable([5,5,32,32]),
    'W_conv2_7x7':weight_variable([7,7,32,32]),
    'W_conv3': weight_variable([5, 5, 64, 10])
}
# ②定义偏置
bias = {
    'b_conv1': bias_variable([32]),
    # 'b_conv2': bias_variable([64]),
    'b_conv2_5x5':bias_variable([32]),
    'b_conv2_7x7':bias_variable([32]),
    'b_conv3': bias_variable([10])
}


# 定义卷积层和池化层(向前传播)
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x,[-1,28,28,1])

# 卷积操作和池化操作
h_conv1 = tf.nn.relu(conv2d(x_image,weight['W_conv1'])+bias['b_conv1'])
h_pool1 = max_pool_2x2(h_conv1)

h_conv2_5x5 = tf.nn.relu(conv2d(h_pool1,weight['W_conv2_5x5'])+bias['b_conv2_5x5'])
h_pool2_5x5 = max_pool_2x2(h_conv2_5x5)
h_conv2_7x7 = tf.nn.relu(conv2d(h_pool1,weight['W_conv2_7x7'])+bias['b_conv2_7x7'])
h_conv2 = tf.concat([h_conv2_5x5,h_conv2_7x7],3)
h_pool2 = max_pool_2x2(h_conv2)

h_conv3 = tf.nn.relu(conv2d(h_pool2,weight['W_conv3'])+bias['b_conv3'])
h_pool3 = avg_pool_7x7(h_conv3)

# 对最后的池化输出结果进行sotfmax映射
h_pool3_flat = tf.reshape(h_pool3,[-1,10])
y_output = tf.nn.softmax(h_pool3_flat)

# 定义损失函数和优化器(交叉熵损失函数),Adam优化器
loss = -tf.reduce_sum(y*tf.compat.v1.log(y_output))
# 将global_step传入,完成每一轮训练自动+1的操作
train_step = tf.compat.v1.train.AdamOptimizer(decaylearning_rate).minimize(loss,global_step=global_step)
# 计算平均错误率
correct_prediction = tf.equal(tf.argmax(y_output,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

# 运行卷积操作
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    sess.run(init)
    # 总共训练3000次
    for i in range(training_epochs):
        batch = mnist.train.next_batch(batch_size=batch_size)  # 50
        # 每训练20次打印一次准确率
        if i % 20 == 0:
            train_accuracy = accuracy.eval(feed_dict={
                x: batch[0], y: batch[1]})
            print("step %d, training accuracy %g" % (i, train_accuracy))
        train_step.run(feed_dict={x: batch[0], y: batch[1]})

    # 使用测试集对模型的准确率进行测试
    print("test accuracy %g" % accuracy.eval(feed_dict={
        x: mnist.test.images, y: mnist.test.labels}))

可以看到模型的准确率上升了
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的洋葱头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值