-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_seq2seq_gru_sorting.py
More file actions
54 lines (31 loc) · 1.18 KB
/
Copy pathrun_seq2seq_gru_sorting.py
File metadata and controls
54 lines (31 loc) · 1.18 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
import torch
import torch.optim as optim
from model.seq2seq_gru import Seq2SeqGRU
from data.sort_data import fixed_batch
if __name__ == "__main__":
input_feature_size = 1
batch_size = 128
seq_len = 8
hidden_size = 512
cuda = torch.device('cuda')
seq2seq = Seq2SeqGRU(1, hidden_size, seq_len).cuda()
optimizer = optim.Adam(seq2seq.parameters())
seq2seq.train()
losses = []
for i in range(10000):
optimizer.zero_grad()
x_batch, y_batch = fixed_batch(batch_size, seq_len)
x_batch = torch.unsqueeze(x_batch, -1).float().cuda()
y_batch = y_batch.cuda()
preds, loss = seq2seq.forward(x_batch, y_batch, 0.5)
losses.append(loss)
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print(f"trainig loss : {sum(losses) / len(losses)}")
x_batch, y_batch = fixed_batch(batch_size, seq_len)
x_batch = torch.unsqueeze(x_batch, -1).float().cuda()
y_batch = y_batch.cuda()
preds, loss = seq2seq.forward(x_batch, y_batch, 0.0)
print(sum(preds.squeeze(2) == y_batch) / len(y_batch))
losses = []