forked from jfsantos/seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainUtils.lua
More file actions
215 lines (200 loc) · 5.18 KB
/
TrainUtils.lua
File metadata and controls
215 lines (200 loc) · 5.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require 'nn';
require 'nngraph';
local orthogonalize = function(m)
if m.weight and m.bias then
local w = torch.cat(m.weight,torch.view(m.bias,m.bias:size(1),1))
if w:size(1) < w:size(2) then
q,_ = torch.qr(w:t())
q = q:t()
else
q,_ = torch.qr(w)
end
m.weight:copy(q[{{},{1,m.weight:size(2)}}])
m.bias:copy(q[{{},m.weight:size(2)+1}])
elseif m.weight then
local w = m.weight
if w:size(1) < w:size(2) then
q,_ = torch.qr(w:t())
q = q:t()
else
q,_ = torch.qr(w)
end
m.weight:copy(q)
end
end
local checkOrthogonalization_ = function(m)
local prefix = prefix or ''
--print(prefix .. torch.typename(m))
local w
if m.weight and m.bias then
w = torch.cat(m.weight,torch.view(m.bias,m.bias:size(1),1))
elseif m.weight then
w = m.weight
end
if w then
local check
if w:size(1) > w:size(2) then
check = torch.mm(w:t(),w)
else
check = torch.mm(w,w:t())
end
local n = check:size(1)
--print(check)
local check = check - torch.eye(n)
return check:norm()
end
end
function columnNormConstraint(m,maxval)
local maxval = maxval or 1
if m.weight then
local nan_check = m.weight:norm()
if nan_check ~= nan_check then
print('\nmodule',m)
print('prior to colnorm constraint')
print('nan_check:',nan_check)
__debug_module = m
error('found a nan, module saved to __debug_module')
end
local norm = m.weight:norm(2,2):expandAs(m.weight) + 1e-8
local lt = torch.lt(norm,maxval):type(norm:type())
local ge = torch.ge(norm,maxval):type(norm:type())
local unchanged = torch.ones(norm:size()):type(norm:type()):cmul(lt)
--print(unchanged)
local constrained = torch.cmul(ge,norm):div(maxval)
--print(constrained)
local div = unchanged + constrained
local nan_check = div:norm()
if nan_check ~= nan_check then
print('\nmodule',m)
print('during colnorm constraint')
print('nan_check:',nan_check)
__debug_module = m
error('found a nan, module saved to __debug_module')
end
if div:eq(0):any() then
print('\nmodule',m)
print('zeros in divisor during colnorm constraint')
__debug_module = m
error('found a nan, module saved to __debug_module')
end
m.weight:cdiv(div)
local nan_check = m.weight:norm()
if nan_check ~= nan_check then
print('\nmodule:',m)
print('norm:',norm:norm())
print('nan_check:',nan_check)
__debug_module = m
error('found a nan, module saved to __debug_module')
end
--print(m.weight:norm(2,1))
end
--[[
if m.bias then
local norm = m.bias:norm(2)
if norm > maxval then
m.bias:div(norm)
end
--print(m.bias:norm(2))
end]]
end
function checkColumnNormConstraint(m,maxval)
local maxval = maxval or 1
if m.weight then
print(m.weight:norm(2,2))
end
--[[
if m.bias then
print(m.bias:norm(2))
end]]
end
local getnorms
getnorms = function(t)
if type(t) == 'table' then
local norms = {}
for k,v in pairs(t) do
norms[k] = getnorms(v)
end
return norms
else
return t:norm()
end
end
local checkoutput
checkoutput = function(m)
if m.output then
return getnorms(m.output)
end
end
local apply2graph
apply2graph = function(graph,func,toggleprint,prefix)
local prefix = prefix or ''
local list
local typename = torch.typename(graph) or ''
if typename == 'nn.gModule' then
list = graph.forwardnodes
elseif graph.modules then
list = graph.modules
else
if graph.weight then
if toggleprint then
local printname = typename
if graph.__tostring__ then
printname = graph:__tostring__()
end
print(prefix .. printname)
end
end
local returnval = func(graph)
if returnval then
if toggleprint then
print(prefix,returnval)
end
end
end
if list then
if toggleprint then
local printname = typename
if graph.__tostring__ then
printname = graph:__tostring__()
end
print(prefix .. printname)
end
local prefix = prefix .. ' '
for i,n in pairs(list) do
local m
if torch.typename(n) == 'nngraph.Node' then
m = n.data.module
else
m = n
end
if m ~= nil then
apply2graph(m,func,toggleprint,prefix)
end
end
end
end
local orthogonalizeGraph = function(graph)
apply2graph(graph,orthogonalize)
end
local checkOrthogonalization = function(graph)
apply2graph(graph,checkOrthogonalization_,true)
end
local columnNormConstraintGraph = function(graph)
apply2graph(graph,columnNormConstraint)
end
local checkColumnNormConstraintGraph = function(graph)
apply2graph(graph,checkColumnNormConstraint,true)
end
TrainUtils = {
['orthogonalize'] = orthogonalize,
['orthogonalizeGraph'] = orthogonalizeGraph,
['checkOrthogonalization'] = checkOrthogonalization,
['columnNormConstraint'] = columnNormConstraint,
['columnNormConstraintGraph'] = columnNormConstraintGraph,
['checkColumnNormConstraint'] = checkColumnNormConstraint,
['checkColumnNormConstraintGraph'] = checkColumnNormConstraintGraph,
['apply2graph'] = apply2graph,
['getnorms'] = getnorms,
['checkoutput'] = checkoutput
}
return TrainUtils