Tensorflow 迭代完成逻辑回归模型

首先需要下载一个Tensorflow 内置的数据集mnist

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
print ("packs loaded")
print ("Download and Extract MNIST dataset")
mnist = input_data.read_data_sets('data/',one_hot=True) ##指定编码格式为00 1 编码
print ("type os 'mnist' is %s" % (type(mnist)))
print ("number os train data is %d" % (mnist.train.num_examples))

查看数据的内容

trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel =mnist.test.labels
print (trainimg.shape)  ##784个像素点,
print (trainlabel.shape)  
print (testimg.shape)
print (testlabel.shape)
print (trainlabel[0])
print (trainimg[0])

输出:

(55000, 784)
(55000, 10)
(10000, 784)
(10000, 10)
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.08627451 0.8313726  0.7019608  0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.17254902
 0.7058824  0.9960785  0.4039216  0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.01960784 0.854902   0.9960785  0.41176474
 0.01568628 0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.

构造一个十分类任务:

#逻辑回归判断,10分类
x = tf.placeholder("float",[None,784])   
y = tf.placeholder("float",[None,10]) #None 因为数据的数量未知,用None 表示无穷
W = tf.Variable(tf.zeros([784,10])) #对于784个点,需要784个权重点,10 是输出需要多大,我们是需要看属于这10个里面的哪一类
b = tf.Variable(tf.zeros([10]))     #3只需要初始化10个b,0值初始化,随意都可以。高斯初始化最好
##做一个模型,10分类任务。逻辑回归是2分类任务,做升级,变成多分类任务。softmax 多分类任务
#softmax 输入是我们的得分值
actv = tf.nn.softmax(tf.matmul(x,W)+b)
#模型创建完之后创建损失函数 -logp   再求均值
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv),reduction_indices=1))
learning_rate = 0.01
#梯度下降算法进行优化,传进学习率,最小化cost
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#模型创建好后。我们要进行测试
#argmax(第一个参数是矩阵,第二个值 0 矩阵按列最大值的索引,1 矩阵按行最大值索引),计算得出结果的概率中的最大值的索引
#y 为真实值的索引
#判断预测值和真实值是不是一样,一样True
pred = tf.equal(tf.arg_max(actv,1),tf.argmax(y,1))
#开始求准确率,将True false 转float 类型,true 1,false 0,所有值加在一起求均值,
accr = tf.reduce_mean(tf.cast(pred,"float"))
#完成逻辑回归的算法

完成初始化:

init = tf.global_variables_initializer()
print ("Batch Learnging?")
batch_size = 100  ##一般指定2的N次幂
batch_xs,batch_ys = mnist.train.next_batch(batch_size) #通过next_batch 把样本数据取出来,batch_xs 代表100个样本,ys代表100个样本的标签
print("type of 'batch_xs' is %s" % (type(batch_xs)))
print("type of 'batch_ys' is %s" % (type(batch_ys)))
print("shape of 'batch_xs' is %s" % (type(batch_xs.shape,)))
print("shape of 'batch_ys' is %s" % (type(batch_ys.shape,)))
##开始初始化session 初始化变量
training_epochs = 50 #对所有样本迭代50次
batch_size = 100    #每进行一次要选择多少个样本
display_step = 5  #展示
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):
        tatch_xs,batch_ys = mnist.train.next_batch(batch_size)  ##next_batch 会帮我们找出数据和标签
        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   ##迭代完之后计算损失值
    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: 2.294769995 train_acc: 0.100 test_acc: 0.063
Epoch:005/050 cost: 2.295633320 train_acc: 0.190 test_acc: 0.069
Epoch:010/050 cost: 2.295321467 train_acc: 0.110 test_acc: 0.116
Epoch:015/050 cost: 2.295408871 train_acc: 0.110 test_acc: 0.122
Epoch:020/050 cost: 2.295364665 train_acc: 0.180 test_acc: 0.160
Epoch:025/050 cost: 2.295268768 train_acc: 0.090 test_acc: 0.101
Epoch:030/050 cost: 2.295049348 train_acc: 0.120 test_acc: 0.111
Epoch:035/050 cost: 2.295347588 train_acc: 0.080 test_acc: 0.042
Epoch:040/050 cost: 2.295594408 train_acc: 0.190 test_acc: 0.175
Epoch:045/050 cost: 2.295485464 train_acc: 0.100 test_acc: 0.098
DONE

我是没整明白,我这里为什么会出现问题。还在排查中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值