-
Notifications
You must be signed in to change notification settings - Fork 45
/
utils.lua
48 lines (44 loc) · 926 Bytes
/
utils.lua
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
local utils = {}
local function keepTrack(t, track, entry_fun, fun, ...)
if torch.isTensor(t) and t:storage() then
local ptr = torch.pointer(t:storage())
if not track[ptr] then
track[ptr] = entry_fun(t, ...)
end
if fun then
fun(t,track,...)
end
return
end
if torch.type(t) == 'table' then
for k, v in ipairs(t) do
keepTrack(v, track, entry_fun, fun, ...)
end
end
end
utils.keepTrack = keepTrack
local function recursiveClone(out)
if torch.isTensor(out) then
return out:clone()
else
local res = {}
for k, v in ipairs(out) do
res[k] = recursiveClone(v)
end
return res
end
end
utils.recursiveClone = recursiveClone
local function copyTable(t)
if type(t) == 'table' then
local r = {}
for k, v in pairs(t) do
r[k] = copyTable(v)
end
return r
else
return t
end
end
utils.copyTable = copyTable
return utils