-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
49 lines (38 loc) · 1.3 KB
/
Main.py
File metadata and controls
49 lines (38 loc) · 1.3 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
from transformers import AdamW
import torch
# EPOCH = 0
# LR = 0
# device = torch.device("cuda")
def run(model, traindataloader):
loss = torch.nn.CrossEntropyLoss().to(device)
optimizer = AdamW(params=model.parameters(), lr=LR, eps=1e-8)
for it in range(EPOCH):
print("Epoch:", it)
for pos in traindataloader:
x = pos[0].to(device)
y = pos[1].to(device).unsqueeze(0)
label = torch.tensor([pos[2]]).long().to(device)
output = model(x, y).to(device)
# 计算损失值
los = loss(output, label)
optimizer.zero_grad()
los.backward()
optimizer.step()
with torch.no_grad():
test(model)
return model
def test(model, testdataloader):
Ping = ConfusionMatrix(7, list(range(7)))
true_ = []
pred_ = []
for pos in testdataloader:
x = pos[0].to(device)
y = pos[1].to(device).unsqueeze(0)
label = torch.tensor([pos[2]]).long().to(device)
output = model(x, y).to(device)
output = torch.nn.Softmax(dim=1)(output)
pred = torch.max(output, 1)[1]
true_.append(label.item())
pred_.append(pred.item())
Ping.update(pred_, true_)
Ping.summary()