Repository navigation
Expand file tree
/
Copy pathmainUtils.lua
More file actions
71 lines (64 loc) · 2.05 KB
/
Copy pathmainUtils.lua
File metadata and controls
71 lines (64 loc) · 2.05 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
63
64
65
66
67
68
69
70
71
----------------------------------------------------------------------
--
-- Deep time series learning: Analysis of Torch
--
-- Utility functions
--
----------------------------------------------------------------------
function uniqueTable(table)
local hash = {}
local res = {}
for _,v in ipairs(table) do
if (not hash[v]) then
res[#res+1] = v -- you could print here instead of saving to result table if you wanted
hash[v] = true
end
end
return res;
end
function uniqueTensor(table)
local hash = {}
local res = {}
for i = 1,table:size(1) do
if (not hash[table[i]]) then
res[#res+1] = table[i] -- you could print here instead of saving to result table if you wanted
hash[table[i]] = true
end
end
return res;
end
function gradientCheck()
local decGP_est, encGP_est = torch.DoubleTensor(decGradParams:size()), torch.DoubleTensor(encGradParams:size())
-- Easy function to do forward pass over coupled network and get error
function forwardPass()
local encOut = enc:forward(encInSeq)
forwardConnect(encLSTM, decLSTM)
local decOut = dec:forward(decInSeq)
local E = criterion:forward(decOut, decOutSeq)
return E
end
-- Check encoder
for i = 1, encGradParams:size(1) do
-- Forward with \theta+eps
encParams[i] = encParams[i] + eps
local C1 = forwardPass()
-- Forward with \theta-eps
encParams[i] = encParams[i] - 2 * eps
local C2 = forwardPass()
encParams[i] = encParams[i] + eps
encGP_est[i] = (C1 - C2) / (2 * eps)
end
tester:assertTensorEq(encGradParams, encGP_est, eps, "Numerical gradient check for encoder failed")
-- Check decoder
for i = 1, decGradParams:size(1) do
-- Forward with \theta+eps
decParams[i] = decParams[i] + eps
local C1 = forwardPass()
-- Forward with \theta-eps
decParams[i] = decParams[i] - 2 * eps
local C2 = forwardPass()
decParams[i] = decParams[i] + eps
decGP_est[i] = (C1 - C2) / (2 * eps)
end
tester:assertTensorEq(decGradParams, decGP_est, eps, "Numerical gradient check for decoder failed")
end