forked from CC2Vec/CC2Vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfp_cc2ftr_model.py
246 lines (192 loc) · 10.3 KB
/
bfp_cc2ftr_model.py
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
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
# Make the the multiple attention with word vectors.
def attention_mul(rnn_outputs, att_weights):
attn_vectors = None
for i in range(rnn_outputs.size(0)):
h_i = rnn_outputs[i]
a_i = att_weights[i]
h_i = a_i * h_i
h_i = h_i.unsqueeze(0)
if attn_vectors is None:
attn_vectors = h_i
else:
attn_vectors = torch.cat((attn_vectors, h_i), 0)
return torch.sum(attn_vectors, 0).unsqueeze(0)
# The word RNN model for generating a sentence vector
class WordRNN(nn.Module):
def __init__(self, vocab_size, embed_size, batch_size, hidden_size):
super(WordRNN, self).__init__()
self.embed_size = embed_size
self.hidden_size = hidden_size
# Word Encoder
self.embed = nn.Embedding(vocab_size, embed_size)
self.wordRNN = nn.GRU(embed_size, hidden_size, bidirectional=True)
# Word Attention
self.wordattn = nn.Linear(2 * hidden_size, 2 * hidden_size)
self.attn_combine = nn.Linear(2 * hidden_size, 2 * hidden_size, bias=False)
def forward(self, inp, hid_state):
emb_out = self.embed(inp)
out_state, hid_state = self.wordRNN(emb_out, hid_state)
word_annotation = self.wordattn(out_state)
attn = F.softmax(self.attn_combine(word_annotation), dim=1)
sent = attention_mul(out_state, attn)
return sent, hid_state
# The sentence RNN model for generating a hunk vector
class SentRNN(nn.Module):
def __init__(self, sent_size, hidden_size):
super(SentRNN, self).__init__()
# Sentence Encoder
self.sent_size = sent_size
self.sentRNN = nn.GRU(sent_size, hidden_size, bidirectional=True)
# Sentence Attention
self.sentattn = nn.Linear(2 * hidden_size, 2 * hidden_size)
self.attn_combine = nn.Linear(2 * hidden_size, 2 * hidden_size, bias=False)
def forward(self, inp, hid_state):
out_state, hid_state = self.sentRNN(inp, hid_state)
sent_annotation = self.sentattn(out_state)
attn = F.softmax(self.attn_combine(sent_annotation), dim=1)
sent = attention_mul(out_state, attn)
return sent, hid_state
# The hunk RNN model for generating the vector representation for the instance
class HunkRNN(nn.Module):
def __init__(self, hunk_size, hidden_size):
super(HunkRNN, self).__init__()
# Sentence Encoder
self.hunk_size = hunk_size
self.hunkRNN = nn.GRU(hunk_size, hidden_size, bidirectional=True)
# Sentence Attention
self.hunkattn = nn.Linear(2 * hidden_size, 2 * hidden_size)
self.attn_combine = nn.Linear(2 * hidden_size, 2 * hidden_size, bias=False)
def forward(self, inp, hid_state):
out_state, hid_state = self.hunkRNN(inp, hid_state)
hunk_annotation = self.hunkattn(out_state)
attn = F.softmax(self.attn_combine(hunk_annotation), dim=1)
hunk = attention_mul(out_state, attn)
return hunk, hid_state
# The HAN model
class HierachicalRNN(nn.Module):
def __init__(self, args):
super(HierachicalRNN, self).__init__()
self.vocab_size = args.vocab_code
self.batch_size = args.batch_size
self.embed_size = args.embed_size
self.hidden_size = args.hidden_size
self.hidden_units = args.hidden_units
self.cls = args.class_num
self.code_file = args.code_file
self.dropout = nn.Dropout(args.dropout_keep_prob) # drop out
# Word Encoder
self.wordRNN = WordRNN(self.vocab_size, self.embed_size, self.batch_size, self.hidden_size)
# Sentence Encoder
self.sentRNN = SentRNN(self.embed_size, self.hidden_size)
# Hunk Encoder
self.hunkRNN = HunkRNN(self.embed_size, self.hidden_size)
# standard neural network layer
self.standard_nn_layer = nn.Linear(self.embed_size * 2 * self.code_file, self.embed_size)
# neural network tensor
self.W_nn_tensor_one = nn.Linear(self.embed_size * self.code_file, self.embed_size * self.code_file)
self.W_nn_tensor_two = nn.Linear(self.embed_size * self.code_file, self.embed_size * self.code_file)
self.V_nn_tensor = nn.Linear(self.embed_size * 2 * self.code_file, 2)
# Hidden layers before putting to the output layer
self.fc1 = nn.Linear(2 * self.embed_size * self.code_file + self.embed_size + 4, 2 * self.hidden_units)
self.fc2 = nn.Linear(2 * self.hidden_units, self.cls)
self.sigmoid = nn.Sigmoid()
def forward_code(self, x, hid_state):
hid_state_hunk, hid_state_sent, hid_state_word = hid_state
n_batch, n_file, n_hunk, n_line, n_dim = x.shape[0], x.shape[1], x.shape[2], x.shape[3], x.shape[4]
# f: file; i: hunk; j: line; k: batch;
files = list()
for f in range(n_file):
hunks = None
for i in range(n_hunk):
sents = None
for j in range(n_line):
words = list()
for k in range(n_batch):
words.append(x[k][f][i][j])
words = np.array(words)
sent, state_word = self.wordRNN(torch.cuda.LongTensor(words).view(-1, self.batch_size), hid_state_word)
if sents is None:
sents = sent
else:
sents = torch.cat((sents, sent), 0)
hunk, state_sent = self.sentRNN(sents, hid_state_sent)
if hunks is None:
hunks = hunk
else:
hunks = torch.cat((hunks, hunk), 0)
out_hunk, state_hunk = self.hunkRNN(hunks, hid_state_hunk)
files.append(out_hunk)
output = torch.squeeze(torch.cat(files, dim=2))
return output
def forward(self, added_code, removed_code, hid_state_hunk, hid_state_sent, hid_state_word):
hid_state = (hid_state_hunk, hid_state_sent, hid_state_word)
x_added_code = self.forward_code(x=added_code, hid_state=hid_state)
x_removed_code = self.forward_code(x=removed_code, hid_state=hid_state)
subtract = self.subtraction(added_code=x_added_code, removed_code=x_removed_code)
multiple = self.multiplication(added_code=x_added_code, removed_code=x_removed_code)
cos = self.cosine_similarity(added_code=x_added_code, removed_code=x_removed_code)
euc = self.euclidean_similarity(added_code=x_added_code, removed_code=x_removed_code)
nn = self.standard_neural_network_layer(added_code=x_added_code, removed_code=x_removed_code)
ntn = self.neural_network_tensor_layer(added_code=x_added_code, removed_code=x_removed_code)
x_diff_code = torch.cat((subtract, multiple, cos, euc, nn, ntn), dim=1)
x_diff_code = self.dropout(x_diff_code)
out = self.fc1(x_diff_code)
out = F.relu(out)
out = self.fc2(out)
out = self.sigmoid(out).squeeze(1)
return out
def forward_commit_embeds_diff(self, added_code, removed_code, hid_state_hunk, hid_state_sent, hid_state_word):
hid_state = (hid_state_hunk, hid_state_sent, hid_state_word)
x_added_code = self.forward_code(x=added_code, hid_state=hid_state)
x_removed_code = self.forward_code(x=removed_code, hid_state=hid_state)
subtract = self.subtraction(added_code=x_added_code, removed_code=x_removed_code)
multiple = self.multiplication(added_code=x_added_code, removed_code=x_removed_code)
cos = self.cosine_similarity(added_code=x_added_code, removed_code=x_removed_code)
euc = self.euclidean_similarity(added_code=x_added_code, removed_code=x_removed_code)
nn = self.standard_neural_network_layer(added_code=x_added_code, removed_code=x_removed_code)
ntn = self.neural_network_tensor_layer(added_code=x_added_code, removed_code=x_removed_code)
x_diff_code = torch.cat((subtract, multiple, cos, euc, nn, ntn), dim=1)
return x_diff_code
def forward_commit_embeds(self, added_code, removed_code, hid_state_hunk, hid_state_sent, hid_state_word):
hid_state = (hid_state_hunk, hid_state_sent, hid_state_word)
x_added_code = self.forward_code(x=added_code, hid_state=hid_state)
x_removed_code = self.forward_code(x=removed_code, hid_state=hid_state)
x_diff_code = torch.cat((x_added_code, x_removed_code), dim=1)
return x_diff_code
def subtraction(self, added_code, removed_code):
return added_code - removed_code
def multiplication(self, added_code, removed_code):
return added_code * removed_code
def cosine_similarity(self, added_code, removed_code):
cosine = nn.CosineSimilarity(eps=1e-6)
return cosine(added_code, removed_code).view(self.batch_size, 1)
def euclidean_similarity(self, added_code, removed_code):
euclidean = nn.PairwiseDistance(p=2)
return euclidean(added_code, removed_code).view(self.batch_size, 1)
def standard_neural_network_layer(self, added_code, removed_code):
concat = torch.cat((removed_code, added_code), dim=1)
output = self.standard_nn_layer(concat)
output = F.relu(output)
return output
def neural_network_tensor_layer(self, added_code, removed_code):
output_one = self.W_nn_tensor_one(removed_code)
output_one = torch.mul(output_one, added_code)
output_one = torch.sum(output_one, dim=1).view(self.batch_size, 1)
output_two = self.W_nn_tensor_two(removed_code)
output_two = torch.mul(output_two, added_code)
output_two = torch.sum(output_two, dim=1).view(self.batch_size, 1)
W_output = torch.cat((output_one, output_two), dim=1)
code = torch.cat((removed_code, added_code), dim=1)
V_output = self.V_nn_tensor(code)
return F.relu(W_output + V_output)
def init_hidden_hunk(self):
return Variable(torch.zeros(2, self.batch_size, self.hidden_size)).cuda()
def init_hidden_sent(self):
return Variable(torch.zeros(2, self.batch_size, self.hidden_size)).cuda()
def init_hidden_word(self):
return Variable(torch.zeros(2, self.batch_size, self.hidden_size)).cuda()