下面是根据一个学习视频,自己写的一个一层网络的tensorflow来训练手写数字识别的逻辑回归的训练模型,一些理解见代码的注释,直接上代码,方便自己以后回顾。
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
"""
*****************定义操作******************
"""
# 定义了像素总值,和lable的种类数
x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# sofmax 将而分类回归为10类的模型
actv = tf.nn.softmax(tf.matmul(x, w) + b)
# reduction_indices 0 是按列压 1时 是横向压缩 https://www.cnblogs.com/likethanlove/p/6547405.html
# y =[0, 1, 0, 0, 0, 0, 0, 0, 0, 0] y*tf.log(actv) -> one values
# softmax 求与label的差值为-ylog(actv)
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
# 梯度下降,让cost最小
learn_rate = 0.01
optm = tf.train.GradientDescentOptimizer(learn_rate)
train = optm.minimize(cost)
# tf.arg_max(actv, 1) 取actv(计算的y)最大值 与 label中的值是否相同,相同则转float为1,不同为0,
# 相加求平均则为准确率
pre = tf.equal(tf.arg_max(actv, 1), tf.arg_max(y, 1))
accr = tf.reduce_mean(tf.cast(pre, "float"))
init = tf.global_variables_initializer()
# 读取data值
mnist = input_data.read_data_sets("data/", one_hot=True)
"""
****************
"""
# 迭代次数,即训练所有图片的次数
training_epochs = 50
# 每次处理的图片个数
batch_size = 100
# 每迭代5次 显示训练效果
display_step = 5
with tf.Session() as sess:
# 变量初始化
sess.run(init)
for epoch in range(training_epochs):
# 迭代一次的batch数 , 并读取一次处理的batch的数据batch_xs, batch_ys
num_batch = int(mnist.train.num_examples / batch_size)
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sum_cost = 0
for i in range(num_batch):
feed_train = {x: batch_xs, y: batch_ys}
sess.run(train, feed_dict=feed_train)
sum_cost += sess.run(cost, feed_dict=feed_train)
# 求一个batch训练后的平均损失率
avg_cost = sum_cost / num_batch
# 每隔迭代5次 dispaly,显示训练效果
if epoch % display_step == 0:
# 测试训练数据正确率时,是将当前batch的训练数据来计算正确率
train_acc = sess.run(accr, feed_dict=feed_train)
# 测试正确率时,是将整个测试数据跑完,算正确率
feed_test = {x: mnist.test.images, y: mnist.test.labels}
test_acc = sess.run(accr, feed_dict=feed_test)
print("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f,"
% (epoch, training_epochs, avg_cost, train_acc, test_acc))
print(w.eval())
print(b.eval())