-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
26 lines (23 loc) · 941 Bytes
/
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
import torch
import torch.nn as nn
class PoetryModel(nn.Module):
def __init__(self, hidden_dim, vocabulary_size):
super(PoetryModel, self).__init__()
self.hidden_dim = hidden_dim
self.embedding = nn.Embedding(vocabulary_size, 124)
self.lstm1 = nn.LSTM(124, self.hidden_dim, num_layers = 2)
self.linear1 = nn.Linear(self.hidden_dim, vocabulary_size)
def forward(self, x, hidden = None):
if hidden is None:
seq_len, batch_size = x.shape
else:
h_0, c_0 = hidden
h_0 = x.data.new(2, batch_size, self.hidden_dim).fill_(0).float()
c_0 = x.data.new(2, batch_size, self.hidden_dim).fill_(0).float()
x = self.embedding(x)
x, hidden = self.lstm1(x, (h_0, c_0))
x = x.view(seq_len * batch_size, -1)
x = self.linear1(x)
return x, hidden
if __name__ == '__main__':
poetryModel = PoetryModel(256, 8100)