Created
February 13, 2020 16:34
-
-
Save henry16lin/c56dc610abb88345294d47ea0eb3750c to your computer and use it in GitHub Desktop.
LSTM_NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### build model ### | |
class RNN(nn.Module): | |
def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, | |
bidirectional, weight, labels, **kwargs): | |
super(RNN, self).__init__(**kwargs) | |
self.num_hiddens = num_hiddens | |
self.num_layers = num_layers | |
self.bidirectional = bidirectional | |
self.embedding = nn.Embedding.from_pretrained(weight) | |
self.embedding.weight.requires_grad = False | |
self.encoder = nn.LSTM(input_size=embed_size, hidden_size=self.num_hiddens, | |
num_layers=num_layers, bidirectional=self.bidirectional, | |
dropout=0.3) | |
if self.bidirectional: | |
self.linear1 = nn.Linear(num_hiddens * 4, labels) | |
else: | |
self.linear1 = nn.Linear(num_hiddens * 2, labels) | |
def forward(self, inputs): | |
embeddings = self.embedding(inputs) | |
states, hidden = self.encoder(embeddings.permute([1, 0, 2])) | |
encoding = torch.cat([states[0], states[-1]], dim=1) #if it's bidirectional, choose first and last output | |
outputs = self.linear1(encoding) | |
return outputs | |
num_epochs = 10 | |
num_hiddens = 100 | |
num_layers = 2 | |
bidirectional = True | |
labels = 2 | |
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu") | |
net = RNN(vocab_size=(vocab_size+1), embed_size=embed_size, | |
num_hiddens=num_hiddens, num_layers=num_layers, | |
bidirectional=bidirectional, weight=weight, | |
labels=labels) | |
print(net) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment