(Artificial Neural Network,ANN)是一種模仿生物神經網路的結構和功能的數學模型,其目的是透過學習和訓練,在處理未知的輸入資料時能夠進行複雜的非線性映射關係,實現自適應的智慧決策。可以說,ANN是人工智慧演算法中最基礎、最核心的一種演算法。
ANN模型的基本結構包含輸入層、隱藏層和輸出層。輸入層接收輸入數據,隱藏層負責對資料進行多層次、高維度的變換與處理,輸出層對處理後的資料進行輸出。 ANN的訓練過程是透過多次迭代,不斷調整神經網路中各層的權重,使得神經網路能夠對輸入資料進行正確的預測與分類。
接下來看看一個簡單的人工神經網路演算法範例:
import numpy as np class NeuralNetwork(): def __init__(self, layers): """ layers: 数组,包含每个层的神经元数量,例如 [2, 3, 1] 表示 3 层神经网络,第一层 2 个神经元,第二层 3 个神经元,第三层 1 个神经元。 weights: 数组,包含每个连接的权重矩阵,默认值随机生成。 biases: 数组,包含每个层的偏差值,默认值为 0。 """ self.layers = layers self.weights = [np.random.randn(a, b) for a, b in zip(layers[1:], layers[:-1])] self.biases = [np.zeros((a, 1)) for a in layers[1:]] def sigmoid(self, z): """Sigmoid 激活函数.""" return 1 / (1 + np.exp(-z)) def forward_propagation(self, a): """前向传播.""" for w, b in zip(self.weights, self.biases): z = np.dot(w, a) + b a = self.sigmoid(z) return a def backward_propagation(self, x, y): """反向传播.""" nabla_w = [np.zeros(w.shape) for w in self.weights] nabla_b = [np.zeros(b.shape) for b in self.biases] a = x activations = [x] zs = [] for w, b in zip(self.weights, self.biases): z = np.dot(w, a) + b zs.append(z) a = self.sigmoid(z) activations.append(a) delta = self.cost_derivative(activations[-1], y) * self.sigmoid_prime(zs[-1]) nabla_b[-1] = delta nabla_w[-1] = np.dot(delta, activations[-2].transpose()) for l in range(2, len(self.layers)): z = zs[-l] sp = self.sigmoid_prime(z) delta = np.dot(self.weights[-l+1].transpose(), delta) * sp nabla_b[-l] = delta nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) return (nabla_w, nabla_b) def train(self, x_train, y_train, epochs, learning_rate): """训练网络.""" for epoch in range(epochs): nabla_w = [np.zeros(w.shape) for w in self.weights] nabla_b = [np.zeros(b.shape) for b in self.biases] for x, y in zip(x_train, y_train): delta_nabla_w, delta_nabla_b = self.backward_propagation(np.array([x]).transpose(), np.array([y]).transpose()) nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)] nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)] self.weights = [w-(learning_rate/len(x_train))*nw for w, nw in zip(self.weights, nabla_w)] self.biases = [b-(learning_rate/len(x_train))*nb for b, nb in zip(self.biases, nabla_b)] def predict(self, x_test): """预测.""" y_predictions = [] for x in x_test: y_predictions.append(self.forward_propagation(np.array([x]).transpose())[0][0]) return y_predictions def cost_derivative(self, output_activations, y): """损失函数的导数.""" return output_activations - y def sigmoid_prime(self, z): """Sigmoid 函数的导数.""" return self.sigmoid(z) * (1 - self.sigmoid(z))
使用以下程式碼範例來實例化和使用這個簡單的神經網路類別:
x_train = [[0, 0], [1, 0], [0, 1], [1, 1]] y_train = [0, 1, 1, 0] # 创建神经网络 nn = NeuralNetwork([2, 3, 1]) # 训练神经网络 nn.train(x_train, y_train, 10000, 0.1) # 测试神经网络 x_test = [[0, 0], [1, 0], [0, 1], [1, 1]] y_test = [0, 1, 1, 0] y_predictions = nn.predict(x_test) print("Predictions:", y_predictions) print("Actual:", y_test)
輸出結果:
#Predictions: [0.011602156431658403, 0.9852717774725432, 0.9839448924887250725432, 0.983944892488725026320,026420,0264202530253225026320253025220253322253353333333333333333333分,2622332:020332:025332333333333分3223333333年]233333年]] 1, 1, 0]
以上是python人工智慧演算法之人工神經網路怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!