-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
executable file
·140 lines (112 loc) · 4.67 KB
/
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class CNNText(nn.Module):
def __init__(self, vocab_size, sentence_len, pretrained_embeddings, output_dim=6, mode="static"):
super(CNNText, self).__init__()
kernel_sizes = [3, 4, 5]
num_filters = 100
embedding_dim = 300
self.vars = nn.ParameterList()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.embedding.weight.data.copy_(torch.from_numpy(pretrained_embeddings))
self.embedding.weight.requires_grad = (mode == "nonstatic")
self.relu = nn.ReLU()
# block 1
kernel_size = kernel_sizes[0]
maxpool_kernel_size = sentence_len - kernel_size + 1
conv1d_1 = nn.Conv1d(in_channels=embedding_dim, out_channels=num_filters, kernel_size=kernel_size,
stride=1)
self.maxpool_1 = nn.MaxPool1d(kernel_size=maxpool_kernel_size)
self.vars.append(nn.Parameter(conv1d_1.weight))
self.vars.append(nn.Parameter(conv1d_1.bias))
# block 2
kernel_size = kernel_sizes[1]
maxpool_kernel_size = sentence_len - kernel_size + 1
conv1d_2 = nn.Conv1d(in_channels=embedding_dim, out_channels=num_filters, kernel_size=kernel_size,
stride=1)
self.maxpool_2 = nn.MaxPool1d(kernel_size=maxpool_kernel_size)
self.vars.append(nn.Parameter(conv1d_2.weight))
self.vars.append(nn.Parameter(conv1d_2.bias))
# block 3
kernel_size = kernel_sizes[1]
maxpool_kernel_size = sentence_len - kernel_size + 1
conv1d_3 = nn.Conv1d(in_channels=embedding_dim, out_channels=num_filters, kernel_size=kernel_size,
stride=1)
self.maxpool_3 = nn.MaxPool1d(kernel_size=maxpool_kernel_size)
self.vars.append(nn.Parameter(conv1d_3.weight))
self.vars.append(nn.Parameter(conv1d_3.bias))
fc = nn.Linear(num_filters * len(kernel_sizes), output_dim)
self.vars.append(nn.Parameter(fc.weight))
self.vars.append(nn.Parameter(fc.bias))
def forward(self, x, vars=None): # x: (batch, sentence_len)
x = self.embedding(x) # embedded x: (batch, sentence_len, embedding_dim)
x = x.transpose(1, 2) # x: (batch, embedding_dim, sentence_len)
idx = 0
if vars == None:
vars = self.vars
y1 = F.conv1d(x, weight=vars[idx], bias=vars[idx + 1])
y1 = self.relu(y1)
y1 = self.maxpool_1(y1)
idx += 2
y2 = F.conv1d(x, weight=vars[idx], bias=vars[idx + 1])
y2 = self.relu(y2)
y2 = self.maxpool_2(y2)
idx += 2
y3 = F.conv1d(x, weight=vars[idx], bias=vars[idx + 1])
y3 = self.relu(y3)
y3 = self.maxpool_3(y3)
idx += 2
y = torch.cat([y1, y2, y3], 2)
y = y.view(y.size(0), -1)
feature_extracted = y
y = F.dropout(y, p=0.5, training=self.training)
y = F.linear(y, vars[idx], vars[idx + 1])
res = F.softmax(y, dim=1)
# if torch.sum(res[0]) != 1:
# raise ValueError
return res, feature_extracted
def zero_grad(self, vars=None):
with torch.no_grad():
if vars is None:
for p in self.vars:
if p.grad is not None:
p.grad.zero_()
else:
for p in vars:
if p.grad is not None:
p.grad.zero_()
def parameters(self):
return self.vars
def set_parameters(self, param):
for i, s in enumerate(param):
self.vars[i] = nn.Parameter(s.clone())
class LinearText(nn.Module):
def __init__(self, feature_len, output_dim=6):
super(LinearText, self).__init__()
self.vars = nn.ParameterList()
fc = nn.Linear(feature_len, output_dim)
self.vars.append(nn.Parameter(fc.weight))
self.vars.append(nn.Parameter(fc.bias))
def forward(self, x, vars=None): # x: (batch, sentence_len)
idx = 0
if vars == None:
vars = self.vars
y = F.linear(x, vars[idx], vars[idx + 1])
res = F.softmax(y, dim=1)
return res, y
def zero_grad(self, vars=None):
with torch.no_grad():
if vars is None:
for p in self.vars:
if p.grad is not None:
p.grad.zero_()
else:
for p in vars:
if p.grad is not None:
p.grad.zero_()
def parameters(self):
return self.vars
def set_parameters(self, param):
for i, s in enumerate(param):
self.vars[i] = nn.Parameter(s.clone())