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 |

相关推荐
etsuyou1 天前
js前端this指向规则
开发语言·前端·javascript
shizhenshide1 天前
为什么有时候 reCAPTCHA 通过率偏低,常见原因有哪些
开发语言·php·验证码·captcha·recaptcha·ezcaptcha
lichong9511 天前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++
Tiny番茄1 天前
31.下一个排列
数据结构·python·算法·leetcode
mit6.8241 天前
[Agent可视化] 配置系统 | 实现AI模型切换 | 热重载机制 | fsnotify库(go)
开发语言·人工智能·golang
友友马1 天前
『 QT 』QT控件属性全解析 (一)
开发语言·前端·qt
小白学大数据1 天前
实战:Python爬虫如何模拟登录与维持会话状态
开发语言·爬虫·python
一念&1 天前
每日一个C语言知识:C 结构体
c语言·开发语言
FriendshipT1 天前
目标检测:使用自己的数据集微调DEIMv2进行物体检测
人工智能·pytorch·python·目标检测·计算机视觉
平谷一勺1 天前
数据清洗-缺失值的处理
python·数据分析