神經(jīng)網(wǎng)絡(luò)解決手寫數(shù)字識別問題
神經(jīng)網(wǎng)絡(luò)是深度學(xué)習(xí)的基礎(chǔ)糊秆,其強大的擬合和學(xué)習(xí)能力鹏往,讓其在圖像識別脚粟,人工智能方面表現(xiàn)十分出眾靡砌,這里不介紹神經(jīng)網(wǎng)絡(luò)的原理結(jié)構(gòu),(這一部分在網(wǎng)上非常多)珊楼,這里給出筆者利用純python寫的神經(jīng)網(wǎng)絡(luò)代碼通殃,實現(xiàn)了對sklearn庫中的digits數(shù)據(jù)集的識別,準(zhǔn)確率在93%以上
代碼如下,詳細(xì)見代碼注釋
import numpy as np
#激活函數(shù)tanh
def tanh(x):
return np.tanh(x)
#tanh的導(dǎo)函數(shù)画舌,為反向傳播做準(zhǔn)備
def tanh_deriv(x):
return 1-np.tanh(x)*np.tanh(x)
#激活函數(shù)邏輯斯底回歸函數(shù)
def logistic(x):
return 1/(1+np.exp(-x))
#激活函數(shù)logistic導(dǎo)函數(shù)
def logistic_deriv(x):
return logistic(x)*(1-logistic(x))
#神經(jīng)網(wǎng)絡(luò)類
class NeuralNetwork:
def __init__(self,layers,activation='tanh'):
#根據(jù)激活函數(shù)不同堕担,設(shè)置不同的激活函數(shù)和其導(dǎo)函數(shù)
if activation == 'logistic':
self.activation = logistic
self.activation_deriv = logistic_deriv
elif activation == 'tanh':
self.activation = tanh
self.activation_deriv = tanh_deriv
#初始化權(quán)重向量,從第一層開始初始化前一層和后一層的權(quán)重向量
self.weights = []
for i in range(1 , len(layers)-1):
#權(quán)重的shape曲聂,是當(dāng)前層和前一層的節(jié)點數(shù)目加1組成的元組
self.weights.append((2*np.random.random((layers[i-1]+1,layers[i]+1))-1)*0.25)
#權(quán)重的shape霹购,是當(dāng)前層加1和后一層組成的元組
self.weights.append((2*np.random.random((layers[i]+1,layers[i+1]))-1)*0.25)
#fit函數(shù)對元素進(jìn)行訓(xùn)練找出合適的權(quán)重,X表示輸入向量朋腋,y表示樣本標(biāo)簽齐疙,learning_rate表示學(xué)習(xí)率
#epochs表示循環(huán)訓(xùn)練次數(shù)
def fit(self , X , y , learning_rate=0.2 , epochs=10000):
X = np.atleast_2d(X)#保證X是二維矩陣
temp = np.ones([X.shape[0],X.shape[1]+1])
temp[:,0:-1] = X
X = temp #以上三步表示給X多加一列值為1
y = np.array(y)#將y轉(zhuǎn)換成np中array的形式
#進(jìn)行訓(xùn)練
for k in range(epochs):
i = np.random.randint(X.shape[0])#從0-epochs任意挑選一行
a = [X[i]]#將其轉(zhuǎn)換為list
#前向傳播
for l in range(len(self.weights)):
a.append(self.activation(np.dot(a[l],self.weights[l])))
#計算誤差
error = y[i] - a[-1]
deltas = [error * self.activation_deriv(a[-1])]
#反向傳播,不包括輸出層
for l in range(len(a)-2,0,-1):
deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))
deltas.reverse()
#更新權(quán)重
for i in range(len(self.weights)):
layer = np.atleast_2d(a[i])
delta = np.atleast_2d(deltas[i])
self.weights[i] += learning_rate*layer.T.dot(delta)
#進(jìn)行預(yù)測
def predict(self,x):
x = np.array(x)
temp = np.ones(x.shape[0]+1)
temp[0:-1] = x
a = temp
for l in range(0,len(self.weights)):
a = self.activation(np.dot(a,self.weights[l]))
return a
解決異或問題
if __name__ == '__main__':
nn = NeuralNetwork([2,2,1],'tanh')
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([0,1,1,0])
nn.fit(X,y)
for i in [[0,0],[0,1],[1,0],[1,1]]:
print i,nn.predict(i)
解決識別手寫數(shù)字
import numpy as np
from sklearn.datasets import load_digits
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.cross_validation import train_test_split
if __name__ == '__main__':
#加載數(shù)字?jǐn)?shù)據(jù)集
digits = load_digits()
X = digits.data
y = digits.target
#對X進(jìn)行最大最小值縮放
X = MinMaxScaler().fit_transform(X)
#生成一個64*100*10的神經(jīng)網(wǎng)絡(luò)旭咽,激活函數(shù)是logistic
nn = NeuralNetwork([64,100,10],'logistic')
X_train,X_test,y_train,y_test = train_test_split(X,y)
#對標(biāo)簽進(jìn)行標(biāo)簽化
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print 'start fitting'
nn.fit(X_train,labels_train,epochs=3000)
predictions = []
for i in range(X_test.shape[0]):
o = nn.predict(X_test[i])
predictions.append(np.argmax(o))//選擇概率最大的下標(biāo)作為預(yù)測結(jié)果
#預(yù)測結(jié)果
print predictions
#混淆矩陣
print confusion_matrix(y_test,predictions)
#分類報告
print classification_report(y_test,predictions)