1. 导入库 2. 超参数设置 3. 载入MNIST数据 4. 设置训练配置 5. 搭建神经网络架构 6. 初始化框架 7. 定义损失函数和优化器 8. 训练 9. 测试 10. 可视化
时间: 2023-12-10 19:03:07 浏览: 66
好的,以下是一个基于TensorFlow的简单的MNIST手写数字识别模型的代码实现,包含了您提到的所有步骤:
```
# 导入库
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
# 超参数设置
learning_rate = 0.01
batch_size = 128
num_epochs = 10
# 载入MNIST数据
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# 设置训练配置
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
global_step = tf.Variable(0, trainable=False, name='global_step')
# 搭建神经网络架构
X = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
hidden_1 = tf.layers.dense(inputs=X, units=512, activation=tf.nn.relu)
hidden_2 = tf.layers.dense(inputs=hidden_1, units=256, activation=tf.nn.relu)
logits = tf.layers.dense(inputs=hidden_2, units=10)
# 初始化框架
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# 定义损失函数和优化器
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y))
train_op = optimizer.minimize(loss, global_step=global_step)
# 训练
for epoch in range(num_epochs):
total_loss = 0
num_batches = int(mnist.train.num_examples / batch_size)
for i in range(num_batches):
batch_x, batch_y = mnist.train.next_batch(batch_size)
_, loss_val, step = sess.run([train_op, loss, global_step], feed_dict={X: batch_x, y: batch_y})
total_loss += loss_val
avg_loss = total_loss / num_batches
print("Epoch:", epoch+1, "Loss:", avg_loss)
# 测试
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_accuracy = sess.run(accuracy, feed_dict={X: mnist.test.images, y: mnist.test.labels})
print("Test Accuracy:", test_accuracy)
# 可视化
import matplotlib.pyplot as plt
plt.plot(avg_loss_history)
plt.xlabel('Epochs')
plt.ylabel('Average Loss')
plt.show()
```
这个代码实现了一个三层的神经网络结构,包含两个隐藏层和一个输出层,使用Adam优化器进行模型训练,使用softmax交叉熵损失函数计算模型误差。在训练完成后,计算测试集上的准确率,并绘制训练过程中损失函数变化的曲线图。
阅读全文