這篇文章將為大家詳細講解有關Python實現神經網絡算法及應用的具體代碼,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Python實現神經網絡算法及應用的具體代碼如下
首先用Python實現簡單地神經網絡算法:
import numpy as np # 定義tanh函數 def tanh(x): return np.tanh(x) # tanh函數的導數 def tan_deriv(x): return 1.0 - np.tanh(x) * np.tan(x) # sigmoid函數 def logistic(x): return 1 / (1 + np.exp(-x)) # sigmoid函數的導數 def logistic_derivative(x): return logistic(x) * (1 - logistic(x)) class NeuralNetwork: def __init__(self, layers, activation='tanh'): """ 神經網絡算法構造函數 :param layers: 神經元層數 :param activation: 使用的函數(默認tanh函數) :return:none """ if activation == 'logistic': self.activation = logistic self.activation_deriv = logistic_derivative elif activation == 'tanh': self.activation = tanh self.activation_deriv = tan_deriv # 權重列表 self.weights = [] # 初始化權重(隨機) 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): """ 訓練神經網絡 :param X: 數據集(通常是二維) :param y: 分類標記 :param learning_rate: 學習率(默認0.2) :param epochs: 訓練次數(大循環次數,默認10000) :return: none """ # 確保數據集是二維的 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): # 隨機抽取X的一行 i = np.random.randint(X.shape[0]) # 用隨機抽取的這一組數據對神經網絡更新 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
使用自己定義的神經網絡算法實現一些簡單的功能:
小案例:
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))

發現結果基本機制,無限接近0或者無限接近1
第二個例子:識別圖片中的數字
導入數據:
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)
數字0

接下來的代碼是識別它們:
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
# 加載數據集
digits = load_digits()
X = digits.data
y = digits.target
# 處理數據,使得數據處于0,1之間,滿足神經網絡算法的要求
X -= X.min()
X /= X.max()
# 層數:
# 輸出層10個數字
# 輸入層64因為圖片是8*8的,64像素
# 隱藏層假設100
nn = NeuralNetwork([64, 100, 10], 'logistic')
# 分隔訓練集和測試集
X_train, X_test, y_train, y_test = train_test_split(X, y)
# 轉化成sklearn需要的二維數據類型
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print("start fitting")
# 訓練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:第幾個數對應大概率值
predictions.append(np.argmax(o))
# 打印預測相關信息
print(confusion_matrix(y_test, predictions))
print(classification_report(y_test, predictions))結果:
矩陣對角線代表預測正確的數量,發現正確率很多

這張表更直觀地顯示出預測正確率:
共450個案例,成功率94%

關于“Python實現神經網絡算法及應用的具體代碼”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網頁題目:Python實現神經網絡算法及應用的具體代碼-創新互聯
URL分享:http://www.yijiale78.com/article2/ddhgic.html
成都網站建設公司_創新互聯,為您提供ChatGPT、標簽優化、網站改版、電子商務、App開發、Google
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯