-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodel_baseline.py
189 lines (146 loc) · 6.21 KB
/
model_baseline.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
class BaselineNet(nn.Module):
def __init__(self,
vocab_dim,
num_classes,
embed_dim = 100,
statement_kernel_num = 14,
statement_kernel_size = [3, 4, 5],
subject_hidden_dim = 5,
subject_lstm_nlayers = 2,
subject_lstm_bidirectional = True,
speaker_pos_hidden_dim = 5,
speaker_pos_lstm_nlayers = 2,
speaker_pos_lstm_bidirectional = True,
context_hidden_dim = 6,
context_lstm_nlayers = 2,
context_lstm_bidirectional = True,
justification_hidden_dim = 6,
justification_lstm_nlayers = 2,
justification_lstm_bidirectional = True,
dropout_query = 0.5,
dropout_features = 0.5):
# Statement CNN
super(BaselineNet, self).__init__()
# import pdb; pdb.set_trace()
self.num_classes = num_classes
self.vocab_dim = vocab_dim
self.embed_dim = embed_dim
self.statement_kernel_num = statement_kernel_num
self.statement_kernel_size = statement_kernel_size
self.embedding = nn.Embedding(self.vocab_dim, self.embed_dim)
# conv layer with in_channel = 1, out_channel = statement_kernel_num
# and kernel spatial size (rectangle) = (kernel_, embed_dim)
self.statement_convs = nn.ModuleList()
for kernel_ in self.statement_kernel_size:
self.statement_convs.append(nn.Conv2d(1, self.statement_kernel_num, (kernel_, self.embed_dim)))
# Subject
self.subject_lstm_nlayers = subject_lstm_nlayers
self.subject_lstm_num_direction = 2 if subject_lstm_bidirectional else 1
self.subject_hidden_dim = subject_hidden_dim
self.subject_lstm = nn.LSTM(
input_size = self.embed_dim,
hidden_size = self.subject_hidden_dim,
num_layers = self.subject_lstm_nlayers,
batch_first = True,
bidirectional = subject_lstm_bidirectional
)
# Speaker
# Speaker Position
self.speaker_pos_lstm_nlayers = speaker_pos_lstm_nlayers
self.speaker_pos_lstm_num_direction = 2 if speaker_pos_lstm_bidirectional else 1
self.speaker_pos_hidden_dim = speaker_pos_hidden_dim
self.speaker_pos_lstm = nn.LSTM(
input_size = self.embed_dim,
hidden_size = self.speaker_pos_hidden_dim,
num_layers = self.speaker_pos_lstm_nlayers,
batch_first = True,
bidirectional = speaker_pos_lstm_bidirectional
)
# State
# Party
# Context
self.context_lstm_nlayers = context_lstm_nlayers
self.context_lstm_num_direction = 2 if context_lstm_bidirectional else 1
self.context_hidden_dim = context_hidden_dim
self.context_lstm = nn.LSTM(
input_size = self.embed_dim,
hidden_size = self.context_hidden_dim,
num_layers = self.context_lstm_nlayers,
batch_first = True,
bidirectional = context_lstm_bidirectional
)
# Justification
self.justification_lstm_nlayers = justification_lstm_nlayers
self.justification_lstm_num_direction = 2 if justification_lstm_bidirectional else 1
self.justification_hidden_dim = justification_hidden_dim
self.justification_lstm = nn.LSTM(
input_size = self.embed_dim,
hidden_size = self.justification_hidden_dim,
num_layers = self.justification_lstm_nlayers,
batch_first = True,
bidirectional = justification_lstm_bidirectional
)
self.dropout_query = nn.Dropout(dropout_query)
self.dropout_features = nn.Dropout(dropout_features)
self.query_dim = self.subject_lstm_nlayers * self.subject_lstm_num_direction \
+ self.embed_dim \
+ self.speaker_pos_lstm_nlayers * self.speaker_pos_lstm_num_direction \
+ self.embed_dim \
+ self.embed_dim \
+ self.context_lstm_nlayers * self.context_lstm_num_direction \
+ self.justification_lstm_nlayers * self.justification_lstm_num_direction
self.fc_query = nn.Linear(self.query_dim, self.embed_dim)
self.fc_att = nn.Linear(self.embed_dim, self.embed_dim)
self.fc_conv = nn.Linear(self.embed_dim, self.embed_dim)
self.fc_cat = nn.Linear(self.embed_dim, self.embed_dim)
self.fc = nn.Linear(len(self.statement_kernel_size) * self.statement_kernel_num + self.query_dim,
self.num_classes)
def forward(self, sample):
statement = Variable(sample.statement).unsqueeze(0)
subject = Variable(sample.subject).unsqueeze(0)
speaker = Variable(sample.speaker).unsqueeze(0)
speaker_pos = Variable(sample.speaker_pos).unsqueeze(0)
state = Variable(sample.state).unsqueeze(0)
party = Variable(sample.party).unsqueeze(0)
context = Variable(sample.context).unsqueeze(0)
justification = Variable(sample.justification).unsqueeze(0)
batch = 1 # Current support one sample per time
# TODO: Increase batch number
# Subject
subject_ = self.embedding(subject) # 1*W*D
_, (subject_, _) = self.subject_lstm(subject_) # (layer x dir) * batch * hidden
subject_ = F.max_pool1d(subject_, self.subject_hidden_dim).view(1, -1) # (layer x dir) * batch * 1 -> 1*(layer x dir)
# Speaker
speaker_ = self.embedding(speaker).squeeze(0) # 1*1*D -> 1*D
# Speaker Position
speaker_pos_ = self.embedding(speaker_pos)
_, (speaker_pos_, _) = self.speaker_pos_lstm(speaker_pos_)
speaker_pos_ = F.max_pool1d(speaker_pos_, self.speaker_pos_hidden_dim).view(1, -1)
# State
state_ = self.embedding(state).squeeze(0)
# Party
party_ = self.embedding(party).squeeze(0)
# Context
context_ = self.embedding(context)
_, (context_, _) = self.context_lstm(context_)
context_ = F.max_pool1d(context_, self.context_hidden_dim).view(1, -1)
# Justification
justification_ = self.embedding(justification)
_, (justification_, _) = self.justification_lstm(justification_)
justification_ = F.max_pool1d(justification_, self.justification_hidden_dim).view(1, -1)
# Statement
statement_ = self.embedding(statement).unsqueeze(0) # 1*W*D -> 1*1*W*D
statement_ = [F.relu(conv(statement_)).squeeze(3) for conv in self.statement_convs] # 1*1*W*1 -> 1*Conv-filters*(W-1) x len(convs)
statement_ = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in statement_] # 1*Conv-filters*1 -> 1*Conv-filters x len(convs)
statement_ = torch.cat(statement_, 1) # 1*len(convs)
# Concatenate
features = torch.cat((statement_, subject_, speaker_, speaker_pos_, state_, party_, context_, justification_), 1)
features = self.dropout_features(features)
out = self.fc(features)
out = F.log_softmax(out, dim=-1)
return out