aboutsummaryrefslogtreecommitdiff
path: root/nerv/examples/lmptb/sample_grulm_ptb_main.lua
blob: 86209da60ee36c9a25916f93dc6882ce573a2b52 (plain) (blame)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
require 'lmptb.lmvocab'
require 'lmptb.lmfeeder'
require 'lmptb.lmutil'
require 'lmptb.layer.init'
--require 'tnn.init'
require 'lmptb.lmseqreader'
require 'lm_trainer'

--[[global function rename]]--
--local printf = nerv.printf
local LMTrainer = nerv.LMTrainer
--[[global function rename ends]]--

function prepare_parameters(global_conf, fn)
    nerv.printf("%s preparing parameters...\n", global_conf.sche_log_pre) 
    
    global_conf.paramRepo = nerv.ParamRepo()
    local paramRepo = global_conf.paramRepo

    nerv.printf("%s loading parameter from file %s...\n", global_conf.sche_log_pre, fn) 
    paramRepo:import({fn}, nil, global_conf)

    nerv.printf("%s preparing parameters end.\n", global_conf.sche_log_pre)

    return nil
end

--global_conf: table
--Returns: nerv.LayerRepo
function prepare_layers(global_conf)
    nerv.printf("%s preparing layers...\n", global_conf.sche_log_pre)
    
    local pr = global_conf.paramRepo

    local du = false

    --local recurrentLconfig = {{["bp"] = "bp_h", ["ltp_hh"] = "ltp_hh"}, {["dim_in"] = {global_conf.hidden_size, global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}, ["break_id"] = global_conf.vocab:get_sen_entry().id, ["independent"] = global_conf.independent, ["clip"] = 10}}
    --local recurrentLconfig = {{}, {["dim_in"] = {global_conf.hidden_size, global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}, ["clip"] = 10, ["direct_update"] = du, ["pr"] = pr}}

    local layers = {
        ["nerv.GRULayerT"] = {
            ["gruL1"] = {{}, {["dim_in"] = {global_conf.hidden_size, global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}, ["pr"] = pr}}, 
        },
        
        ["nerv.DropoutLayerT"] = {
            ["dropoutL1"] = {{}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}}}, 
        },

        ["nerv.SelectLinearLayer"] = {
            ["selectL1"] = {{}, {["dim_in"] = {1}, ["dim_out"] = {global_conf.hidden_size}, ["vocab"] = global_conf.vocab, ["pr"] = pr}},
        },
        
        ["nerv.CombinerLayer"] = {
            ["combinerL1"] = {{}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size, global_conf.hidden_size}, ["lambda"] = {1}}},
        },

        ["nerv.AffineLayer"] = {
            ["outputL"] = {{}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.vocab:size()}, ["direct_update"] = du, ["pr"] = pr}},
        },

        ["nerv.SoftmaxCELayerT"] = {
            ["softmaxL"] = {{}, {["dim_in"] = {global_conf.vocab:size(), global_conf.vocab:size()}, ["dim_out"] = {1}}},
        },
    }
   
    for l = 2, global_conf.layer_num do 
        layers["nerv.DropoutLayerT"]["dropoutL" .. l] = {{}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}}}
        layers["nerv.GRULayerT"]["gruL" .. l] = {{}, {["dim_in"] = {global_conf.hidden_size, global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}, ["pr"] = pr}}
        layers["nerv.CombinerLayer"]["combinerL" .. l] = {{}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size, global_conf.hidden_size}, ["lambda"] = {1}}}
    end
    --[[ --we do not need those in the new tnn framework
    printf("%s adding %d bptt layers...\n", global_conf.sche_log_pre, global_conf.bptt)
    for i = 1, global_conf.bptt do
        layers["nerv.IndRecurrentLayer"]["recurrentL" .. (i + 1)] = recurrentLconfig 
        layers["nerv.SigmoidLayer"]["sigmoidL" .. (i + 1)] = {{}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}}}
        layers["nerv.SelectLinearLayer"]["selectL" .. (i + 1)] = {{["ltp"] = "ltp_ih"}, {["dim_in"] = {1}, ["dim_out"] = {global_conf.hidden_size}}}
    end
    --]]

    local layerRepo = nerv.LayerRepo(layers, pr, global_conf)
    nerv.printf("%s preparing layers end.\n", global_conf.sche_log_pre)
    return layerRepo
end

--global_conf: table
--layerRepo: nerv.LayerRepo
--Returns: a nerv.TNN
function prepare_tnn(global_conf, layerRepo)
    nerv.printf("%s Generate and initing TNN ...\n", global_conf.sche_log_pre)

    --input: input_w, input_w, ... input_w_now, last_activation
    local connections_t = {
        {"<input>[1]", "selectL1[1]", 0},
        
        --{"selectL1[1]", "recurrentL1[1]", 0},  
        --{"recurrentL1[1]", "sigmoidL1[1]", 0},
        --{"sigmoidL1[1]", "combinerL1[1]", 0},
        --{"combinerL1[1]", "recurrentL1[2]", 1},
        
        {"selectL1[1]", "gruL1[1]", 0},
        {"gruL1[1]", "combinerL1[1]", 0},
        {"combinerL1[1]", "gruL1[2]", 1},
        {"combinerL1[2]", "dropoutL1[1]", 0},
        
        {"dropoutL"..global_conf.layer_num.."[1]", "outputL[1]", 0},
        {"outputL[1]", "softmaxL[1]", 0},
        {"<input>[2]", "softmaxL[2]", 0},
        {"softmaxL[1]", "<output>[1]", 0}
    }

    for l = 2, global_conf.layer_num do
        table.insert(connections_t, {"dropoutL"..(l-1).."[1]", "gruL"..l.."[1]", 0})
        table.insert(connections_t, {"gruL"..l.."[1]", "combinerL"..l.."[1]", 0})
        table.insert(connections_t, {"combinerL"..l.."[1]", "gruL"..l.."[2]", 1})
        table.insert(connections_t, {"combinerL"..l.."[2]", "dropoutL"..l.."[1]", 0})
    end

    --[[
    printf("%s printing DAG connections:\n", global_conf.sche_log_pre)
    for key, value in pairs(connections_t) do
        printf("\t%s->%s\n", key, value)
    end
    ]]--

    local tnn = nerv.TNN("TNN", global_conf, {["dim_in"] = {1, global_conf.vocab:size()}, 
            ["dim_out"] = {1}, ["sub_layers"] = layerRepo,
            ["connections"] = connections_t, ["clip_t"] = global_conf.clip_t,
        })

    tnn:init(global_conf.batch_size, global_conf.chunk_size)

    nerv.printf("%s Initing TNN end.\n", global_conf.sche_log_pre)
    return tnn
end

function prepare_dagL(global_conf, layerRepo)
    nerv.printf("%s Generate and initing TNN ...\n", global_conf.sche_log_pre)

    --input: input_w, input_w, ... input_w_now, last_activation
    local connections_t = {
        ["<input>[1]"] = "selectL1[1]",
       
        ["selectL1[1]"] = "gruL1[1]",
        ["gruL1[1]"] = "combinerL1[1]",
        ["<input>[2]"] = "gruL1[2]",
        --{"combinerL1[2]", "dropoutL1[1]", 0},
        
        ["combinerL" .. global_conf.layer_num .. "[1]"] = "outputL[1]",
        ["outputL[1]"] = "<output>[1]",     
        ["combinerL1[2]"] = "<output>[2]",
    }
    
    if global_conf.layer_num > 1 then
        nerv.error("multiple layer is currently not supported(not hard to implement though)")
    end
    --[[
    for l = 2, global_conf.layer_num do
        table.insert(connections_t, {"dropoutL"..(l-1).."[1]", "gruL"..l.."[1]", 0})
        table.insert(connections_t, {"gruL"..l.."[1]", "combinerL"..l.."[1]", 0})
        table.insert(connections_t, {"combinerL"..l.."[1]", "gruL"..l.."[2]", 1})
        table.insert(connections_t, {"combinerL"..l.."[2]", "dropoutL"..l.."[1]", 0})
    end
    ]]--

    --[[
    printf("%s printing DAG connections:\n", global_conf.sche_log_pre)
    for key, value in pairs(connections_t) do
        printf("\t%s->%s\n", key, value)
    end
    ]]--

    local dagL = nerv.DAGLayerT("dagL", global_conf, {["dim_in"] = {1, global_conf.hidden_size}, 
            ["dim_out"] = {global_conf.vocab:size(), global_conf.hidden_size}, ["sub_layers"] = layerRepo,
            ["connections"] = connections_t
        })

    dagL:init(global_conf.batch_size)

    nerv.printf("%s Initing DAGL end.\n", global_conf.sche_log_pre)
    return tnn
end

function load_net_tnn(global_conf, fn)
    prepare_parameters(global_conf, fn)
    local layerRepo = prepare_layers(global_conf)
    local tnn = prepare_tnn(global_conf, layerRepo)
    return tnn
end

function load_net_dagL(global_conf, fn)
    prepare_parameters(global_conf, fn)
    local layerRepo = prepare_layers(global_conf)
    local tnn = prepare_dagL(global_conf, layerRepo)
    return tnn
end

local train_fn, valid_fn, test_fn
global_conf = {}
local set = arg[1] --"test"

root_dir = '/home/slhome/txh18/workspace'

if (set == "ptb") then

data_dir = root_dir .. '/ptb/DATA'
train_fn = data_dir .. '/ptb.train.txt.adds'
valid_fn = data_dir .. '/ptb.valid.txt.adds'
test_fn = data_dir .. '/ptb.test.txt.adds'
vocab_fn = data_dir .. '/vocab'

qdata_dir = root_dir .. '/ptb/questionGen/gen'

global_conf = {
    lrate = 0.15, wcost = 1e-5, momentum = 0, clip_t = 5,
    cumat_type = nerv.CuMatrixFloat,
    mmat_type = nerv.MMatrixFloat,
    nn_act_default = 0, 

    hidden_size = 300,
    layer_num = 1,
    chunk_size = 15,
    batch_size = 32, 
    max_iter = 35,
    lr_decay = 1.003,
    decay_iter = 10,
    param_random = function() return (math.random() / 5 - 0.1) end,
    dropout_str = "0.5",

    train_fn = train_fn,
    valid_fn = valid_fn,
    test_fn = test_fn,
    vocab_fn = vocab_fn,
    max_sen_len = 90,
    sche_log_pre = "[SCHEDULER]:",
    log_w_num = 40000, --give a message when log_w_num words have been processed
    timer = nerv.Timer(),
    work_dir_base = root_dir .. '/ptb/EXP-nerv/grulm_v1.0',
    
    fn_to_sample = root_dir .. '/ptb/EXP-nerv/grulm_v1.0h300l1ch15ba32slr0.15wc1e-05dr0.5/params.final',
}

elseif (set == "msr_sc") then

data_dir = '/home/slhome/txh18/workspace/sentenceCompletion/DATA_PV2'
train_fn = data_dir .. '/normed_all.sf.len60.adds.train'
valid_fn = data_dir .. '/normed_all.sf.len60.adds.dev'
test_fn = data_dir .. '/answer_normed.adds'
vocab_fn = data_dir .. '/normed_all.choose.vocab30000.addqvocab'

global_conf = {
    lrate = 1, wcost = 1e-6, momentum = 0,
    cumat_type = nerv.CuMatrixFloat,
    mmat_type = nerv.MMatrixFloat,
    nn_act_default = 0, 

    hidden_size = 300,
    layer_num = 1,
    chunk_size = 15,
    batch_size = 10, 
    max_iter = 30,
    decay_iter = 10,
    lr_decay = 1.003,
    param_random = function() return (math.random() / 5 - 0.1) end,
    dropout_str = "0",

    train_fn = train_fn,
    valid_fn = valid_fn,
    test_fn = test_fn,
    vocab_fn = vocab_fn,
    sche_log_pre = "[SCHEDULER]:",
    log_w_num = 400000, --give a message when log_w_num words have been processed
    timer = nerv.Timer(),
    work_dir_base = '/home/slhome/txh18/workspace/sentenceCompletion/EXP-Nerv/rnnlm_test'
}

elseif (set == "twitter") then

data_dir = root_dir .. '/twitter_new/DATA'
train_fn = data_dir .. '/twitter.choose2.adds'
valid_fn = data_dir .. '/twitter.valid.adds'
test_fn = data_dir .. '/comm.test.choose-ppl.adds'
vocab_fn = data_dir .. '/twitter.choose.train.vocab'

--qdata_dir = root_dir .. '/ptb/questionGen/gen'

global_conf = {
    lrate = 0.15