softmax回歸梯度公式推導及實現

2021-08-17 12:11:36 字數 3620 閱讀 6745

推導如下:

**實現使用的是鳶尾花資料集,該資料集有3種鳶尾花,資料集剛開始長下面這個樣子:

data=pd.read_csv('iris.data',header=none) #一共有150個樣本

對資料進行預處理,首先把3種鳶尾花名稱編碼成0,1,2,然後還要插入一列,使資料x變成(1,x),方便算theta*(1,x)

sort=data[4]   

data[4]=pd.categorical(data[4]).codes #將種類編碼成0,1,2

data.insert(0,'常數項',1) #將x擴充套件成(1,x),以便計算theta*(1,x)

變換完成後,資料變成下面的樣子

然後就可以選出訓練集和測試集了

x=data.iloc[:,0:5].as_matrix()

y=data[4].as_matrix()

x,x_test,y,y_test=train_test_split(x,y,test_size=0.3) #用105個樣本訓練

theta=softmax(x,y,0.02) #學習因子不能取太大,否則最後計算出的theta無窮大

softmax回歸引數訓練的程式如下

#採用隨機梯度下降法,每次只挑選乙個樣本做優化

def softmax(x,y,alpha):  

theta=np.zeros((3,5))  #初始theta矩陣  

for i in range(10000): #迭代10000次  

k=np.random.randint(0,105) #從105個樣本中隨機挑選乙個做優化

x_=x[k].reshape(5,1)

theta_t_x=np.dot(theta,x_)  #計算所有的theta*x  

e_theta_t_x=np.exp(theta_t_x)   #計算所有指數函式  

denominator=e_theta_t_x.sum()  #計算分母  

numerator=e_theta_t_x   #分子  

fraction=numerator/denominator  #計算所有分數  

y_vector=np.where(np.arange(3).reshape(3,1)==y[k],1,0) #計算y向量

gradient=(fraction-y_vector)*x[k]

theta-=alpha*gradient   #更新theta矩陣  

return theta

訓練完theta後,就可以在測試集上驗證了

predict=np.dot(theta,x_test.t)

predict_sort=predict.argmax(axis=0)

num_of_wrong=(predict_sort!=y_test).sum()

print("**錯誤的個數: ",num_of_wrong)

print("正確率為: %".format((45-num_of_wrong)/45*100))

結果如下:

**錯誤的個數:  1

正確率為: 97.77777777777777%

完整的**實現如下所示:

import numpy as np  

import pandas as pd  

from sklearn.model_selection import train_test_split  

#採用隨機梯度下降法,每次只挑選乙個樣本做優化

def softmax(x,y,alpha):  

theta=np.zeros((3,5))  #初始theta矩陣  

for i in range(10000): #迭代10000次  

k=np.random.randint(0,105) #從105個樣本中隨機挑選乙個做優化

x_=x[k].reshape(5,1)

theta_t_x=np.dot(theta,x_)  #計算所有的theta*x  

e_theta_t_x=np.exp(theta_t_x)   #計算所有指數函式  

denominator=e_theta_t_x.sum()  #計算分母  

numerator=e_theta_t_x   #分子  

fraction=numerator/denominator  #計算所有分數  

y_vector=np.where(np.arange(3).reshape(3,1)==y[k],1,0) #計算y向量

gradient=(fraction-y_vector)*x[k]

theta-=alpha*gradient   #更新theta矩陣  

return theta  

if __name__=="__main__":  

data=pd.read_csv('iris.data',header=none)  #一共有150個樣本  

sort=data[4]     

data[4]=pd.categorical(data[4]).codes   #將種類編碼成0,1,2  

data.insert(0,'常數項',1)   #將x擴充套件成(1,x),以便計算theta*x  

x=data.iloc[:,0:5].as_matrix()  

y=data[4].as_matrix()  

x,x_test,y,y_test=train_test_split(x,y,test_size=0.3)  #用105個樣本訓練  

theta=softmax(x,y,0.01)   #學習因子不能取太大,否則最後計算出的theta無窮大  

predict=np.dot(theta,x_test.t)  

predict_sort=predict.argmax(axis=0)  

num_of_wrong=(predict_sort!=y_test).sum()  

print("**錯誤的個數: ",num_of_wrong)  

print("正確率為: %".format((45-num_of_wrong)/45*100))

Logistic回歸和梯度上公升 公式推導

theta theta alpha frac frac sum y h x x 所以權重的迭代更新式為 theta theta alpha sum y h x x 批量梯度上公升 每進行一次迭代更新 就會 計算所有樣本 因此得到的模型正確率比較高,但同時計算複雜度高,演算法耗時。計算過程如下 1.首...

梯度下降演算法公式推導

梯度下降法的基本思想可以模擬為乙個下山的過程。假設這樣乙個場景 乙個人被困在山上,需要從山上下來 找到山的最低點 但此時山上的濃霧很大,導致可視度很低 因此,下山的路徑就無法確定,必須利用自己周圍的資訊一步一步地找到下山的路。這個時候,便可利用梯度下降演算法來幫助自己下山。怎麼做呢,首先以他當前的所...

梯度下降法實現softmax回歸MATLAB程式

解決二分類問題時我們通常用logistic回歸,而解決多分類問題時若果用logistic回歸,則需要設計多個分類器,這是相當麻煩的事情。softmax回歸可以看做是logistic回歸的普遍推廣 logistic回歸可看成softmax回歸在類別數為2時的特殊情況 在多分類問題上softmax回歸是...