-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoderRNN.py
More file actions
38 lines (33 loc) · 1.17 KB
/
DecoderRNN.py
File metadata and controls
38 lines (33 loc) · 1.17 KB
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
import numpy as np
import string
import re
import random
import torch
import torch.nn as nn
import torch.nn.init as torch_init
from torch import optim
import torch.nn.functional as F
class DecoderRNN(nn.Module):
def __init__(self, hidden_size, vocab_size, output_size):
super(DecoderRNN, self).__init__()
# hidden size is the same as encoder
self.hidden_size = hidden_size
# word embedding
# size of dictionary for embedding = output_size
# size of embedding vector = hidden_size
self.embedding = nn.Embedding(vocab_size, hidden_size)
# LSTM unit
# output of embedding would be = hidden_size
self.lstm = nn.LSTM(output_size*hidden_size, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
torch_init.xavier_normal_(self.out.weight)
# self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden, hidden_list):
output = self.embedding(input).view(1, 1, -1)
output, hidden = self.lstm(output, hidden)
output = self.out(output[0])
# output = self.softmax(output)
return output, hidden
def initHidden(self):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
return torch.zeros(1, 1, self.hidden_size, device=device)