-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_init_inject.py
28 lines (21 loc) · 986 Bytes
/
decoder_init_inject.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
import torch
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn
class Decoder(nn.Module):
def __init__(self, hidden_size, output_size, embed_size, num_layers):
super(Decoder, self).__init__()
self.hidden_size = hidden_size
self.embedding = nn.Embedding(output_size, embed_size)
self.gru = nn.GRU(embed_size, hidden_size, num_layers=num_layers)
self.out = nn.Linear(hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
# Init inject
def forward(self, input, hidden):
output = self.embedding(input)
output = F.relu(output)
output, hidden = self.gru(output, hidden)
output = self.softmax(self.out(output[0]))
return output, hidden
def get_decoder(hidden_size=2048, output_size=10000, embed_size=128, num_layers=1):
return Decoder(hidden_size=hidden_size, output_size=output_size, embed_size=embed_size, num_layers=num_layers)