forked from karpathy/char-rnn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
export_to_recurrentjs.lua
264 lines (208 loc) · 8.87 KB
/
export_to_recurrentjs.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
--[[
This file reads a model trained using train.lua in this repo
and exports it in a JSON format compatible with RecurrentJS
so that it can be run by a browser.
]] --
-- simple script that loads a checkpoint and prints its opts
--require('mobdebug').start() -- Uncomment this line if you want to debug in terminal or in zbs-studio
require 'torch'
require 'nn'
require 'nngraph'
require 'io'
require 'util.OneHot'
require 'util.misc'
local ok, cunn = pcall(require, 'cunn')
local ok2, cutorch = pcall(require, 'cutorch')
function createWeightsTable(cudaTensor)
local thistable = {}
doubleTensor = cudaTensor:double()
thistable.n = doubleTensor:size(1)
thistable.d = doubleTensor:size(2)
thistable.w = {}
for i = 1, doubleTensor:size(1) do
for j = 1,doubleTensor:size(2) do
indexInt = ((i-1) * doubleTensor:size(2)) + j - 1
indexStr = tostring(indexInt)
thistable.w[indexStr] = doubleTensor[i][j]
end
end
return thistable
end
function createBiasTable(cudaTensor)
local thistable = {}
doubleTensor = cudaTensor:double()
thistable.n = doubleTensor:size(1)
thistable.d = 1
thistable.w = {}
for i = 1, doubleTensor:size(1) do
thistable.w[i-1] = doubleTensor[i]
end
return thistable
end
-- This function escapes each entry and creates a 0-indexed vocabulary
function escapeVocab(vocab)
escapedvocab = {}
local inspect = require 'inspect'
for key, val in pairs(vocab) do
escapedkey = fixUTF8(inspect(key), "Invalid")
if (not string.find(escapedkey, "Invalid")) then
escapedvocab[escapedkey] = val - 1 -- making it 0-indexed
end
end
return escapedvocab
end
function invertTable(vocab)
t = {}
for k, v in pairs(vocab) do
escapedk = string.sub(k, 2, #k-1)
strval = tostring(v)
t[strval] = escapedk
end
return t
end
function getKeys(vocab)
t = {}
for k, v in pairs(vocab) do
table.insert(t, k)
end
return t
end
function fixUTF8(s, replacement)
local p, len, invalid = 1, #s, {}
while p <= len do
if p == s:find("[%z\1-\127]", p) then p = p + 1
elseif p == s:find("[\194-\223][\128-\191]", p) then p = p + 2
elseif p == s:find( "\224[\160-\191][\128-\191]", p)
or p == s:find("[\225-\236][\128-\191][\128-\191]", p)
or p == s:find( "\237[\128-\159][\128-\191]", p)
or p == s:find("[\238-\239][\128-\191][\128-\191]", p) then p = p + 3
elseif p == s:find( "\240[\144-\191][\128-\191][\128-\191]", p)
or p == s:find("[\241-\243][\128-\191][\128-\191][\128-\191]", p)
or p == s:find( "\244[\128-\143][\128-\191][\128-\191]", p) then p = p + 4
else
s = s:sub(1, p-1)..replacement..s:sub(p+1)
table.insert(invalid, p)
end
end
return s, invalid
end
-- Yeah, turns out LUA needs this
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
json = require("json")
path = "examples/PaulGraham128"
local model = torch.load(path .. ".t7") -- Given we are still doing development, this is currently fixed
rnn = model.protos.rnn
-- json.encode would work well with a big table with all the weights.
-- Unfortunately, LUAJit objects can't be bigger than 1 GB even in x64 systems
-- so we need to be creative and stream vectors in output instead.
-- This function helps do just that.
function streamWriteWeightsTable(fileDescriptor, tableName, table)
fileDescriptor:write('"'.. tableName .. '":')
fileDescriptor:write(json.encode(table))
end
--print(AllModelWeights)
-- ConvNetJS is more flexible than this Torch code because it allows different layers to be of different size.
-- This does not support it, so we just iterate through each layer and copy the same layer size in the JSON.
hiddenSizes = {}
for i = 1, model.opt.num_layers do
table.insert(hiddenSizes, model.opt.rnn_size)
end
fho,err = io.open(path .. ".json","w")
modelstr = json.encode(AllModelWeights)
modelstr = modelstr:gsub("\\[", ""):gsub("\\]", "")
vocab = escapeVocab(model.vocab)
mymodel = {}
mymodel.generator = model.opt.model
--mymodel.model = AllModelWeights
mymodel.letterToIndex = vocab
mymodel.indexToLetter = invertTable(vocab)
mymodel.vocab = getKeys(vocab)
mymodel.hidden_sizes = hiddenSizes
mymodel.letter_size = tablelength(model.vocab) -- Size of the embeddings for RecurrentJS smaller than the vocab in input. For us it's not.
mymodel.solver = {}
mymodel.solver["decay_rate"] = 0.999 -- RecurrentJS needs these, even though most people won't be training there anyway.
mymodel.solver["smooth_eps"] = 1E-8
mymodelStr = json.encode(mymodel):gsub("\\\"", ""):gsub("\\'", "")
mymodelStr = mymodelStr:sub(1, #mymodelStr-1) -- Remove last closing bracket
mymodelStr = mymodelStr .. ',"model":{'
fho:write(mymodelStr)
Biases = {} -- We can afford to store biases
-- The way weights are stored is simple:
-- Each layer has 2 nn.Linear() layers with the weights from input to hidden
-- and hidden to hidden, respectively. Each of these 2 matrices has size
-- [layer_input, 4 * rnn_size]. Layer_input is of size [Voc] for the first layer,
-- and of size rnn_size for all other layers.
-- The quadruple size comes from the fact that 4 matrices are concatenated there.
-- In order, these are the weights for:
-- - input gate,
-- - forget gate,
-- - output gate,
-- - new memory cell
-----------------------------------------------------------------------
-------------- The following region handles weight matrices
local LinearModules = rnn:findModules("nn.Linear")
for i,linearmodule in ipairs(LinearModules) do
if (i == 1) then
streamWriteWeightsTable(fho, "Wil", createWeightsTable(torch.eye(linearmodule.weight:size(2))) )
fho:write(',')
end -- if n == 1 we add this, but also add x0
if (i == #LinearModules) then
streamWriteWeightsTable(fho, "Whd", createWeightsTable(linearmodule.weight) )
fho:write(',')
Biases["bd"] = linearmodule.bias
else
local W = torch.reshape(linearmodule.weight, 4, linearmodule.weight:size(1) / 4, linearmodule.weight:size(2)) -- These are all packed and need unpacking into i, f, o, g. The last gate, g, is called c in the new format.
local B = torch.reshape(linearmodule.bias, 4, linearmodule.bias:size(1) / 4)
if(i % 2 == 1) then
streamWriteWeightsTable(fho, "Wix" .. (i-1)/2, createWeightsTable(W[1]) )
fho:write(',')
Biases["bix".. (i-1)/2] = B[1]
streamWriteWeightsTable(fho, "Wfx" .. (i-1)/2, createWeightsTable(W[2]) )
fho:write(',')
Biases["bfx".. (i-1)/2] = B[2]
streamWriteWeightsTable(fho, "Wox" .. (i-1)/2, createWeightsTable(W[3]) )
fho:write(',')
Biases["box".. (i-1)/2] = B[3]
streamWriteWeightsTable(fho, "Wcx" .. (i-1)/2, createWeightsTable(W[4]) )
fho:write(',')
Biases["bcx".. (i-1)/2] = B[4]
print("Wx" .. (i-1)/2)
else
streamWriteWeightsTable(fho, "Wih" .. math.floor((i-1)/2), createWeightsTable(W[1]) )
fho:write(',')
Biases["bih" .. math.floor((i-1)/2)] = B[1]
streamWriteWeightsTable(fho, "Wfh" .. math.floor((i-1)/2), createWeightsTable(W[2]) )
fho:write(',')
Biases["bfh" .. math.floor((i-1)/2)] = B[2]
streamWriteWeightsTable(fho, "Woh" .. math.floor((i-1)/2), createWeightsTable(W[3]) )
fho:write(',')
Biases["boh" .. math.floor((i-1)/2)] = B[3]
streamWriteWeightsTable(fho, "Wch" .. math.floor((i-1)/2), createWeightsTable(W[4]) )
fho:write(',')
Biases["bch" .. math.floor((i-1)/2)] = B[4]
print("Wh" .. math.floor((i-1)/2))
end
end
end
-----------------------------------------------------------------
--The following region handles biases (for each gate, we have to sum up the contribution coming from x with that coming from h)
--Some printing (leaving it here to help development)
for i=0,model.opt.num_layers-1 do -- Recall that RecurrentJS is 0-indexed
streamWriteWeightsTable(fho, "bi" .. i, createBiasTable( Biases["bix" .. i] + Biases["bih" .. i] ) )
fho:write(',')
streamWriteWeightsTable(fho, "bf" .. i, createBiasTable( Biases["bfx" .. i] + Biases["bfh" .. i] ) )
fho:write(',')
streamWriteWeightsTable(fho, "bo" .. i, createBiasTable( Biases["box" .. i] + Biases["boh" .. i] ) )
fho:write(',')
streamWriteWeightsTable(fho, "bc" .. i, createBiasTable( Biases["bcx" .. i] + Biases["bch" .. i] ) )
fho:write(',')
end
streamWriteWeightsTable(fho, "bd", createBiasTable(Biases["bd"]) )
-- No commas for the last one!
fho:write("}}")
fho:flush()
fho:close()