-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator_train.py
130 lines (100 loc) · 4.16 KB
/
operator_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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from structure.Operator import Operator
from utils.index import translate_sentence, bleu, save_checkpoint, load_checkpoint
from utils.vocab import polish_dict, train_iterator, valid_data, test_data
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
load_model = False
save_model = True
train_model = True
test_model = True
epochs = 50
learning_rate = 1e-4
batch_size = 64
src_vocab_size = len(polish_dict.vocab)
trg_vocab_size = len(polish_dict.vocab)
embedding_size = 516
num_heads = 12
num_encoder_layers = 8
num_decoder_layers = 8
dropout = 0.10
max_len = 128
forward_expansion = 4
src_pad_idx = polish_dict.vocab.stoi["<pad>"]
writer = SummaryWriter("runs/loss_plot")
model = Operator(
embedding_size,
src_vocab_size,
trg_vocab_size,
src_pad_idx,
num_heads,
num_encoder_layers,
num_decoder_layers,
forward_expansion,
dropout,
max_len,
device,
).to(device)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, factor=0.1, patience=10, verbose=True
)
pad_idx = polish_dict.vocab.stoi["<pad>"]
criterion = nn.CrossEntropyLoss(ignore_index=pad_idx)
if load_model:
load_checkpoint(torch.load("network.pth.tar"), model, optimizer)
sentence = "dodaj kolumny gra i gracze do pliku gry o wartościach fnaf minecraft valorant oraz 100 150 120"
step = 0
if train_model:
for epoch in range(epochs):
print(f"[Epoch {epoch} / {epochs}]")
if save_model and epoch % 10 == 0:
checkpoint = {
"state_dict": model.state_dict(),
"optimizer": optimizer.state_dict(),
}
save_checkpoint(checkpoint)
model.eval()
translated_sentence = translate_sentence(
model, sentence, polish_dict, polish_dict, device, max_length=50
)
translated_sentence = " ".join(translated_sentence).replace("<eos>", "").replace("<unk>", "")
print(f"Sentence: {sentence} \nTranslated Sentence: {translated_sentence}")
model.train()
losses = []
for batch_idx, batch in enumerate(train_iterator):
inp_data = batch.src.to(device)
target = batch.trg.to(device)
output = model(inp_data, target[:-1, :])
output = output.reshape(-1, output.shape[2])
target = target[1:].reshape(-1)
optimizer.zero_grad()
loss = criterion(output, target)
losses.append(loss.item())
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1)
optimizer.step()
writer.add_scalar("Training loss", loss, global_step=step)
step += 1
mean_loss = sum(losses) / len(losses)
scheduler.step(mean_loss)
if test_model:
for data in test_data:
question, answer = " ".join(data.src), " ".join(data.trg)
translated_sentence = translate_sentence(
model, answer, polish_dict, polish_dict, device, max_length=50
)
translated_sentence = " ".join(translated_sentence).replace("<eos>", "").replace("<unk>", "")
print(f"Question: {question} | Answer: {translated_sentence} | Right Answer {answer}")
test_score = bleu(test_data[1:100], model, polish_dict, polish_dict, device)
print(f"Bleu score: | Test Data: {test_score}")
# while True:
# sentence = input("Write sentence for translation: ")
# translated_sentence = translate_sentence(
# model, sentence, polish_dict, polish_dict, device, max_length=50
# )
# answer = " ".join(translated_sentence).replace("<eos>", "").replace("<unk>", "")
# print(f"Answer: {answer}")