TensorFlow 逻辑回归

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)
print (trainlabel.shape)
print (testimg.shape)
print (testlabel.shape)
#print (trainimg)
print (trainlabel[0])

(55000, 784)#训练数据集数量和大小
(55000, 10)#训练数据集和标签
(10000, 784)#测试数据集数量和大小
(10000, 10)#测试数据集和标签
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]#分类方式是属于就是1不属于就是0

初始化所有的参数y=w*x+b

x = tf.placeholder("float", [None, 784]) #placeholder先占据一个位置,不给付实际的值制定矩阵第一个是样本数目,第二个是像素个数
y = tf.placeholder("float", [None, 10])  # None is for infinite 第一个是样本,第二个是属于的分类,10分类。
W = tf.Variable(tf.zeros([784, 10]))#w是x 的权重,784个像素需要784个w权重,10 是要得到10分类。
b = tf.Variable(tf.zeros([10]))#10分类只要10个b就行。
# LOGISTIC REGRESSION MODEL
actv = tf.nn.softmax(tf.matmul(x, W) + b) #建立softmax模型,就是把得到的数字转化为概率的矩阵。
# COST FUNCTION
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1)) #损失函数-log(p)*y,如果分对了就留下来,其他的都为0,y是[0,0,0,0,0,0,0,0,1,0,0],reduce_mean求均值。reduction_indices=1


# OPTIMIZER
learning_rate = 0.01
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# 预测
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))   #比较训练的结果和真实的索引是否一样,如果一样返回ture
# 验证
accr = tf.reduce_mean(tf.cast(pred, "float"))#tf.cast把刚对比的类型进行转换为float类型,ture是1,false是0,求均值,均值越大越准确,均值肯定是最大是1
# 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()#TensorFlow张量的秩
#tf.shape(arr).eval()#矩阵的形状
#tf.argmax(arr, 0).eval()#0是按照列中的元素返回最大值的索引,1是按照行进行判断出来最大值的索引
# 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#样本迭代的次数
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.#损失值为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)#一步一步取这个bach
        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})#梯度下降求解,用字典形式带入梯度求解,在TensorFlow中,只要求做最后一步就是梯度下降,他会随着参数上找公式进行求解。
        feeds = {x: batch_xs, y: batch_ys}
        avg_cost += sess.run(cost, feed_dict=feeds)/num_batch#迭代cost值,为了
    # 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")

猜你喜欢

转载自blog.csdn.net/n_sapientia/article/details/81070518