#用tensorflow在mnist数据集上做逻辑回归(分类)任务
#mnist数据集里有10个手写体,对这10个手写体进行分类任务
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import input_data
#这一步是直接下载数据集
mnist = input_data.read_data_sets('data/', one_hot=True)
trainimg = mnist.train.images#训练的图像
trainlabel = mnist.train.labels#训练的标签
testimg = mnist.test.images#测试的图像
testlabel = mnist.test.labels#测试的标签
print ("MNIST loaded")
print (trainimg.shape)#55000个样本,784是图像大小是28*28的,共784个像素点
print (trainlabel.shape)
print (testimg.shape)
print (testlabel.shape)
#print (trainimg)
print (trainlabel[0])#属于哪个分类,哪个分类是1,第一个样本是7
#placeholder进行占位,X是float型的,有无穷行(None表示无穷),784列
x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10]) # 10分类
W = tf.Variable(tf.zeros([784, 10]))
#对W B进行0初始化,因为有784个像素点,把所有像素点组成一个列向量,
#就需要有一个元素是784的行向量才能与其相乘(即权重W)
b = tf.Variable(tf.zeros([10]))
# 逻辑回归只能做二分类任务,引入softmax做多分类任务
actv = tf.nn.softmax(tf.matmul(x, W) + b) #WX+b,得出属于每一个分类的概率
#损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
# 用梯度下降优化模型,学习率为0.01
learning_rate = 0.01
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# actv是softmax的输出值,argmax函数后面有解释
#对比预测值的索引与真实值的索引是否一样,一样就返回true
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
# cast(pred, "float"),把第一个参数转化成第二个参数的类型
#把true float 转化成0 1类型,然后把所有值加在一起,对的是1,错的是0,就可以求精度了
accr = tf.reduce_mean(tf.cast(pred, "float"))
# INITIALIZER
init = tf.global_variables_initializer()
sess = tf.InteractiveSession()
arr = np.array([[31, 23, 4, 24, 27, 34],
[18, 3, 25, 0, 6, 35],
[28, 14, 33, 22, 20, 8],
[13, 30, 21, 19, 7, 9],
[16, 1, 26, 32, 2, 29],
[17, 12, 5, 11, 10, 15]])
#tf.rank(arr).eval() 指定当前函数是几维的 运行结果是2
#tf.shape(arr).eval() 打印矩阵的行和列,运行结果[6,6]
#tf.argmax(arr, 0).eval() 返回最大值的索引,0表示列,每一列最大结果索引[1,3,2,4,0,1]
# 0 -> 31 (arr[0, 0])
# 3 -> 30 (arr[3, 1])
# 2 -> 33 (arr[2, 2])
tf.argmax(arr, 1).eval()#返回最大值的索引,1表示行,每一行最大结果索引
# 5 -> 34 (arr[0, 5])
# 5 -> 35 (arr[1, 5])
# 2 -> 33 (arr[2, 2])
training_epochs = 50#把所有的样本迭代50次
batch_size = 100#每进行一次迭代选择100个样本
display_step = 5#展示
# run一下初始化操作
sess = tf.Session()
sess.run(init)
# 迭代
for epoch in range(training_epochs):
avg_cost = 0.#初始化损失值为0
num_batch = int(mnist.train.num_examples/batch_size)
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
#遍历每一个batch,返回一个data 和一个label
sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})
#run一下梯度下降求解
feeds = {x: batch_xs, y: batch_ys}
avg_cost += sess.run(cost, feed_dict=feeds)/num_batch#完成一个epoch值的迭代
# DISPLAY
if epoch % display_step == 0:#每5个epoch值打印一下
feeds_train = {x: batch_xs, y: batch_ys}
feeds_test = {x: mnist.test.images, y: mnist.test.labels}
#accr为刚才计算的精度
train_acc = sess.run(accr, feed_dict=feeds_train)
test_acc = sess.run(accr, feed_dict=feeds_test)
print ("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f"
% (epoch, training_epochs, avg_cost, train_acc, test_acc))
print ("DONE")
#运行结果,第二个是损失值,第三个是训练的精度,第四个是测试的精度
02-01
296
10-29
333
01-03
572
07-26
605