-
Notifications
You must be signed in to change notification settings - Fork 2
/
line_model.py
35 lines (25 loc) · 969 Bytes
/
line_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
import torch
from torch import nn as nn
import numpy as np
from torch.nn import functional as F
HIDDEN_DIM_DROPOUT_PROB = 0.3
class PatchClassifierByLine(nn.Module):
def __init__(self):
super(PatchClassifierByLine, self).__init__()
self.input_size = 768
self.hidden_size = 128
self.lstm = nn.LSTM(input_size=self.input_size,
hidden_size=self.hidden_size,
batch_first=True,
bidirectional=True)
self.relu = nn.ReLU()
self.drop_out = nn.Dropout(HIDDEN_DIM_DROPOUT_PROB)
self.out_proj = nn.Linear(self.hidden_size, 2)
def forward(self, embedding_batch):
self.lstm.flatten_parameters()
_, (embedding_final_hidden_state, _) = self.lstm(embedding_batch)
x = embedding_final_hidden_state[0]
x = self.relu(x)
x = self.drop_out(x)
out = self.out_proj(x)
return out