4.2 神经网络算法代码实现
生活随笔
收集整理的這篇文章主要介紹了
4.2 神经网络算法代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考前一篇文章“4.1 神經網絡算法原理” , 用Python實現了一個版本,不過這個版本有一些坑,懶得去調了,
以后會出一個新的版本,這個版本就這樣了吧
python代碼
NeraulNetwork.py import numpy as np# 雙曲正切函數 def tanh(x):return np.tanh(x)# 雙曲正切函數導數 def tanh_derivative(x):return 1 - np.tanh(x) * np.tanh(x)# sigmoid函數 def sigmoid(x):return 1 / (1 + np.exp(-x))# sigmoid函數導數 def sigmoid_derivative(x):return sigmoid(x) * (1 - sigmoid(x))# 神經網絡實現類 class NeuralNetwork():# layers:神經網絡# activation:激勵函數# learning_rate 學習率# loss_threshold:損失閥值# epoch:最大訓練次數def __init__(self, layers=[], activation="sigmoid",learning_rate=0.1, epoch=1000, loss_threshold=0.01):if activation == "sigmoid":self.activation = sigmoidself.activation_derivative = sigmoid_derivativeelif activation == "tanh":self.activation = tanhself.activation_derivative = tanh_derivativeelse:self.activation = sigmoidself.activation_derivative = sigmoid_derivativeself.layers = layersself.init_weights(layers)self.init_bias(layers)self.init_nodes()self.init_errors()self.learning_rate = learning_rateself.epoch = epochself.loss_threshold = loss_threshold# 校驗二維數組def valiad_two_array(self, data):if isinstance(data, list) and len(data) > 0:if isinstance(data[0], list) == False or len(data[0]) == 0:raise RuntimeError("參數錯誤,請傳一個不為空的二維數組")else:raise RuntimeError("參數錯誤,請傳一個不為空的二維數組")# 校驗一維數組def valid_one_array(self, data):if isinstance(data, list) == False or len(data) == 0:raise RuntimeError("參數錯誤,請傳入一個不為空的一維數組")# 初始化權重def init_weights(self, layers):self.weights = []for i in range(1, len(layers)):self.weights.append(np.random.random((layers[i - 1], layers[i])))# 初始化偏向def init_bias(self, layers):self.bias = []for i in range(1, len(layers)):self.bias.append(np.random.random((layers[i], 1)))# 訓練模型def fit(self, data):self.valiad_two_array(data)self.counter = 0for i in range(len(data)):self.training_data(data[i], i)# 預測數據def predict(self, data):self.valiad_two_array(data)counter = 0for one_data in data:self.forward_propagation(one_data)predict = self.nodes[len(self.layers) - 1]for i in range(len(predict)):predict[i] = self.handle_by_threshold(predict[i])print("predict[{}] = {} ".format(counter, predict))counter += 1# 根據閥值處理數據def handle_by_threshold(self, data):if data >= 0.5:return 1else:return 0# 一次訓練流程def training_data(self, one_data, number):self.loss = 1one_training_counter = 0while self.loss > self.loss_threshold and one_training_counter < self.epoch:self.counter += 1one_training_counter += 1self.forward_propagation(one_data)self.back_propagation_error(one_data)self.back_propagation_update_weights()self.back_propagation_update_bias()# print("總次數{},第{}行數據,當前次數:{},\n{}".# format(self.counter, number, one_training_counter, self.get_obj_info()))# 獲取對象信息def get_obj_info(self):info = "\n\n weights: " + str(self.weights) \+ "\n\n bais: " + str(self.bias) \+ "\n\n nodes: " + str(self.nodes) \+ "\n\n errors: " + str(self.errors) \+ "\n\n loss: " + str(self.loss)return info# 輸出層錯誤計算# out:經過激勵函數計算后的結果# predict:原始預測的結果def calculate_out_layer_error(self, out, predict):return out * (1 - out) * (predict - out)# 隱藏層錯誤計算# out:經過激勵函數計算后的結果# errors:下一層所有節點的損失合計def calculate_hidden_layer_error(self, out, errors):return out * (1 - out) * errors# 前向傳播,遞歸得到每一個節點的值# one_row_data:一行數據# counter: 計數器def forward_propagation(self, one_row_data, counter=0):if counter == 0:input = self.get_input(one_row_data)self.input = inputfor i in range(len(self.input)):self.nodes[0][i] = self.input[i]counter += 1if counter == len(self.layers):returncurrent_nodes = self.nodes[counter]pre_nodes = self.nodes[counter - 1]for i in range(len(current_nodes)):current_value = 0for j in range(len(pre_nodes)):pre_node = pre_nodes[j]pre_weights = self.weights[counter - 1][j][i]current_value += pre_node * pre_weightscurrent_bias = self.bias[counter - 1][i][0]current_value = (current_value + current_bias)[0]current_node = self.activation(current_value)current_nodes[i] = current_nodeself.forward_propagation(one_row_data, counter + 1)# 得到特征值def get_input(self, one_row_data):return one_row_data[:self.layers[0]]# 根據特征值真實結果def get_out(self, one_row_data):return one_row_data[self.layers[0]:]# 后向傳播,得到誤差def back_propagation_error(self, one_row_data, counter=-1):if counter == -1: # 第一次進入方法,初始化counter = len(self.layers) - 1out = self.get_out(one_row_data)self.out = outif counter == 0: # 遍歷集合(第一層輸入層不計算損失)returncurrent_nodes = self.nodes[counter]if counter == len(self.layers) - 1: # 輸出層損失計算loss = 0for i in range(len(current_nodes)):current_node = current_nodes[i]predict = self.out[i]error_value = self.calculate_out_layer_error(current_node, predict)self.errors[counter][i] = error_valueloss += pow(predict - current_node, 2)self.loss = losselse: # 隱藏層損失計算next_errors = self.errors[counter + 1]for i in range(len(current_nodes)):current_node = current_nodes[i]errors = 0for j in range(len(next_errors)):error = next_errors[j]weight = self.weights[counter][i]errors += error * weighterror_value = self.calculate_hidden_layer_error(current_node, errors)self.errors[counter][i] = error_valueself.back_propagation_error(one_row_data, counter - 1)# 后向傳播,更新權重def back_propagation_update_weights(self):for i in reversed(range(len(self.layers) - 1)):current_nodes = self.nodes[i]errors = self.errors[i + 1]for j in range(len(current_nodes)):for m in range(len(errors)):error = errors[m]current_node = current_nodes[j]weight = self.weights[i][j][m]weight_delta = self.learning_rate * error * current_nodeupdate_weight = weight + weight_deltaself.weights[i][j][m] = update_weight# 后向傳播,更新偏向def back_propagation_update_bias(self):for i in reversed(range(len(self.layers) - 1)):bias = self.bias[i]for j in range(len(bias)):error = self.errors[i + 1][j]bias_delta = self.learning_rate * errorbias[j] += bias_delta# 設置權重def set_weights(self, weights):self.weights = weights# 設置偏向def set_bias(self, bias):self.bias = bias# 初始化所有節點(節點值設置為一個隨機數)def init_nodes(self):self.nodes = []for i in range(len(self.layers)):self.nodes.append(np.random.random((self.layers[i], 1)))# 初始化所有節點損失值(損失值設置為一個隨機數)def init_errors(self):self.errors = []for i in range(len(self.layers)):self.errors.append(np.random.random((self.layers[i], 1)))?
驗證寫的代碼
NeraulNetworkTest.py from NeraulNetwork import NeuralNetwork import numpy as nplayers = [3, 2, 1] nn = NeuralNetwork(layers)weights = np.array([[[0.2, -0.3], [0.4, 0.1], [-0.5, 0.2]], [[-0.3], [-0.2]]]) nn.set_weights(weights)bias = np.array([[[-0.4], [0.2]], [[0.1]]]) nn.set_bias(bias)data = [[1, 0, 1, 1],[0, 0, 1, 0],[0, 0, 0, 0],[0, 0, 1, 0],[1, 0, 1, 1],[0, 0, 0, 0],[0, 0, 1, 0],[1, 0, 1, 1],[0, 0, 0, 0],[1, 0, 1, 1],[0, 0, 1, 0],[0, 0, 0, 0],[1, 0, 1, 1],[0, 0, 0, 0],[1, 0, 1, 1],[0, 0, 0, 0],[1, 0, 1, 1],[0, 0, 1, 0],[0, 0, 0, 0],[1, 0, 1, 1],[0, 0, 1, 0],[0, 0, 0, 0], ] nn.fit(data)predict_data = [[1, 0, 1, 1], [0, 0, 0, 0],[0, 0, 1, 0],[1, 1, 1, 1]] predict_result = nn.predict(predict_data)?
輸出結果
predict[0] = [[1.]] predict[1] = [[0.]] predict[2] = [[0.]] predict[3] = [[1.]]?
總結
以上是生活随笔為你收集整理的4.2 神经网络算法代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4.1 神经网络算法原理
- 下一篇: python九九乘法表求和,平均数,最大