-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset.lua
99 lines (83 loc) · 2.64 KB
/
reset.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
require 'torch'
require 'nn'
require 'optim'
require 'LanguageModel'
require 'util.DataLoader'
local utils = require 'util.utils'
local unpack = unpack or table.unpack
local cmd = torch.CmdLine()
-- Dataset options
cmd:option('-input_h5', 'data/dialogues.h5')
cmd:option('-input_json', 'data/ascii.json')
cmd:option('-batch_size', 270)
-- cmd:option('-batch_size', 10)
-- cmd:option('-batch_size', 1)
cmd:option('-seq_length', 700)
-- cmd:option('-seq_length', 40)
-- Model options
cmd:option('-init_from', '')
cmd:option('-model_type', 'lstm')
cmd:option('-wordvec_size', 64)
cmd:option('-rnn_size', 1350)
cmd:option('-num_layers', 2)
cmd:option('-dropout', 0.1)
cmd:option('-batchnorm', 1)
-- Optimization options
cmd:option('-max_epochs', 50)
cmd:option('-learning_rate', 1.8e-3)
cmd:option('-grad_clip', 6)
cmd:option('-lr_decay_every', 1)
cmd:option('-lr_decay_factor', 0.9)
-- Output options
cmd:option('-print_every', 1)
cmd:option('-checkpoint_every', 10)
cmd:option('-checkpoint_name', '/nas/doc/nn/checkpoint')
-- Benchmark options
cmd:option('-speed_benchmark', 0)
cmd:option('-memory_benchmark', 0)
-- Backend options
cmd:option('-gpu', -1)
cmd:option('-gpu_backend', 'cuda')
local opt = cmd:parse(arg)
-- Set up GPU stuff
local dtype = 'torch.FloatTensor'
if opt.gpu >= 0 and opt.gpu_backend == 'cuda' then
require 'cutorch'
require 'cunn'
cutorch.setDevice(opt.gpu + 1)
dtype = 'torch.CudaTensor'
print(string.format('Running with CUDA on GPU %d', opt.gpu))
elseif opt.gpu >= 0 and opt.gpu_backend == 'opencl' then
-- Memory benchmarking is only supported in CUDA mode
-- TODO: Time benchmarking is probably wrong in OpenCL mode.
require 'cltorch'
require 'clnn'
cltorch.setDevice(opt.gpu + 1)
dtype = torch.Tensor():cl():type()
print(string.format('Running with OpenCL on GPU %d', opt.gpu))
else
-- Memory benchmarking is only supported in CUDA mode
opt.memory_benchmark = 0
print 'Running in CPU mode'
end
local model = nil
if opt.init_from ~= '' then
print('Initializing from ', opt.init_from)
model = torch.load(opt.init_from).model:type(dtype)
else
assert(false,"No file specified")
end
local params, grad_params = model:getParameters()
print('number of parameters in the model: ' .. params:nElement())
print ('Resetting states')
model:resetStates()
model:clearState()
local checkpoint = {}
-- Now save a torch checkpoint with the model
-- Cast the model to float before saving so it can be used on CPU
model:float()
checkpoint.model = model
local filename = string.format('%s.reset.t7', opt.init_from)
paths.mkdir(paths.dirname(filename))
print ('Saving to',filename)
torch.save(filename, checkpoint)