假如我们有一个数据集形状为(348,14)。即有348个记录,每个记录有14个特征值。
我们想要搭建一个如下的神经网络:
import torch
import numpy as np
# 创建数据集: 每个样本有14个特征
x_train = np.array([
[0.5, -1.2, 0.3, 0.8, 1.0, -0.5, 2.3, 1.2, -0.3, 1.5, -1.1, 0.6, -0.8, 0.7],
[1.5, 2.2, 1.3, -0.7, 1.1, 0.5, -1.3, 0.4, 1.2, 0.8, 0.3, 0.6, 2.1, 0.2],
[0.9, -0.2, -0.5, -1.2, 1.3, -1.1, 0.7, 1.5, 0.9, 1.0, -0.4, 0.5, -1.0, 1.4],
[-0.4, 0.8, 1.2, -0.1, 1.5, 0.2, 0.6, -1.3, 1.0, 1.3, 0.3, -0.9, 1.1, 0.5],
[1.0, 0.2, -1.4, 0.3, -0.7, 1.1, -0.1, 0.5, 0.6, 1.5, 0.7, -0.5, 0.9, -0.2]
], dtype=np.float64)
y_train = np.array([[5.0], [6.0], [4.0], [7.0], [3.0]], dtype=np.float64)
# 将数据转化为张量
x = torch.tensor(x_train, dtype=torch.float64)
y = torch.tensor(y_train, dtype=torch.float64)
# 权重参数初始化
weights1 = torch.randn((14, 128), dtype=torch.float64, requires_grad=True) # 输入维度是14, 第一层有128个神经元
biases1 = torch.randn((1, 128), dtype=torch.float64, requires_grad=True)
weights2 = torch.randn((128, 1), dtype=torch.float64, requires_grad=True)
biases2 = torch.randn((1, 1), dtype=torch.float64, requires_grad=True)
learning_rate = 0.001 # 学习率
losses = [] # 用于存储损失值
for i in range(1000): # 这里遍历1000次
net1 = x.mm(weights1) + biases1 # 如果有5条记录那么结构为[5,128]
out1 = torch.relu(net1) # 通过激活函数[5,128]
predictions = out1.mm(weights2) + biases2 # 输出,预测值[5,1]
# 计算损失
loss = torch.mean((predictions - y) ** 2)
losses.append(loss.detach().numpy())
if i % 100 == 0:
print("loss:", loss.item())
# 反向传播计算
loss.backward()
# 更新权重
with torch.no_grad(): # 使用no_grad避免梯度跟踪
weights1 -= learning_rate * weights1.grad
biases1 -= learning_rate * biases1.grad
weights2 -= learning_rate * weights2.grad
biases2 -= learning_rate * biases2.grad
# 每次迭代清空梯度累加值
weights1.grad.zero_()
biases1.grad.zero_()
weights2.grad.zero_()
biases2.grad.zero_()