使用Tensorflow构造LogisticRegression
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
#加载mnist数据集,one_hot=True为0,1格式
mnist = input_data.read_data_sets('f:/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张图片,大小为28*28
print(trainlabel.shape) # 训练标签集的大小,一共有0-9这10中结果
print(testimg.shape) # 测试图片的大小
print(testlabel.shape) # 测试标签的大小
# print (trainimg)
print(trainlabel[0]) #第一个标签代表的数据为7
输出结果为
#使用placeholder可以预先设计出参数的大小,而不去考虑其具体的数值
x = tf.placeholder("float", [None, 784]) #在tf中,None代表无穷
y = tf.placeholder("float", [None, 10]) #x表示图片数据集,y表示结果集
W = tf.Variable(tf.zeros([784, 10])) #W、b为回归参数
b = tf.Variable(tf.zeros([10]))
#逻辑回归模型
actv = tf.nn.softmax(tf.matmul(x, W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
# 梯度下降求解最优模型
learning_rate = 0.01
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# 判断预测值和真实值是否相同
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
# 计算精度,回归建模完成
accr = tf.reduce_mean(tf.cast(pred, "float"))
# 初始化tf
init = tf.global_variables_initializer()
# argmax的用法
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() # 矩阵维数
tf.shape(arr).eval() #矩阵形状
tf.argmax(arr, 0).eval() #矩阵每列的最大值索引
# 0 -> 31 (arr[0, 0])
# 3 -> 30 (arr[3, 1])
# 2 -> 33 (arr[2, 2])
tf.argmax(arr, 1).eval() #矩阵每行的最大值索引
# 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 # 每迭代5次进行一次展示
# 初始化Session
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) # 生成批处理X,Y数据集
sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) # 计算损失值
feeds = {x: batch_xs, y: batch_ys} # 存放在批处理字典里
avg_cost += sess.run(cost, feed_dict=feeds) / num_batch # 每一批的损失值累加
# DISPLAY
if epoch % display_step == 0:
feeds_train = {x: batch_xs, y: batch_ys}
feeds_test = {x: mnist.test.images, y: mnist.test.labels}
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")
经过50次迭代后,损失函数,训练集精度,测试集精度如下