-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
166 lines (145 loc) · 6.44 KB
/
train.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import json
import random
import torch
import torch.utils
import torch.nn as nn
import torch.optim as optim
import os
import numpy as np
from tqdm import tqdm
from dataset import NeckMpuDataset
from model import Mpu2TextClassifier
from torch.nn.utils.rnn import pad_sequence
def collate_fn(batch):
label = pad_sequence([i["label"] for i in batch], batch_first=True, padding_value=0)
data = pad_sequence(
[i["data"].transpose(0, 1) for i in batch], batch_first=True, padding_value=0
).transpose(1, 2)
pad_length = (data.shape[2] // 20 + 1) * 20 - data.shape[2]
data = torch.cat(
[data, torch.zeros(size=(data.shape[0], data.shape[1], pad_length))], dim=-1
)
return {
"data": data,
"label": label,
"cls_label": torch.hstack([i["cls_label"] for i in batch]),
"length": torch.hstack([i["length"] for i in batch]),
}
def train(ROOT_PATH, MODEL_PATH, file, rand_seed = 42):
random.seed(rand_seed)
torch.manual_seed(rand_seed)
torch.cuda.manual_seed(rand_seed)
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
with open(os.path.join(ROOT_PATH, r"train-data\\ready\\word2num.json"), "r") as f:
word2num_dict = json.load(f)
num_epochs = 5
batch_size = 32
train_dataset = NeckMpuDataset(mode="train")
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn
)
valid_dataset = NeckMpuDataset(mode="valid")
valid_loader = torch.utils.data.DataLoader(
valid_dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn
)
test_dataset = NeckMpuDataset(mode="test")
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn
)
model = Mpu2TextClassifier(
window_size=20,
mpu_channels=30,
n_dim=128,
n_head=4,
dropout=0.5,
cls_num=len(word2num_dict),
more_than_one_word=word2num_dict["<more_than_one_word>"],
depth=3
).to(DEVICE)
optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
loss_history = []
acc_history = []
for epoch in range(num_epochs):
model.train()
mean_loss = 0.0
mean_acc = 0.0
with tqdm(train_loader, unit="batch") as tepoch:
for input_dict in tepoch:
for key in input_dict.keys():
if isinstance(input_dict[key], torch.Tensor):
input_dict[key] = input_dict[key].to(DEVICE, non_blocking=True)
tepoch.set_description(f"Epoch {epoch + 1}")
optimizer.zero_grad()
output_dict = model(input_dict)
loss = output_dict["loss"]
acc = output_dict["acc"]
mean_loss += loss.item()
mean_acc += acc.item()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 3.0)
optimizer.step()
tepoch.set_postfix(acc=acc.item())
mean_loss /= len(train_loader)
mean_acc /= len(train_loader)
loss_history.append(mean_loss)
acc_history.append(mean_acc)
mean_loss = 0.0
mean_acc = 0.0
# 验证阶段
model.eval() # 将模型设置为评估模式
with torch.no_grad(): # 确保不会计算梯度,因为我们不在验证集上训练
for input_dict in valid_loader:
for key in input_dict.keys():
if isinstance(input_dict[key], torch.Tensor):
input_dict[key] = input_dict[key].to(DEVICE, non_blocking=True)
output_dict = model(input_dict)
loss = output_dict["loss"]
acc = output_dict["acc"]
mean_loss += loss.item()
mean_acc += acc.item()
# 计算测试集上的平均损失和准确率
mean_loss /= len(valid_loader)
mean_acc /= len(valid_loader)
# 打印测试结果
# with open(rf"C:\Users\hp\Desktop\Sentence-recognition-based-on-mpu6500\Sentence-recognition-based-on-mpu6500\results\loss&acc\{rand_seed}.txt", 'w') as file:
file.write(f"Test Epoch {epoch + 1}, Loss: {mean_loss:.4f}, Accuracy: {mean_acc:.4f}\n")
file.flush()
print(f"Test Epoch {epoch + 1}, Loss: {mean_loss:.4f}, Accuracy: {mean_acc:.4f}")
mean_loss = np.zeros((1, 21))
mean_acc = np.zeros((1, 21))
loss_total = 0.0
acc_total = 0.0
count_length = np.zeros((1, 21))
# 测试阶段
model.eval() # 将模型设置为评估模式
with torch.no_grad(): # 确保不会计算梯度,因为我们不在测试集上训练
for input_dict in test_loader:
for key in input_dict.keys():
if isinstance(input_dict[key], torch.Tensor):
input_dict[key] = input_dict[key].to(DEVICE, non_blocking=True)
output_dict = model(input_dict)
loss = output_dict["loss"]
acc = output_dict["acc"]
length = output_dict["length"]
mean_loss[0][length] += loss.item()
loss_total += loss.item()
mean_acc[0][length] += acc.item()
acc_total += acc.item()
count_length[0][length] += 1
# with open(rf"C:\Users\hp\Desktop\Sentence-recognition-based-on-mpu6500\Sentence-recognition-based-on-mpu6500\results\loss&acc\{rand_seed}.txt", 'w') as file:
# 计算测试集上的平均损失和准确率
for i in range(21):
mean_loss[0][i] /= count_length[0][i]
mean_acc[0][i] /= count_length[0][i]
# 打印保存测试结果
# file.write(f"Test on Length {i}, Loss: {mean_loss[0][i]:.4f}, Accuracy: {mean_acc[0][i]:.4f}\n")
# file.flush()
print(f"Test on Length {i}, Loss: {mean_loss[0][i]:.4f}, Accuracy: {mean_acc[0][i]:.4f}")
loss_total /= len(test_loader)
acc_total /= len(test_loader)
file.write(f"Test Result, Loss: {loss_total:.4f}, Accuracy: {acc_total:.4f}\n")
file.flush()
print(f"Test Result, Loss: {loss_total:.4f}, Accuracy: {acc_total:.4f}")
torch.save(model.state_dict(), MODEL_PATH)
torch.save(loss_history, "loss.pt")
torch.save(acc_history, "acc.pt")