Instructions to use jerilseb/mnist-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use jerilseb/mnist-classifier with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import MNISTClassifier model = MNISTClassifier.from_pretrained("jerilseb/mnist-classifier", device_map="auto") - Notebooks
- Google Colab
- Kaggle
|
Download README.md from jerilseb/mnist-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/jerilseb/mnist-classifier/resolve/main/README.md
- Command line
-
hf download hf://jerilseb/mnist-classifier/README.md
-
curl -L -o README.md https://huggingface.co/jerilseb/mnist-classifier/resolve/main/README.md
1.89 kB
| library_name: transformers | |
| tags: [] | |
| # Usage | |
| Define the model and config | |
| ```python | |
| from transformers import PreTrainedModel, PretrainedConfig | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class MNISTConfig(PretrainedConfig): | |
| model_type = "mnist_classifier" | |
| def __init__(self, input_size=784, hidden_size1=1024, hidden_size2=512, num_labels=10, **kwargs): | |
| super().__init__(**kwargs) | |
| self.input_size = input_size | |
| self.hidden_size1 = hidden_size1 | |
| self.hidden_size2 = hidden_size2 | |
| self.num_labels = num_labels | |
| class MNISTClassifier(PreTrainedModel): | |
| config_class = MNISTConfig | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.layer1 = nn.Linear(config.input_size, config.hidden_size1) | |
| self.layer2 = nn.Linear(config.hidden_size1, config.hidden_size2) | |
| self.layer3 = nn.Linear(config.hidden_size2, config.num_labels) | |
| def forward(self, pixel_values): | |
| inputs = pixel_values.view(-1, self.config.input_size) | |
| outputs = self.layer1(inputs) | |
| outputs = F.leaky_relu(outputs) | |
| outputs = self.layer2(outputs) | |
| outputs = F.leaky_relu(outputs) | |
| outputs = self.layer3(outputs) | |
| return outputs | |
| ``` | |
| Register the model | |
| ```python | |
| from transformers import AutoConfig, AutoModel | |
| AutoConfig.register("mnist_classifier", MNISTConfig) | |
| AutoModel.register(MNISTConfig, MNISTClassifier) | |
| ``` | |
| Run Inference | |
| ```python | |
| from transformers import AutoConfig, AutoModel | |
| import torch | |
| config = AutoConfig.from_pretrained("jerilseb/mnist-classifier") | |
| model = AutoModel.from_pretrained("jerilseb/mnist-classifier") | |
| input_tensor = torch.randn(1, 28, 28) # Single image, adjust batch size as needed | |
| with torch.no_grad(): | |
| output = model(input_tensor) | |
| predicted_class = output.argmax(-1).item() | |
| print(f"Predicted class: {predicted_class}") | |
| ``` | |