|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import logging |
| 8 | + |
| 9 | +import torch |
| 10 | +import torch.nn as nn |
| 11 | + |
| 12 | +from ..model_base import EagerModelBase |
| 13 | + |
| 14 | + |
| 15 | +class BidirectionalLSTM(nn.Module): |
| 16 | + """Bidirectional LSTM for sequence modeling""" |
| 17 | + |
| 18 | + def __init__(self, input_size=100, hidden_size=128, num_layers=2, num_classes=10): |
| 19 | + super(BidirectionalLSTM, self).__init__() |
| 20 | + self.hidden_size = hidden_size |
| 21 | + self.num_layers = num_layers |
| 22 | + |
| 23 | + # Bidirectional LSTM |
| 24 | + self.lstm = nn.LSTM( |
| 25 | + input_size, hidden_size, num_layers, batch_first=True, bidirectional=True |
| 26 | + ) |
| 27 | + |
| 28 | + # Output layer (hidden_size * 2 because of bidirectional) |
| 29 | + self.fc = nn.Linear(hidden_size * 2, num_classes) |
| 30 | + |
| 31 | + def forward(self, x): |
| 32 | + # Initialize hidden states |
| 33 | + # For bidirectional: hidden states shape is (num_layers * 2, batch, hidden_size) |
| 34 | + h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(x.device) |
| 35 | + c0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(x.device) |
| 36 | + |
| 37 | + # LSTM forward pass |
| 38 | + out, _ = self.lstm(x, (h0, c0)) |
| 39 | + |
| 40 | + # Take the last time step output |
| 41 | + out = self.fc(out[:, -1, :]) |
| 42 | + return out |
| 43 | + |
| 44 | + |
| 45 | +class BidirectionalLSTMTextClassifier(nn.Module): |
| 46 | + """Bidirectional LSTM for text classification with embedding layer""" |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, vocab_size=10000, embedding_dim=128, hidden_size=256, num_classes=2 |
| 50 | + ): |
| 51 | + super(BidirectionalLSTMTextClassifier, self).__init__() |
| 52 | + self.hidden_size = hidden_size |
| 53 | + |
| 54 | + # Embedding layer |
| 55 | + self.embedding = nn.Embedding(vocab_size, embedding_dim) |
| 56 | + |
| 57 | + # Bidirectional LSTM |
| 58 | + self.lstm = nn.LSTM( |
| 59 | + embedding_dim, hidden_size, bidirectional=True, batch_first=True |
| 60 | + ) |
| 61 | + |
| 62 | + # Output layer |
| 63 | + self.fc = nn.Linear(hidden_size * 2, num_classes) |
| 64 | + |
| 65 | + def forward(self, x): |
| 66 | + # Embedding |
| 67 | + embedded = self.embedding(x) |
| 68 | + |
| 69 | + # LSTM |
| 70 | + lstm_out, _ = self.lstm(embedded) |
| 71 | + |
| 72 | + # Global max pooling over sequence dimension |
| 73 | + pooled = torch.max(lstm_out, dim=1)[0] |
| 74 | + |
| 75 | + # Classification |
| 76 | + output = self.fc(pooled) |
| 77 | + return output |
| 78 | + |
| 79 | + |
| 80 | +class BidirectionalLSTMModel(EagerModelBase): |
| 81 | + def __init__(self): |
| 82 | + pass |
| 83 | + |
| 84 | + def get_eager_model(self) -> torch.nn.Module: |
| 85 | + logging.info("Loading Bidirectional LSTM model") |
| 86 | + model = BidirectionalLSTM( |
| 87 | + input_size=100, hidden_size=128, num_layers=2, num_classes=10 |
| 88 | + ) |
| 89 | + model.eval() |
| 90 | + logging.info("Loaded Bidirectional LSTM model") |
| 91 | + return model |
| 92 | + |
| 93 | + def get_example_inputs(self): |
| 94 | + # Example: (batch_size=1, seq_len=50, input_size=100) |
| 95 | + tensor_size = (1, 50, 100) |
| 96 | + return (torch.randn(tensor_size),) |
0 commit comments