-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtraining.py
60 lines (46 loc) · 1.57 KB
/
training.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
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 14 17:50:00 2019
@author: HSU, CHIH-CHAO
"""
#try to use nn for crossentropy
import torch
import torch.nn.functional as F
#%% Training the Model
def train(m, device, train_itr, optimizer, epoch, max_epoch):
m.train()
corrects, train_loss = 0.0,0
for batch in train_itr:
text, target = batch.text, batch.label
text = torch.transpose(text,0, 1)
target.data.sub_(1)
text, target = text.to(device), target.to(device)
optimizer.zero_grad()
logit = m(text)
loss = F.cross_entropy(logit, target)
loss.backward()
optimizer.step()
train_loss+= loss.item()
result = torch.max(logit,1)[1]
corrects += (result.view(target.size()).data == target.data).sum()
size = len(train_itr.dataset)
train_loss /= size
accuracy = 100.0 * corrects/size
return train_loss, accuracy
def valid(m, device, test_itr):
m.eval()
corrects, test_loss = 0.0,0
for batch in test_itr:
text, target = batch.text, batch.label
text = torch.transpose(text,0, 1)
target.data.sub_(1)
text, target = text.to(device), target.to(device)
logit = m(text)
loss = F.cross_entropy(logit, target)
test_loss += loss.item()
result = torch.max(logit,1)[1]
corrects += (result.view(target.size()).data == target.data).sum()
size = len(test_itr.dataset)
test_loss /= size
accuracy = 100.0 * corrects/size
return test_loss, accuracy