forked from namwonss/Math-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifierTrain.py
97 lines (65 loc) · 2.78 KB
/
classifierTrain.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
import torch
import torch.nn as nn
import torch.optim as optim
from bert_pytorch import BERT
from utils import load_raw_math_data, load_vocab, make_weights_for_balanced_classes
from adabelief_pytorch import AdaBelief
from MathDataset import MathDataset
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm
import random as rd
import os
import numpy as np
class MathNet(nn.Module):
def __init__(self, bert, class_num, seq_len, dmodel):
super().__init__()
self.BERT = bert
self.W = nn.Linear(dmodel * seq_len, class_num)
self.class_num = class_num
self.seq_len = seq_len
for param in self.BERT.parameters():
param.requires_grad = False
def forward(self, sequence):
out = self.BERT(sequence)
bsize, slen, dmodel = out.shape
linear_seq = out.reshape(bsize, -1)
classes = self.W(linear_seq)
return classes
if __name__ == "__main__":
if not os.path.exists("./output"):
os.makedirs("./output")
questions, labels = load_raw_math_data("./data/traindata.tsv")
vocab = load_vocab("./data/vocab.txt")
seq_len = 128
number_of_classes = 7
data_len = len(questions)
device = torch.device("cuda:0")
bert = torch.load("./pretrained/bert_trained.model.ep1000")
math_dataset = MathDataset(questions, labels, vocab)
weights = make_weights_for_balanced_classes(labels, number_of_classes)
weights = torch.DoubleTensor(weights)
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, len(weights))
train_dataset = torch.utils.data.DataLoader(math_dataset, batch_size = 128, sampler = sampler, pin_memory=True)
model = MathNet(bert, number_of_classes, 128, 468).to(device)
optimizer = AdaBelief(model.parameters(), lr = 0.0001, eps=1e-8, betas=(0.9, 0.999), weight_decouple = True, rectify = False)
#optimizer = optim.SGD(model.parameters(), lr = 0.0001, weight_decay = 0.5)
criterionA = nn.CrossEntropyLoss()
pbar = tqdm(range(0,300000))
for epoch in pbar:
avg_loss = 0.0
model.train()
# Train
for batch in train_dataset:
questions = batch[0].to(device)
labels = batch[2].to(device)
pred_class = model(questions)
loss = criterionA(pred_class, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
avg_loss += loss.item()
if epoch and epoch % 20 == 0:
print("model save...")
torch.save(model, "./output/classifier_base"+f"_{epoch}"+".pth")
cur_epoch = avg_loss / len(train_dataset)
pbar.set_description("train loss : %f " % cur_epoch)