本文共 2331 字,大约阅读时间需要 7 分钟。
使用逻辑回归模型进行MNIST数字识别是一个经典的机器学习问题。本文将详细介绍模型的构建过程、训练方法以及结果分析。
首先,我们需要加载MNIST数据集,并进行数据预处理。代码如下:
import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datadef logistic_regression(): # 加载数据集 mnist = input_data.read_data_sets(r'C:\Users\Administrator\Desktop\AI_project\tensorflow\MNIST_data', one_hot=True) # 定义批次大小 batch_size = 128 # 定义输入占位符 x = tf.placeholder(tf.float32, [batch_size, 784], name="x_data") y = tf.placeholder(tf.int32, [batch_size, 10], name="y_data") # 初始化权重和偏置 w = tf.Variable(tf.random_normal([784, 10], stddev=0.1)) b = tf.Variable(tf.zeros([10])) # 计算预测结果 val = tf.add(tf.matmul(x, w), b) # 定义损失函数 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=val)) # 定义优化器并训练模型 optimizer = tf.train.AdamOptimizer() train_op = optimizer.minimize(loss) # 预测准确率 pred = tf.nn.softmax(val) acc = tf.reduce_mean(tf.cast(tf.argmax(pred, axis=1) == tf.argmax(y, axis=1), tf.float32)) # 初始化变量 init_op = tf.global_variables_initializer() # 训练和测试过程 with tf.Session() as sess: sess.run(init_op) # 训练过程 n_batch = math.ceil(mnist.train.num_examples / batch_size) for i in range(50): loss_total = 0 for _ in range(n_batch): x_input, y_input = mnist.train.next_batch(batch_size) _, loss_val = sess.run([train_op, loss], feed_dict={x: x_input, y: y_input}) loss_total += loss_val avg_loss = loss_total / n_batch print(f"Interation {i}, Loss: {avg_loss}") # 测试过程 acc_total = 0 for _ in range(n_batch): x_input, y_input = mnist.test.next_batch(batch_size) acc = sess.run(acc, feed_dict={x: x_input, y: y_input}) acc_total += acc avg_acc = acc_total / n_batch print(f"Accuracy: {avg_acc}") 训练过程中,损失函数值逐渐下降,准确率逐渐上升,显示出模型在训练过程中的有效性。随着训练次数的增加,模型性能得到显著提升。
通过以上优化,模型在MNIST数据集上的表现更加稳定和可靠。建议在实际应用中根据具体情况调整批次大小和优化器参数,以获得最佳效果。
转载地址:http://wphfk.baihongyu.com/