-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_architectures.py
43 lines (34 loc) · 1.45 KB
/
model_architectures.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
import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Encoder_RNN(nn.Module):
def __init__(self, input_size, hidden_size):
super(Encoder_RNN, self).__init__()
self.hidden_size = hidden_size
self.embedding = nn.Embedding(input_size, hidden_size)
self.gru = nn.GRU(hidden_size, hidden_size)
def forward(self, input, hidden):
embed = self.embedding(input).view(1, 1, -1)
output, hidden = self.gru(embed, hidden)
# output and hidden are the same vectors
return output, hidden
def initHidden(self):
return torch.zeros(1, 1, self.hidden_size, device=device)
class Decoder_RNN(nn.Module):
def __init__(self, output_size, hidden_size):
super(Decoder_RNN, self).__init__()
self.hidden_size = hidden_size
self.embedding = nn.Embedding(output_size, hidden_size)
self.gru = nn.GRU(hidden_size, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden):
embed = self.embedding(input).view(1, 1, -1)
embed = F.relu(embed)
output, hidden = self.gru(embed, hidden)
output = self.softmax(self.out(output[0]))
return output, hidden
def initHidden(self):
return torch.zeros(1, 1, self.hidden_size, device=device)