-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
37 lines (30 loc) · 1.32 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
import torch
import torch.nn as nn
class RnnScore(nn.Module):
def __init__(self, num_q: int):
super(RnnScore, self).__init__()
self.q_embedding = nn.Embedding(num_q, 32)
self.h_layer = nn.Linear(32, 16)
self.c_layer = nn.Linear(32, 16)
self.cnn = nn.Sequential(nn.Conv1d(2, 64, 60, stride=6),
nn.ReLU(inplace=True),
nn.Conv1d(64, 32, 30, stride=3),
nn.ReLU(inplace=True),
nn.Conv1d(32, 16, 10))
self.rnn = nn.LSTM(16, 16, batch_first=True)
self.score_layer = nn.Linear(16, 1)
def forward(self, x: torch.Tensor, q: torch.Tensor) -> torch.Tensor:
q_code = self.q_embedding(q)
h = self.h_layer(q_code).unsqueeze(0)
c = self.c_layer(q_code).unsqueeze(0)
features = self.cnn(x).transpose(1, 2)
output, _ = self.rnn(features, (h, c))
return self.score_layer(output[:, -1])
class RankNet(nn.Module):
def __init__(self, num_q: int):
super(RankNet, self).__init__()
self.scorer = RnnScore(num_q)
def forward(self, x1: torch.Tensor, x2: torch.Tensor, q: torch.Tensor) -> torch.Tensor:
s1 = self.scorer(x1, q)
s2 = self.scorer(x2, q)
return torch.sigmoid(s1 - s2)