Instructions to use UNCANNY69/MisAlBERT_LSTM with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use UNCANNY69/MisAlBERT_LSTM with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="UNCANNY69/MisAlBERT_LSTM", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained("UNCANNY69/MisAlBERT_LSTM", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download model.py from UNCANNY69/MisAlBERT_LSTM: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/UNCANNY69/MisAlBERT_LSTM/resolve/main/model.py
- Command line
-
hf download hf://UNCANNY69/MisAlBERT_LSTM/model.py
-
curl -L -o model.py https://huggingface.co/UNCANNY69/MisAlBERT_LSTM/resolve/main/model.py
2.99 kB
| from abc import ABCMeta | |
| import torch | |
| from transformers.pytorch_utils import nn | |
| import torch.nn.functional as F | |
| from transformers import AlbertModel, AlbertForSequenceClassification, PreTrainedModel | |
| from transformers.modeling_outputs import SequenceClassifierOutput | |
| from transformers import AlbertConfig | |
| from transformers import PretrainedConfig | |
| class AlbertLSTMConfig(PretrainedConfig): | |
| model_type = "albertLSTMForSequenceClassification" | |
| def __init__(self, | |
| num_classes=2, | |
| embed_dim=768, | |
| num_layers=12, | |
| hidden_dim_lstm=256, # New parameter for LSTM | |
| dropout_rate=0.1, | |
| **kwargs): | |
| super().__init__(**kwargs) | |
| self.num_classes = num_classes | |
| self.embed_dim = embed_dim | |
| self.num_layers = num_layers | |
| self.hidden_dim_lstm = hidden_dim_lstm # Assign LSTM hidden dimension | |
| self.dropout_rate = dropout_rate | |
| self.id2label = { | |
| 0: "fake", | |
| 1: "true", | |
| } | |
| self.label2id = { | |
| "fake": 0, | |
| "true": 1, | |
| } | |
| class AlbertLSTMForSequenceClassification(PreTrainedModel, metaclass=ABCMeta): | |
| config_class = AlbertLSTMConfig | |
| def __init__(self, config): | |
| super(AlbertLSTMForSequenceClassification, self).__init__(config) | |
| self.num_classes = config.num_classes | |
| self.embed_dim = config.embed_dim | |
| self.num_layers = config.num_layers | |
| self.hidden_dim_lstm = config.hidden_dim_lstm | |
| self.dropout = nn.Dropout(config.dropout_rate) | |
| self.albert = AlbertModel.from_pretrained('albert-base-v2', | |
| output_hidden_states=True, | |
| output_attentions=False) | |
| print("ALBERT Model Loaded") | |
| self.lstm = nn.LSTM(self.embed_dim, self.hidden_dim_lstm, batch_first=True, num_layers=3) # noqa | |
| self.fc = nn.Linear(self.hidden_dim_lstm, self.num_classes) | |
| def forward(self, input_ids, attention_mask, token_type_ids, labels=None): | |
| albert_output = self.albert(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids) | |
| hidden_states = albert_output["hidden_states"] | |
| hidden_states = torch.stack([hidden_states[layer_i][:, 0].squeeze() | |
| for layer_i in range(0, self.num_layers)], dim=-1) # noqa | |
| hidden_states = hidden_states.view(-1, self.num_layers, self.embed_dim) | |
| out, _ = self.lstm(hidden_states, None) | |
| out = self.dropout(out[:, -1, :]) | |
| logits = self.fc(out) | |
| loss = None | |
| if labels is not None: | |
| loss = F.cross_entropy(logits, labels) | |
| out = SequenceClassifierOutput( | |
| loss=loss, | |
| logits=logits, | |
| hidden_states=albert_output.hidden_states, | |
| attentions=albert_output.attentions, | |
| ) | |
| return out |