1. 程式人生 > >徹底搞懂softmax、邏輯迴歸

徹底搞懂softmax、邏輯迴歸

1 使用Tensorflow對minist進行分類,使用最簡單的softmax,下面是程式碼:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from datetime import datetime
import math
import time
#載入資料集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

# 配置每個 GPU 上佔用的記憶體的比例 有GPU時候使用
gpu_options = tf.GPUOptions
(per_process_gpu_memory_fraction=0.95) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) x = tf.placeholder(tf.float32,[None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x,W)+b) y_ = tf.placeholder(tf.float32,[None, 10]) cross_entropy = tf.reduce
_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) init = tf.initialize_all_variables() sess = tf.Session() #無GPU時候開啟 sess.run(init) for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run
(train_step, feed_dict = {x: batch_xs, y_: batch_ys}) correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_: mnist.test.labels}))

最終輸出:0.92左右
2 模型中訓練出的W和b到底是多少呢,可以通過列印看看:

seeb = sess.run(b) 
seew = sess.run(W) 
print(seeb)
print(seew[50])
print(seeb.shape)
print(seew.shape)

可以看到,訓練出的W和b已經被取出來了,我們想用想儲存都可以
3 驗證模型輸出:

print(mnist.test.images.shape)
print(mnist.test.labels.shape)

輸出:這樣可以明白了test的結構
(10000, 784)
(10000, 10)
我們取出第一個,並預測:

testx = [mnist.test.images[0]]
#print(testx)
testy = [mnist.test.labels[0]]
print(testy)
sess.run(y, feed_dict={x:testx})

輸出:
[array([ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.])]
Out[46]:
array([[ 3.53539799e-05, 4.24364188e-09, 1.03236089e-04,
1.41154591e-03, 9.89227829e-07, 2.13419171e-05,
1.82815807e-08, 9.98052120e-01, 2.00856193e-05,
3.55230470e-04]], dtype=float32)
明顯,預測為7,預測正確
4 自己寫程式碼驗證整個模型:

from numpy import *;
import numpy as np; #這個方式使用numpy的函式時,需要以np.開頭。
import math

testx = [mnist.test.images[0]]
a1=mat(testx);
a2=mat(seew);
a3=a1*a2;
a4 = a3+seeb
etab=[0,0,0,0,0,0,0,0,0,0]
for i in range(0,10):
    t = a4[0:,i]
    t = t.tolist()[0]
    t = t[0]
    etab[i] = math.exp(t)
a = sum(etab)
for i in range(0,10):
    t = etab[i]
    t = t/a
    print(t)

最終輸出結果:
3.53539897083e-05
4.24364244027e-09
0.000103236158687
0.00141154597564
9.89226982999e-07
2.13419156007e-05
1.82816162943e-08
0.998052194092
2.00856209105e-05
0.000355230495374
和模型預測結果一致!
5 sklearn庫的邏輯迴歸模型

from sklearn.linear_model import LinearRegression  
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV

Flag = mnist.train.labels.argmax(axis=1)

LR = LogisticRegressionCV()
LR.fit(mnist.train.images,Flag)
#預測
t = LR.predict(mnist.test.images)
print(t)
err = 0
Flag = mnist.test.labels.argmax(axis=1)
for i in range(0,t.shape[0]):
    if t[i] != Flag[i]:
        err = err+1
err = err/t.shape[0]
print(1-err)

最後輸出:0.9197
取出訓練的引數,套用上面的驗證模型驗證:

w2 = LR.coef_.T
b2 = LR.intercept_
#print(w2.shape)
#print(b2.shape)

from numpy import *;
import numpy as np; 
import math

testx = [mnist.test.images[0]]
a1=mat(testx);
a2=mat(w2);
a3=a1*a2;
a4 = a3+b2
etab=[0,0,0,0,0,0,0,0,0,0]
for i in range(0,10):
    t = a4[0:,i]
    t = t.tolist()[0]
    t = t[0]
    etab[i] = math.exp(t)
a = sum(etab)
for i in range(0,10):
    t = etab[i]
    t = t/a
    print(t)

最終輸出:
1.1880514704e-07
1.14077025507e-14
3.43722382983e-07
6.02540011641e-05
6.69096332382e-09
1.60123847877e-06
1.49785118331e-11
0.999934460415
1.7622763634e-07
3.03888425803e-06
預測值為7,說明的確建立的模型和tensflow一樣一樣的
6 sklearn庫的線性迴歸模型(這部分和softmax沒有關係,僅做參考)

from sklearn.linear_model import LinearRegression  
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV

LR = LinearRegression()
LR.fit(mnist.train.images, mnist.train.labels)
print(LR.coef_.shape)
print(LR.intercept_)

#預測
temp = LR.predict(mnist.test.images)
#t = temp.max(axis=1)
t = temp.argmax(axis=1)
print(t)
err = 0
Flag = mnist.test.labels.argmax(axis=1)
for i in range(0,t.shape[0]):
    if t[i] != Flag[i]:
        err = err+1
err = err/t.shape[0]
print(1-err)

最終輸出準確率:0.861
7 sklearn庫的隨機森林模型(這部分和softmax沒有關係,僅做參考)

from sklearn.linear_model import LinearRegression  
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV
from sklearn.ensemble import RandomForestRegressor  

Flag = mnist.train.labels.argmax(axis=1)

LR = RandomForestRegressor()
LR.fit(mnist.train.images,Flag)
#預測
t = LR.predict(mnist.test.images)
t = np.around(t)
print(t)
err = 0
Flag = mnist.test.labels.argmax(axis=1)
for i in range(0,t.shape[0]):
    if t[i] != Flag[i]:
        err = err+1
err = err/t.shape[0]
print(1-err)

準確率:0.7163999999999999