Spaces:
Sleeping

Space1 / models.py
YashaswiAbhinav
Move LSTMClassifier to models.py to fix loading issue
104752c
Raw History Blame Contribute Delete
771 Bytes
import torch.nn as nn
class LSTMClassifier(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim, output_dim, n_layers, dropout):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(
embed_dim,
hidden_dim,
num_layers=n_layers,
batch_first=True,
dropout=dropout if n_layers > 1 else 0
)
self.fc = nn.Linear(hidden_dim, output_dim)
self.dropout_layer = nn.Dropout(dropout) # Renamed to avoid conflict
def forward(self, x):
embedded = self.dropout_layer(self.embedding(x)) # Use the renamed attribute
output, (hidden, cell) = self.lstm(embedded)
return self.fc(output[:, -1, :])