ankurani's picture
Upload 31 files
76cd8d0 verified
raw
history blame contribute delete
968 Bytes
# 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