-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask.lua
More file actions
62 lines (53 loc) · 1.63 KB
/
task.lua
File metadata and controls
62 lines (53 loc) · 1.63 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local Task = {}
Task.start_token_val = 1
Task.sep_token_val = 2
Task.end_token_val = 3
function Task.generateSequence(make_even, is_test, opt)
local min_len = is_test and opt.min_test_seq_len or opt.min_train_seq_len
local max_len = is_test and opt.max_test_seq_len or opt.max_train_seq_len
local seq_len = torch.floor(torch.uniform(min_len, max_len + 1))
if make_even and seq_len % 2 == 1 then
seq_len = seq_len + 1
end
local x = torch.rand(opt.batch_size, seq_len)
:mul(opt.vocab_size - Task.end_token_val)
:floor()
:add(Task.end_token_val + 1)
return x, seq_len
end
function Task.copy(x, index)
return x:select(2, index)
end
function Task.reverse(x, index)
local seq_len = x:size(2)
return x:select(2, seq_len - index + 1)
end
function Task.bigramFlip(x, index)
if index % 2 == 1 then
return x:select(2, index + 1)
else
return x:select(2, index - 1)
end
end
function Task.startToken(opt)
if Task.start_token_tensor == nil then
Task.start_token_tensor
= torch.Tensor(opt.batch_size):fill(Task.start_token_val)
end
return Task.start_token_tensor
end
function Task.sepToken(opt)
if Task.sep_token_tensor == nil then
Task.sep_token_tensor =
torch.Tensor(opt.batch_size):fill(Task.sep_token_val)
end
return Task.sep_token_tensor
end
function Task.endToken(opt)
if Task.end_token_tensor == nil then
Task.end_token_tensor
= torch.Tensor(opt.batch_size):fill(Task.end_token_val)
end
return Task.end_token_tensor
end
return Task