-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.lua
178 lines (143 loc) · 4.42 KB
/
test2.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
require 'torch'
require 'nn'
require 'optim'
require 'LanguageModel'
require 'util.DataLoader'
-- /usr/cs/bin/th train.lua -input_h5 data/catjava.h5 -input_json data/catjava.json -resume 169000
torch.setdefaulttensortype('torch.FloatTensor')
local utils = require 'util.utils'
local unpack = unpack or table.unpack
local cmd = torch.CmdLine()
cmd:option('-rnn_model', 'global')
-- Dataset options
-- cmd:option('-input_h5', 'data/global_lm.h5')
-- cmd:option('-input_json', 'data/global_lm.json')
cmd:option('-batch_size', 1)
cmd:option('-seq_length', 50)
-- Model options
cmd:option('-model_type', 'lstm')
cmd:option('-wordvec_size', 64)
cmd:option('-rnn_size', 128)
cmd:option('-num_layers', 2)
cmd:option('-dropout', 0)
cmd:option('-batchnorm', 0)
-- Output options
cmd:option('-checkpoint_name', 'cv/global/checkpoint')
-- Backend options
cmd:option('-gpu', 0)
cmd:option('-gpu_backend', 'cuda')
local opt = cmd:parse(arg)
opt.input_h5 = 'data/global_lm.h5'
opt.input_json = 'data/global_lm.json'
global_checkpoint = torch.load('./cv/global/checkpoint_251000.t7')
buggy_checkpoint = torch.load('./cv/buggy/checkpoint_320000.t7')
global_model = global_checkpoint.model
buggy_model = buggy_checkpoint.model
-- Initialize the DataLoader and vocabulary
-- local loader = DataLoader(opt)
local vocab = utils.read_json(opt.input_json)
local idx_to_token = {}
local token_to_idx = {}
for k, v in pairs(vocab.idx_to_token) do
idx_to_token[tonumber(k)] = v
token_to_idx[v] = tonumber(k)
end
-- 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))
else
-- Memory benchmarking is only supported in CUDA mode
opt.memory_benchmark = 0
print 'Running in CPU mode'
end
-- Initialize the model and criterion
-- local opt_clone = torch.deserialize(torch.serialize(opt))
-- opt_clone.idx_to_token = idx_to_token
-- local model = nn.LanguageModel(opt_clone):type(dtype)
-- Set up some variables we will use below
local N, T = opt.batch_size, opt.seq_length
global_model:evaluate()
global_model:resetStates()
buggy_model:evaluate()
buggy_model:resetStates()
function log2(log_in)
return torch.log(log_in)/torch.log(2)
end
results = {}
results[1] = {}
results[1]['rnn'] = {}
results[1]['ngram'] = {}
results[0] = {}
results[0]['rnn'] = {}
results[0]['ngram'] = {}
-- line_idx = 0
-- count = 0
-- for line in io.lines("data/testdata.txt") do
-- if (line_idx % 3) == 0 then
-- if #line <= 1 then
-- count = count + 1
-- end
-- end
-- line_idx = line_idx + 1
-- end
-- print(count)
-- os.exit()
lambda = 0.5
line_idx = 0
for line in io.lines("data/testdata.txt") do
if (line_idx % 100) <= 2 then
if (line_idx % 3) == 0 then
if #line > 0 then
xv = torch.Tensor(#line)
for i = 1,#line do
char = string.sub(line,i,i)
if token_to_idx[char] then
xv[i] = token_to_idx[char]
else
xv[i] = 1
end
end
seq_len = xv:size(1)
xv = xv:reshape(1,seq_len)
local global_scores = global_model:forward(xv):view(N * seq_len, -1)
local buggy_scores = buggy_model:forward(xv):view(N * seq_len, -1)
H = 0
for i = 1,global_scores:size(1) do
global_probs = nn.SoftMax():forward(global_scores[i])
buggy_probs = nn.SoftMax():forward(buggy_scores[i])
t1 = global_probs*lambda
t2 = buggy_probs*(1-lambda)
for j = 1,probs:size(1) do
if probs[j] > 0 then
H = H + probs[j]*log2(probs[j])
end
end
end
H = (H*-1)/global_scores:size(1)
else
H = 0
end
elseif (line_idx % 3) == 1 then
ngram_H = tonumber(line)
elseif (line_idx % 3) == 2 then
table.insert(results[tonumber(line)]['rnn'], H)
table.insert(results[tonumber(line)]['ngram'], ngram_H)
end
end
if (line_idx % 5000) == 0 then
print(line_idx)
end
line_idx = line_idx + 1
end
if opt.rnn_model == 'global' then
torch.save('./global_results.t7', results)
elseif opt.rnn_model == 'buggy' then
torch.save('./buggy_results.t7', results)
elseif opt.rnn_model == 'local' then
torch.save('./local_results.t7', results)
end