File size: 968 Bytes
76cd8d0 |
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 |
# Code for making a base nework that would take tokenized input and pass it through an embedding layer and then through a LSTM layer to get the output
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
class base_network(nn.Module):
def __init__(self, input_size, embedding_size, hidden_size, num_layers, dropout, bidirectional, device):
super(base_network, self).__init__()
self.embedding = nn.Embedding(input_size, embedding_size)
self.lstm = nn.LSTM(embedding_size, hidden_size, num_layers, batch_first=True,
dropout=dropout, bidirectional=bidirectional)
# self.fc = nn.Linear(hidden_size, output_size)
self.device = device
def forward(self, x):
x = x.to(self.device)
x = self.embedding(x)
x, (h_n, c_n) = self.lstm(x)
out = torch.permute(h_n[-2:, :, :], (1, 0, 2)).reshape(x.size(0), -1)
return out
|