這篇文章將為大家詳細(xì)講解有關(guān)Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)算法及應(yīng)用的具體代碼,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的婁底網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)算法及應(yīng)用的具體代碼如下
首先用Python實(shí)現(xiàn)簡單地神經(jīng)網(wǎng)絡(luò)算法:
import numpy as np # 定義tanh函數(shù) def tanh(x): return np.tanh(x) # tanh函數(shù)的導(dǎo)數(shù) def tan_deriv(x): return 1.0 - np.tanh(x) * np.tan(x) # sigmoid函數(shù) def logistic(x): return 1 / (1 + np.exp(-x)) # sigmoid函數(shù)的導(dǎo)數(shù) def logistic_derivative(x): return logistic(x) * (1 - logistic(x)) class NeuralNetwork: def __init__(self, layers, activation='tanh'): """ 神經(jīng)網(wǎng)絡(luò)算法構(gòu)造函數(shù) :param layers: 神經(jīng)元層數(shù) :param activation: 使用的函數(shù)(默認(rèn)tanh函數(shù)) :return:none """ if activation == 'logistic': self.activation = logistic self.activation_deriv = logistic_derivative elif activation == 'tanh': self.activation = tanh self.activation_deriv = tan_deriv # 權(quán)重列表 self.weights = [] # 初始化權(quán)重(隨機(jī)) for i in range(1, len(layers) - 1): self.weights.append((2 * np.random.random((layers[i - 1] + 1, layers[i] + 1)) - 1) * 0.25) self.weights.append((2 * np.random.random((layers[i] + 1, layers[i + 1])) - 1) * 0.25) def fit(self, X, y, learning_rate=0.2, epochs=10000): """ 訓(xùn)練神經(jīng)網(wǎng)絡(luò) :param X: 數(shù)據(jù)集(通常是二維) :param y: 分類標(biāo)記 :param learning_rate: 學(xué)習(xí)率(默認(rèn)0.2) :param epochs: 訓(xùn)練次數(shù)(大循環(huán)次數(shù),默認(rèn)10000) :return: none """ # 確保數(shù)據(jù)集是二維的 X = np.atleast_2d(X) temp = np.ones([X.shape[0], X.shape[1] + 1]) temp[:, 0: -1] = X X = temp y = np.array(y) for k in range(epochs): # 隨機(jī)抽取X的一行 i = np.random.randint(X.shape[0]) # 用隨機(jī)抽取的這一組數(shù)據(jù)對(duì)神經(jīng)網(wǎng)絡(luò)更新 a = [X[i]] # 正向更新 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() 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) 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
使用自己定義的神經(jīng)網(wǎng)絡(luò)算法實(shí)現(xiàn)一些簡單的功能:
小案例:
X: Y
0 0 0
0 1 1
1 0 1
1 1 0
from NN.NeuralNetwork import NeuralNetwork import numpy as np nn = NeuralNetwork([2, 2, 1], 'tanh') temp = [[0, 0], [0, 1], [1, 0], [1, 1]] X = np.array(temp) y = np.array([0, 1, 1, 0]) nn.fit(X, y) for i in temp: print(i, nn.predict(i))
發(fā)現(xiàn)結(jié)果基本機(jī)制,無限接近0或者無限接近1
第二個(gè)例子:識(shí)別圖片中的數(shù)字
導(dǎo)入數(shù)據(jù):
from sklearn.datasets import load_digits import pylab as pl digits = load_digits() print(digits.data.shape) pl.gray() pl.matshow(digits.images[0]) pl.show()
觀察下:大小:(1797, 64)
數(shù)字0
接下來的代碼是識(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 NN.NeuralNetwork import NeuralNetwork from sklearn.cross_validation import train_test_split # 加載數(shù)據(jù)集 digits = load_digits() X = digits.data y = digits.target # 處理數(shù)據(jù),使得數(shù)據(jù)處于0,1之間,滿足神經(jīng)網(wǎng)絡(luò)算法的要求 X -= X.min() X /= X.max() # 層數(shù): # 輸出層10個(gè)數(shù)字 # 輸入層64因?yàn)閳D片是8*8的,64像素 # 隱藏層假設(shè)100 nn = NeuralNetwork([64, 100, 10], 'logistic') # 分隔訓(xùn)練集和測試集 X_train, X_test, y_train, y_test = train_test_split(X, y) # 轉(zhuǎn)化成sklearn需要的二維數(shù)據(jù)類型 labels_train = LabelBinarizer().fit_transform(y_train) labels_test = LabelBinarizer().fit_transform(y_test) print("start fitting") # 訓(xùn)練3000次 nn.fit(X_train, labels_train, epochs=3000) predictions = [] for i in range(X_test.shape[0]): o = nn.predict(X_test[i]) # np.argmax:第幾個(gè)數(shù)對(duì)應(yīng)大概率值 predictions.append(np.argmax(o)) # 打印預(yù)測相關(guān)信息 print(confusion_matrix(y_test, predictions)) print(classification_report(y_test, predictions))
結(jié)果:
矩陣對(duì)角線代表預(yù)測正確的數(shù)量,發(fā)現(xiàn)正確率很多
這張表更直觀地顯示出預(yù)測正確率:
共450個(gè)案例,成功率94%
關(guān)于“Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)算法及應(yīng)用的具體代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁題目:Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)算法及應(yīng)用的具體代碼-創(chuàng)新互聯(lián)
URL分享:http://vcdvsql.cn/article2/ddhgic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、標(biāo)簽優(yōu)化、網(wǎng)站改版、電子商務(wù)、App開發(fā)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容