import numpy as np # Sigmoid 啟用函數 def sigmoid(x): return 1 / (1 + np.exp(-x)) # 使用 sigmoid 導數進行非線性變換以及反向傳播計算梯度 def sigmoid_derivative(x): return x * (1 - x) def mse_loss(y_true, y_pred): return np.mean(np.square(y_true - y_pred)) class NeuralNetwork: def __init__(self, input_nodes, hidden_nodes, output_nodes): self.input_nodes = input_nodes self.hidden_nodes = hidden_nodes self.output_nodes = output_nodes self.weights_ih = np.random.rand(self.input_nodes, self.hidden_nodes) - 0.5 self.weights_ho = np.random.rand(self.hidden_nodes, self.output_nodes) - 0.5 self.bias_h = np.random.rand(1, self.hidden_nodes) - 0.5 self.bias_o = np.random.rand(1, self.output_nodes) - 0.5 def feedforward(self, input_data): hidden = sigmoid(np.dot(input_data, self.weights_ih) + self.bias_h) output = sigmoid(np.dot(hidden, self.weights_ho) + self.bias_o) return hidden, output def backward(self, input_data, hidden, output, target_data, learning_rate=0.1): # 計算損失函數的梯度 output_error = target_data - output output_delta = output_error * sigmoid_derivative(output) hidden_error = np.dot(output_delta, self.weights_ho.T) hidden_delta = hidden_error * sigmoid_derivative(hidden) self.weights_ho += learning_rate * np.dot(hidden.T, output_delta) self.weights_ih += learning_rate * np.dot(input_data.T, hidden_delta) self.bias_o += learning_rate * np.sum(output_delta, axis=0) self.bias_h += learning_rate * np.sum(hidden_delta, axis=0) # 根據輸入輸出資料,訓練多輪,更新神經網路的權重和偏置,最終得到正確的神經網路引數 def train(self, input_data, target_data, epochs, learning_rate=0.5): for _ in range(epochs): hidden, output = self.feedforward(input_data) self.backward(input_data, hidden, output, target_data, learning_rate) if __name__ == "__main__": # 範例 X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) Y = np.array([[0], [1], [1], [0]]) nn = NeuralNetwork(input_nodes=2, hidden_nodes=2, output_nodes=1) print("Before training:") _, output = nn.feedforward(X) print(output) nn.train(X, Y, epochs=2000, learning_rate=0.8) print("After training:") _, output = nn.feedforward(X) print(output) # 計算損失 loss = mse_loss(Y, output) print("Loss:", loss)
output_error = target_data - output
sigmoid(x) = 1 / (1 + e^(-x))