-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
100 lines (86 loc) · 3.11 KB
/
Copy pathtest.py
File metadata and controls
100 lines (86 loc) · 3.11 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
class MLP(nn.Module):
def __init__(self, in_dim, hidden_dim, out_dim):
super().__init__()
self.net = nn.Sequential(
nn.Linear(in_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, out_dim)
)
def forward(self, x):
return self.net(x)
class Trainer:
def __init__(self, model, train_loader, test_loader, optimizer, criterion, device):
self.model = model
self.train_loader = train_loader
self.test_loader = test_loader
self.optimizer = optimizer
self.criterion = criterion
self.device = device
self.model.to(self.device)
def fit(self, epochs=10):
for epoch in range(epochs):
self.model.train()
total_loss = 0
for x, y in self.train_loader:
x, y = x.to(self.device), y.to(self.device)
self.optimizer.zero_grad()
output = self.model(x)
loss = self.criterion(output, y)
loss.backward()
self.optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(self.train_loader)
print(f"Epoch {epoch+1}/{epochs}, Train Loss: {avg_loss:.4f}")
self.evaluate()
def evaluate(self):
self.model.eval()
total_loss = 0
with torch.no_grad():
for x, y in self.test_loader:
x, y = x.to(self.device), y.to(self.device)
output = self.model(x)
loss = self.criterion(output, y)
total_loss += loss.item()
avg_loss = total_loss / len(self.test_loader)
print(f"Test Loss: {avg_loss:.4f}")
if __name__ == "__main__":
# 配置参数
IN_DIM = 512
HIDDEN_DIM = 256
OUT_DIM = 1
BATCH_SIZE = 32
LR = 1e-3
EPOCHS = 5
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 1. 生成模拟数据
# 假设输入是 (N, 512),输出是 (N, 1)
x = torch.randn(1000, IN_DIM)
y = torch.randn(1000, OUT_DIM)
# 2. 划分数据集
train_size = int(0.8 * len(x))
train_x, test_x = x[:train_size], x[train_size:]
train_y, test_y = y[:train_size], y[train_size:]
# 3. 创建 DataLoader
train_dataset = TensorDataset(train_x, train_y)
test_dataset = TensorDataset(test_x, test_y)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)
# 4. 初始化模型、优化器、损失函数
model = MLP(IN_DIM, HIDDEN_DIM, OUT_DIM)
optimizer = optim.AdamW(model.parameters(), lr=LR)
criterion = nn.MSELoss()
# 5. 初始化训练器并开始训练
trainer = Trainer(
model=model,
train_loader=train_loader,
test_loader=test_loader,
optimizer=optimizer,
criterion=criterion,
device=DEVICE
)
print(f"Start training on {DEVICE}...")
trainer.fit(epochs=EPOCHS)