-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
145 lines (129 loc) · 4.73 KB
/
models.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
import math
import torch
from torch import nn as nn
from torch.nn import functional as F
""" class Model(nn.Module):
def __init__(
self,
TEXT,
hidden_dim,
conv_depth,
kernel_size,
pool_size,
dense_depth,
max_len,
similarity,
trainable,
):
super().__init__()
embedding_matrix = TEXT.vocab.vectors
emb_dim = embedding_matrix.size()[1]
self.similarity = similarity
self.embedding = nn.Embedding.from_pretrained(embedding_matrix)
self.embedding.requires_grad = trainable
self.encoder_left = nn.LSTM(
emb_dim, hidden_dim, num_layers=1, bidirectional=False, batch_first=True
)
self.encoder_right = nn.LSTM(
emb_dim, hidden_dim, num_layers=1, bidirectional=False, batch_first=True
)
self.conv1 = nn.Conv2d(1, conv_depth, kernel_size)
self.batch_norm1 = nn.BatchNorm2d(conv_depth)
output_size = int((((max_len - 2) / 2) ** 2) * conv_depth)
self.max_pool1 = nn.MaxPool2d(pool_size)
self.mlp1 = nn.Linear(output_size, dense_depth)
self.mlp2 = nn.Linear(dense_depth, 16)
self.out = nn.Linear(16, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, seq):
hdn_left, _ = self.encoder_left(self.embedding(seq[0]))
hdn_right, _ = self.encoder_right(self.embedding(seq[1]))
if self.similarity == "dot":
similarity = torch.matmul(hdn_left, torch.transpose(hdn_right, 1, 2))
similarity = torch.unsqueeze(similarity, 1)
else:
num = torch.matmul(hdn_left, torch.transpose(hdn_right, 1, 2))
n1 = torch.norm(hdn_left, dim=-1)
n2 = torch.norm(hdn_right, dim=-1)
den_for_row = (n1 * n2)[:, :, None]
repeat_along_last_axis = num.size()[-1]
similarity = num * torch.repeat_interleave(
den_for_row, repeat_along_last_axis, dim=-1
)
similarity = torch.unsqueeze(similarity, 1)
x = self.conv1(similarity)
x = F.relu(x)
x = self.batch_norm1(x)
x = self.max_pool1(x)
x = torch.flatten(x, start_dim=1)
x = self.mlp1(x)
x = F.relu(x)
x = F.dropout(x, 0.3)
x = self.mlp2(x)
x = F.relu(x)
x = F.dropout(x, 0.3)
x = self.out(x)
x = self.sigmoid(x)
return x """
class Model(nn.Module):
def __init__(
self,
TEXT,
hidden_dim,
conv_depth,
kernel_size,
pool_size,
dense_depth1,
dense_depth2,
max_len,
similarity,
trainable,
):
super().__init__()
embedding_matrix = TEXT.vocab.vectors
emb_dim = embedding_matrix.size()[1]
self.similarity = similarity
self.embedding = nn.Embedding.from_pretrained(embedding_matrix)
self.embedding.requires_grad = trainable
self.encoder_left = nn.LSTM(
emb_dim, hidden_dim, num_layers=1, bidirectional=False, batch_first=True
)
self.encoder_right = nn.LSTM(
emb_dim, hidden_dim, num_layers=1, bidirectional=False, batch_first=True
)
self.conv1 = nn.Conv2d(1, conv_depth, kernel_size)
self.batch_norm1 = nn.BatchNorm2d(conv_depth)
output_size = (math.floor((max_len - kernel_size + 1) / 2) ** 2) * conv_depth
self.max_pool1 = nn.MaxPool2d(pool_size)
self.mlp1 = nn.Linear(output_size, dense_depth1)
self.mlp2 = nn.Linear(dense_depth1, dense_depth2)
self.out = nn.Linear(dense_depth2, 2)
def forward(self, seq):
hdn_left, _ = self.encoder_left(self.embedding(seq[0]))
hdn_right, _ = self.encoder_right(self.embedding(seq[1]))
if self.similarity == "dot":
similarity = torch.matmul(hdn_left, torch.transpose(hdn_right, 1, 2))
similarity = torch.unsqueeze(similarity, 1)
else:
num = torch.matmul(hdn_left, torch.transpose(hdn_right, 1, 2))
n1 = torch.norm(hdn_left, dim=-1)
n2 = torch.norm(hdn_right, dim=-1)
den_for_row = (n1 * n2)[:, :, None]
repeat_along_last_axis = num.size()[-1]
similarity = num * torch.repeat_interleave(
den_for_row, repeat_along_last_axis, dim=-1
)
similarity = torch.unsqueeze(similarity, 1)
x = self.conv1(similarity)
x = F.relu(x)
x = self.batch_norm1(x)
x = self.max_pool1(x)
x = torch.flatten(x, start_dim=1)
x = self.mlp1(x)
x = F.relu(x)
x = F.dropout(x, 0.3)
x = self.mlp2(x)
x = F.relu(x)
x = F.dropout(x, 0.3)
x = self.out(x)
return x