python实现人工神经网络

要编写一个简单的人工神经网络(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 |

相关推荐
数据智能老司机1 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机2 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i3 小时前
django中的FBV 和 CBV
python·django
c8i3 小时前
python中的闭包和装饰器
python
这里有鱼汤7 小时前
小白必看:QMT里的miniQMT入门教程
后端·python
TF男孩17 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP1 天前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法