bp神经网络多元回归预测三层,要有激活函数定义损失函数和优化器代码用numpy库和pandas库复杂一点relu函数
时间: 2024-03-16 09:42:57 浏览: 42
以下是一个使用numpy和pandas库实现的三层BP神经网络,用于多元回归预测,包含激活函数relu、损失函数和优化器的定义:
```python
import numpy as np
import pandas as pd
# 定义激活函数
def relu(x):
return np.maximum(0, x)
# 定义损失函数
def mean_squared_error(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)
# 定义优化器
def sgd(w, dw, learning_rate):
return w - learning_rate * dw
# 读取数据集
data = pd.read_csv("data.csv")
# 将数据集分为训练集和测试集
train_data = data[:80]
test_data = data[80:]
# 初始化权重和偏置
input_size = 2
hidden_size = 4
output_size = 1
w1 = np.random.randn(input_size, hidden_size)
b1 = np.random.randn(hidden_size)
w2 = np.random.randn(hidden_size, output_size)
b2 = np.random.randn(output_size)
# 训练模型
epochs = 1000
learning_rate = 0.01
for i in range(epochs):
# 前向传播
hidden = relu(np.dot(train_data.iloc[:, :2], w1) + b1)
output = np.dot(hidden, w2) + b2
# 计算损失
loss = mean_squared_error(train_data.iloc[:, 2], output)
# 反向传播
dloss_doutput = 2 * (output - train_data.iloc[:, 2])
doutput_dhidden = w2.T
dhidden_dz = (np.dot(train_data.iloc[:, :2], w1) + b1) > 0
dloss_dhidden = np.dot(dloss_doutput, doutput_dhidden) * dhidden_dz
dw2 = np.dot(hidden.T, dloss_doutput)
db2 = np.sum(dloss_doutput, axis=0)
dw1 = np.dot(train_data.iloc[:, :2].T, dloss_dhidden)
db1 = np.sum(dloss_dhidden, axis=0)
# 更新权重和偏置
w2 = sgd(w2, dw2, learning_rate)
b2 = sgd(b2, db2, learning_rate)
w1 = sgd(w1, dw1, learning_rate)
b1 = sgd(b1, db1, learning_rate)
# 输出训练过程中的损失
if i % 100 == 0:
print("Epoch %d: loss = %f" % (i, loss))
# 在测试集上评估模型
hidden = relu(np.dot(test_data.iloc[:, :2], w1) + b1)
output = np.dot(hidden, w2) + b2
test_loss = mean_squared_error(test_data.iloc[:, 2], output)
print("Test loss = %f" % test_loss)
```
以上代码中,使用relu作为激活函数,均方误差作为损失函数,随机梯度下降作为优化器。其中,`data.csv`是包含3列数据的CSV文件,前两列是输入特征,第三列是输出标签。首先将数据集分为训练集和测试集,然后初始化权重和偏置,并在训练集上进行训练。训练过程中,首先进行前向传播,计算损失,然后进行反向传播,计算梯度,最后更新权重和偏置。训练完成后,在测试集上评估模型,计算测试集上的损失。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)