逻辑回归
导入模块并且加载数据
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")
#Extracting data/train-images-idx3-ubyte.gz
#Extracting data/train-labels-idx1-ubyte.gz
#Extracting data/t10k-images-idx3-ubyte.gz
#Extracting data/t10k-labels-idx1-ubyte.gz
#MNIST loaded
观察下数据结构
print (trainimg.shape)
print (trainlabel.shape)
print (testimg.shape)
print (testlabel.shape)
#print (trainimg)
#打印出来这个值说明他是7,每一个1代表一个标签
print (trainlabel[0])
#(55000, 784)
#(55000, 10)
#(10000, 784)
#(10000, 10)
#[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
先设置x,y占位,定义w,b,构造损失函数
x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10]) # None is for infinite
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# LOGISTIC REGRESSION MODEL
#进行[x,x,x,x,x,x,x,x,x,x]把这个数进行归一化计算属于哪个值得概率最大
actv = tf.nn.softmax(tf.matmul(x, W) + b)
# COST FUNCTION
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
# OPTIMIZER
learning_rate = 0.01
#梯度下降进行损失优化
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
进行预测然后类型转移
#argmax就是找到索引最大值返回位置,0代表列,1代表行
#equal,对应位置进行比较如果相等返回True,不相等False
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
#转成浮点类型
accr = tf.reduce_mean(tf.cast(pred, "float"))
#变量初始化
init = tf.global_variables_initializer()
最后一步进行训练开始
training_epochs = 50
batch_size = 100
display_step = 5
# SESSION
sess = tf.Session()
sess.run(init)
# MINI-BATCH LEARNING
for epoch in range(training_epochs):
avg_cost = 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)
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")
#Epoch: 000/050 cost: 1.177906594 train_acc: 0.840 test_acc: 0.855
#Epoch: 005/050 cost: 0.440515266 train_acc: 0.860 test_acc: 0.895
#Epoch: 010/050 cost: 0.382895913 train_acc: 0.910 test_acc: 0.905
#Epoch: 015/050 cost: 0.356607343 train_acc: 0.870 test_acc: 0.909
#Epoch: 020/050 cost: 0.341326642 train_acc: 0.860 test_acc: 0.912
#Epoch: 025/050 cost: 0.330556413 train_acc: 0.910 test_acc: 0.913
#Epoch: 030/050 cost: 0.321508561 train_acc: 0.840 test_acc: 0.916
#Epoch: 035/050 cost: 0.314936944 train_acc: 0.940 test_acc: 0.917
#Epoch: 040/050 cost: 0.309805418 train_acc: 0.940 test_acc: 0.918
#Epoch: 045/050 cost: 0.305343132 train_acc: 0.960 test_acc: 0.918
#DONE