欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

5.1tensorflow5.1神经网络调参实现mnist数据集分类正确率98%以上(实现动态学习率调整)

程序员文章站 2023-12-30 19:38:16
...

整理不易,有条件的点个关注、点个赞呗!感恩各位大哥!

tensorflow简单介绍

tensor采用图运算的方式搭建并训练深度学习网络,该部分使用的库包版本为tensorflow==1.14.0

代码拆分(每个代码块可以放到一个jupyter的cell里)

导入tensorflow及相关包

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

加载数据、定义批次

# 载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot = True)

# 每个批次的大小
batch_size = 100
# 计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

定义网络结构


# 定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32)
# 定义初始学习率变量,用于动态控制学习率
lr = tf.Variable(0.001,dtype=tf.float32)

# 创建一个简单的神经网络
W1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
b1 = tf.Variable(tf.zeros([500])+0.1)
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)
L1_drop = tf.nn.dropout(L1,keep_prob)

W2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
b2 = tf.Variable(tf.zeros([300])+0.1)
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob)


W3 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
b3 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L2_drop,W3)+b3)

定义损失函数、优化器、评价指标等


## 二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
# 交叉熵损失函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)) 
# 使用梯度下降法
train_step = tf.train.AdamOptimizer(lr).minimize(loss)

# 初始化变量
init = tf.global_variables_initializer()

# 结果放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) # argmax返回一维张量中最大的值所在的位置
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

模型训练


with tf.Session() as sess:
    sess.run(init)
    
    for epoch in range(51):
    	# 根据epoch调整学习率
        sess.run(tf.assign(lr,0.001 * (0.95 ** epoch)))
        for batch in range(n_batch):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1})
        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.})
        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.})
        print("Iter" + str(epoch) + ",Testing Accuracy" + str(test_acc),"Training Accuracy" + str(train_acc))

输出:

ouput:
Iter38,Testing Accuracy0.981 Training Accuracy0.99765456
Iter39,Testing Accuracy0.9806 Training Accuracy0.99767274
Iter40,Testing Accuracy0.9808 Training Accuracy0.9976909
Iter41,Testing Accuracy0.9811 Training Accuracy0.9977091
Iter42,Testing Accuracy0.9809 Training Accuracy0.99774545
Iter43,Testing Accuracy0.9812 Training Accuracy0.99776363
Iter44,Testing Accuracy0.9811 Training Accuracy0.99776363
Iter45,Testing Accuracy0.9811 Training Accuracy0.99776363
Iter46,Testing Accuracy0.9818 Training Accuracy0.99776363
Iter47,Testing Accuracy0.9818 Training Accuracy0.99776363
Iter48,Testing Accuracy0.9815 Training Accuracy0.99776363
Iter49,Testing Accuracy0.9814 Training Accuracy0.99776363
Iter50,Testing Accuracy0.9815 Training Accuracy0.9977818

完整代码

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot = True)

# 每个批次的大小
batch_size = 100
# 计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

# 定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32)
lr = tf.Variable(0.001,dtype=tf.float32)

# 创建一个简单的神经网络
W1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
b1 = tf.Variable(tf.zeros([500])+0.1)
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)
L1_drop = tf.nn.dropout(L1,keep_prob)

W2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
b2 = tf.Variable(tf.zeros([300])+0.1)
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob)


W3 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
b3 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L2_drop,W3)+b3)

# 二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)) # 交叉熵
# 使用梯度下降法
train_step = tf.train.AdamOptimizer(lr).minimize(loss)

# 初始化变量
init = tf.global_variables_initializer()

# 结果放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) # argmax返回一维张量中最大的值所在的位置
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    sess.run(init)
    
    for epoch in range(51):
        sess.run(tf.assign(lr,0.001 * (0.95 ** epoch)))
        for batch in range(n_batch):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1})
        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.})
        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.})
        print("Iter" + str(epoch) + ",Testing Accuracy" + str(test_acc),"Training Accuracy" + str(train_acc))

上一篇:

下一篇: