-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest.py
48 lines (34 loc) · 1.35 KB
/
test.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
import unittest
import torch
from decoder import MoChADecoder
class MoChATest(unittest.TestCase):
def setUp(self):
self.batch_size = 5
self.sequence_length = 40
self.chunk_size = 3
self.dim = 10
self.vocab_size = 100
def test_soft(self):
"""Soft Monotonic Chunkwise Attention"""
enc_outputs = torch.Tensor(
self.batch_size, self.sequence_length, self.dim).normal_()
dec_inputs = torch.LongTensor(
self.batch_size, self.sequence_length).clamp_(min=0, max=self.vocab_size - 1)
decoder = MoChADecoder(vocab_size=self.vocab_size, chunk_size=self.chunk_size)
if torch.cuda.is_available():
enc_outputs = enc_outputs.cuda()
dec_inputs = dec_inputs.cuda()
decoder = decoder.cuda()
decoder.forward_train(enc_outputs, dec_inputs)
def test_hard(self):
"""Hard Monotonic Chunkwise Attention"""
enc_outputs = torch.Tensor(
self.batch_size, self.sequence_length, self.dim).normal_()
decoder = MoChADecoder(
vocab_size=self.vocab_size, chunk_size=self.chunk_size)
if torch.cuda.is_available():
enc_outputs = enc_outputs.cuda()
decoder = decoder.cuda()
decoder.forward_test(enc_outputs)
if __name__ == '__main__':
unittest.main()