forked from CC2Vec/CC2Vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit_DExtended_model.py
69 lines (55 loc) · 2.58 KB
/
jit_DExtended_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
import torch.nn as nn
import torch
import torch.nn.functional as F
class DeepJITExtended(nn.Module):
def __init__(self, args):
super(DeepJITExtended, self).__init__()
self.args = args
V_msg = args.vocab_msg
V_code = args.vocab_code
Dim = args.embedding_dim
Class = args.class_num
Embedding = args.embedding_ftr
Ci = 1 # input of convolutional layer
Co = args.num_filters # output of convolutional layer
Ks = args.filter_sizes # kernel sizes
# CNN-2D for commit message
self.embed_msg = nn.Embedding(V_msg, Dim)
self.convs_msg = nn.ModuleList([nn.Conv2d(Ci, Co, (K, Dim)) for K in Ks])
# CNN-2D for commit code
self.embed_code = nn.Embedding(V_code, Dim)
self.convs_code_line = nn.ModuleList([nn.Conv2d(Ci, Co, (K, Dim)) for K in Ks])
self.convs_code_file = nn.ModuleList([nn.Conv2d(Ci, Co, (K, Co * len(Ks))) for K in Ks])
# other information
self.dropout = nn.Dropout(args.dropout_keep_prob)
self.fc1 = nn.Linear(2 * len(Ks) * Co + Embedding, args.hidden_units) # hidden units
self.fc2 = nn.Linear(args.hidden_units, Class)
self.sigmoid = nn.Sigmoid()
def forward_msg(self, x, convs):
# note that we can use this function for commit code line to get the information of the line
x = x.unsqueeze(1) # (N, Ci, W, D)
x = [F.relu(conv(x)).squeeze(3) for conv in convs] # [(N, Co, W), ...]*len(Ks)
x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] # [(N, Co), ...]*len(Ks)
x = torch.cat(x, 1)
return x
def forward_code(self, x, convs_line, convs_hunks):
n_batch, n_file = x.shape[0], x.shape[1]
x = x.reshape(n_batch * n_file, x.shape[2], x.shape[3])
# apply cnn 2d for each line in a commit code
x = self.forward_msg(x=x, convs=convs_line)
# apply cnn 2d for each file in a commit code
x = x.reshape(n_batch, n_file, self.args.num_filters * len(self.args.filter_sizes))
x = self.forward_msg(x=x, convs=convs_hunks)
return x
def forward(self, ftr, msg, code):
x_msg = self.embed_msg(msg)
x_msg = self.forward_msg(x_msg, self.convs_msg)
x_code = self.embed_code(code)
x_code = self.forward_code(x_code, self.convs_code_line, self.convs_code_file)
x_commit = torch.cat((ftr, x_msg, x_code), 1)
x_commit = self.dropout(x_commit)
out = self.fc1(x_commit)
out = F.relu(out)
out = self.fc2(out)
out = self.sigmoid(out).squeeze(1)
return out