-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
55 lines (40 loc) · 1.94 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
import torch
import torch.nn as nn
class EncoderModel(nn.Module):
def __init__(self, vocab_size):
super(EncoderModel, self).__init__()
self.vocab_size = vocab_size
# Encoder part som convolution layers and max pooling layers
encoder_layers = list()
encoder_layers.append(nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3, stride=1, padding=1))
encoder_layers.append(nn.MaxPool2d(kernel_size=3, stride=2, padding=0))
encoder_layers.append(nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=2, padding=1))
encoder_layers.append(nn.MaxPool2d(kernel_size=2, stride=2, padding=0))
encoder_layers.append(nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=2, padding=1))
self.encoder = nn.Sequential(*encoder_layers)
def forward(self, x):
h = self.encoder(x)
return torch.mean(h.reshape(h.shape[0], h.shape[1], -1), dim=1)
def save(self, path='SavedModels/encoderModel'):
torch.save(self, path)
@staticmethod
def load(path='SavedModels/encoderModel'):
return torch.load(path)
class DecoderModel(nn.Module):
def __init__(self, vocab_size, hidden_size):
super(DecoderModel, self).__init__()
self.vocab_size = vocab_size
self.hidden_size = hidden_size
# Decoder part of model, a GRU with a linear for generating symbols of formula
self.decoder = nn.GRU(self.vocab_size, hidden_size, batch_first=True)
self.hidden2label = nn.Linear(self.hidden_size, vocab_size)
self.softmax = nn.LogSoftmax(dim=2)
def forward(self, x, hidden):
lstm_out, new_hidden = self.decoder(x, hidden)
logit = self.softmax(self.hidden2label(lstm_out))
return logit, new_hidden
def save(self, path='SavedModels/decoderModel1'):
torch.save(self, path)
@staticmethod
def load(path='SavedModels/decoderModel1'):
return torch.load(path)