Instructions to use E-katrin/lora_check with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use E-katrin/lora_check with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="E-katrin/lora_check", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("E-katrin/lora_check", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download mlp_classifier.py from E-katrin/lora_check: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/E-katrin/lora_check/resolve/main/mlp_classifier.py
- Command line
-
hf download hf://E-katrin/lora_check/mlp_classifier.py
-
curl -L -o mlp_classifier.py https://huggingface.co/E-katrin/lora_check/resolve/main/mlp_classifier.py
1.41 kB
| import torch | |
| from torch import nn | |
| from torch import Tensor, LongTensor | |
| from transformers.activations import ACT2FN | |
| class MlpClassifier(nn.Module): | |
| """ Simple feed-forward multilayer perceptron classifier. """ | |
| def __init__( | |
| self, | |
| input_size: int, | |
| hidden_size: int, | |
| n_classes: int, | |
| activation: str, | |
| dropout: float, | |
| class_weights: list[float] = None, | |
| ): | |
| super().__init__() | |
| self.n_classes = n_classes | |
| self.classifier = nn.Sequential( | |
| nn.Dropout(dropout), | |
| nn.Linear(input_size, hidden_size), | |
| ACT2FN[activation], | |
| nn.Dropout(dropout), | |
| nn.Linear(hidden_size, n_classes) | |
| ) | |
| if class_weights is not None: | |
| class_weights = torch.tensor(class_weights, dtype=torch.long) | |
| self.cross_entropy = nn.CrossEntropyLoss(weight=class_weights) | |
| def forward(self, embeddings: Tensor, labels: LongTensor = None) -> dict: | |
| logits = self.classifier(embeddings) | |
| # Calculate loss. | |
| loss = 0.0 | |
| if labels is not None: | |
| # Reshape tensors to match expected dimensions | |
| loss = self.cross_entropy( | |
| logits.view(-1, self.n_classes), | |
| labels.view(-1) | |
| ) | |
| # Predictions. | |
| preds = logits.argmax(dim=-1) | |
| return {'preds': preds, 'loss': loss} | |