-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.py
More file actions
54 lines (42 loc) · 1.57 KB
/
Copy pathtest1.py
File metadata and controls
54 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: gbk -*-
# 文件名: torch_intro.py
import torch
import torch.nn as nn
import torch.optim as optim
# 1?? 设备选择(GPU 如果可用,否则 CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using device:", device)
# 2?? 创建简单数据集(y = 2*x + 1 + 噪声)
x_train = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1).to(device) # shape [100, 1]
y_train = 2 * x_train + 1 + 0.2 * torch.rand(x_train.size()).to(device)
# 3?? 定义简单线性回归模型
class LinearModel(nn.Module):
def __init__(self):
super(LinearModel, self).__init__()
self.linear = nn.Linear(1, 1) # 输入 1 维,输出 1 维
def forward(self, x):
return self.linear(x)
model = LinearModel().to(device)
print("Model structure:\n", model)
# 4?? 定义损失函数和优化器
criterion = nn.MSELoss() # 均方误差
optimizer = optim.SGD(model.parameters(), lr=0.1) # 随机梯度下降
# 5?? 训练模型
num_epochs = 50
for epoch in range(num_epochs):
# 前向传播
outputs = model(x_train)
loss = criterion(outputs, y_train)
# 反向传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 10 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}")
# 6?? 输出训练结果
predicted = model(x_train).detach().cpu() # 转回 CPU 用于查看
import matplotlib.pyplot as plt
plt.scatter(x_train.cpu(), y_train.cpu(), label='Original Data')
plt.plot(x_train.cpu(), predicted, 'r', label='Fitted Line')
plt.legend()
plt.show()