使用 Numpy 编写一个神经网络,并开发 BP 算法。两个隐层,第 一个隐层 4 个神经元,第二个隐层 8 个神经元,输入是 4 个神经 元,输出是 2 个神经元。激活函数用 ReLu。并对 data.csv 数据集进 行训练和测试,计算精度。
时间: 2023-11-22 18:56:34 浏览: 73
好的,让我们开始吧。首先,我们需要导入一些必要的库,如下所示:
```python
import numpy as np
import pandas as pd
```
然后,我们需要定义一些超参数,如下所示:
```python
# 定义超参数
learning_rate = 0.1
epochs = 1000
```
接下来,我们需要加载数据集,如下所示:
```python
# 加载数据集
data = pd.read_csv('data.csv', header=None)
X = data.iloc[:,:-2].values
y = data.iloc[:,-2:].values
```
然后,我们需要初始化模型参数,如下所示:
```python
# 初始化模型参数
input_dim = X.shape[1]
hidden_dim_1 = 4
hidden_dim_2 = 8
output_dim = y.shape[1]
W1 = np.random.randn(input_dim, hidden_dim_1) / np.sqrt(input_dim)
b1 = np.zeros((1, hidden_dim_1))
W2 = np.random.randn(hidden_dim_1, hidden_dim_2) / np.sqrt(hidden_dim_1)
b2 = np.zeros((1, hidden_dim_2))
W3 = np.random.randn(hidden_dim_2, output_dim) / np.sqrt(hidden_dim_2)
b3 = np.zeros((1, output_dim))
```
现在,我们需要定义 ReLU 激活函数和其导数,如下所示:
```python
# 定义 ReLU 激活函数和其导数
def relu(x):
return np.maximum(0, x)
def relu_derivative(x):
return np.where(x > 0, 1, 0)
```
接下来,我们可以开始训练模型了。我们需要循环迭代每个 epoch,并进行前向传播和反向传播。在每个 epoch 结束时,我们会计算模型的精度。
```python
# 训练模型
for i in range(epochs):
# 前向传播
z1 = np.dot(X, W1) + b1
a1 = relu(z1)
z2 = np.dot(a1, W2) + b2
a2 = relu(z2)
z3 = np.dot(a2, W3) + b3
y_pred = relu(z3)
# 反向传播
delta4 = y_pred - y
delta3 = np.dot(delta4, W3.T) * relu_derivative(z3)
delta2 = np.dot(delta3, W2.T) * relu_derivative(z2)
delta1 = np.dot(delta2, W1.T) * relu_derivative(z1)
dW3 = np.dot(a2.T, delta4)
db3 = np.sum(delta4, axis=0, keepdims=True)
dW2 = np.dot(a1.T, delta3)
db2 = np.sum(delta3, axis=0)
dW1 = np.dot(X.T, delta1)
db1 = np.sum(delta1, axis=0)
# 更新模型参数
W1 -= learning_rate * dW1
b1 -= learning_rate * db1
W2 -= learning_rate * dW2
b2 -= learning_rate * db2
W3 -= learning_rate * dW3
b3 -= learning_rate * db3
# 计算精度
if i % 100 == 0:
accuracy = np.mean(np.argmax(y_pred, axis=1) == np.argmax(y, axis=1))
print("Epoch:", i, "Accuracy:", accuracy)
```
最后,我们可以用测试数据集来测试模型的精度,如下所示:
```python
# 加载测试数据集
test_data = pd.read_csv('test_data.csv', header=None)
X_test = test_data.iloc[:,:-2].values
y_test = test_data.iloc[:,-2:].values
# 前向传播
z1 = np.dot(X_test, W1) + b1
a1 = relu(z1)
z2 = np.dot(a1, W2) + b2
a2 = relu(z2)
z3 = np.dot(a2, W3) + b3
y_pred = relu(z3)
# 计算精度
accuracy = np.mean(np.argmax(y_pred, axis=1) == np.argmax(y_test, axis=1))
print("Test Accuracy:", accuracy)
```
这样,我们就完成了神经网络的编写和 BP 算法的开发。
阅读全文