要编写一个简单的人工神经网络(ANN)程序,可以从一个基本的前馈神经网络开始,该网络通常包括输入层、一个或多个隐藏层以及输出层。在这个例子中,将使用Python的NumPy库来处理数学运算,并使用Sigmoid函数作为激活函数。将实现一个用于二分类的简单神经网络。
以下是构建和训练该神经网络的步骤和相应的Python代码:
1. 导入必要的库
|---|--------------------------------|
| | import numpy as np
|
| | |
| | # Sigmoid激活函数及其导数
|
| | def sigmoid(x):
|
| | return 1 / (1 + np.exp(-x))
|
| | |
| | def sigmoid_derivative(x):
|
| | return x * (1 - x)
|
2. 初始化参数
需要随机初始化权重和偏置。
|---|---------------------------------------------------------------------|
| | def initialize_parameters(input_size, hidden_size, output_size):
|
| | np.random.seed(3) # 设置随机种子以确保结果可重复
|
| | W1 = np.random.randn(input_size, hidden_size) * 0.01
|
| | b1 = np.zeros((1, hidden_size))
|
| | W2 = np.random.randn(hidden_size, output_size) * 0.01
|
| | b2 = np.zeros((1, output_size))
|
| | |
| | return {"W1": W1, "b1": b1, "W2": W2, "b2": b2}
|
3. 前向传播
|---|-----------------------------------------------------|
| | def forward_propagation(X, parameters):
|
| | W1 = parameters["W1"]
|
| | b1 = parameters["b1"]
|
| | W2 = parameters["W2"]
|
| | b2 = parameters["b2"]
|
| | |
| | Z1 = np.dot(X, W1) + b1
|
| | A1 = sigmoid(Z1)
|
| | Z2 = np.dot(A1, W2) + b2
|
| | A2 = sigmoid(Z2)
|
| | |
| | cache = {"Z1": Z1, "A1": A1, "Z2": Z2, "A2": A2}
|
| | return A2, cache
|
4. 计算损失
将使用交叉熵损失函数。
|---|---------------------------------------------------------------------------------|
| | def compute_cost(A2, Y):
|
| | m = Y.shape[1]
|
| | logprobs = np.multiply(-np.log(A2), Y) + np.multiply(-np.log(1 - A2), 1 - Y)
|
| | cost = np.sum(logprobs) / m
|
| | return cost
|
5. 反向传播
|---|-------------------------------------------------------------|
| | def backward_propagation(parameters, cache, X, Y):
|
| | m = X.shape[1]
|
| | |
| | A2 = cache["A2"]
|
| | Z1 = cache["Z1"]
|
| | A1 = cache["A1"]
|
| | W2 = parameters["W2"]
|
| | |
| | dZ2 = A2 - Y
|
| | dW2 = np.dot(A1.T, dZ2) / m
|
| | db2 = np.sum(dZ2, axis=1, keepdims=True) / m
|
| | |
| | dZ1 = np.dot(dZ2, W2.T) * sigmoid_derivative(A1)
|
| | dW1 = np.dot(X.T, dZ1) / m
|
| | db1 = np.sum(dZ1, axis=1, keepdims=True) / m
|
| | |
| | grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2}
|
| | return grads
|
6. 更新参数
|---|------------------------------------------------------------------|
| | def update_parameters(parameters, grads, learning_rate=0.01):
|
| | parameters["W1"] -= learning_rate * grads["dW1"]
|
| | parameters["b1"] -= learning_rate * grads["db1"]
|
| | parameters["W2"] -= learning_rate * grads["dW2"]
|
| | parameters["b2"] -= learning_rate * grads["db2"]
|
| | |
| | return parameters
|
7. 整合模型
|---|----------------------------------------------------------------------------|
| | def nn_model(X, Y, hidden_size, num_iterations=10000, print_cost=True):
|
| | np.random.seed(3)
|
| | n_x = X.shape[0]
|
| | n_y = Y.shape[0]
|
| | parameters = initialize_parameters(n_x, hidden_size, n_y)
|
| | |
| | for i in range(0, num_iterations):
|
| | A2, cache = forward_propagation(X, parameters)
|
| | cost = compute_cost(A2, Y)
|
| | grads = backward_propagation(parameters, cache, X, Y)
|
| | parameters = update_parameters(parameters, grads)
|
| | |
| | if print_cost and i % 1000 == 0:
|
| | print("Cost after iteration %i: %f" %(i, cost))
|
| | |
| | return parameters
|