Spaces:
Sleeping
Sleeping
Download ml/digits/mnist_model.py from HighFive52/ml-api: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/HighFive52/ml-api/resolve/main/ml/digits/mnist_model.py
- Command line
-
hf download hf://spaces/HighFive52/ml-api/ml/digits/mnist_model.py
-
curl -L -o mnist_model.py https://huggingface.co/spaces/HighFive52/ml-api/resolve/main/ml/digits/mnist_model.py
1.05 kB
| import torch | |
| import torch.nn as nn | |
| class MNISTModel(nn.Module): | |
| def __init__(self, dropout_rate=0.3): | |
| super().__init__() | |
| self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1) | |
| self.bn1 = nn.BatchNorm2d(32) | |
| self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) | |
| self.bn2 = nn.BatchNorm2d(64) | |
| self.pool = nn.MaxPool2d(2, 2) | |
| self.relu = nn.ReLU() | |
| self.dropout = nn.Dropout(dropout_rate) | |
| self.fc1 = nn.Linear(64 * 7 * 7, 128) | |
| self.fc2 = nn.Linear(128, 10) | |
| def forward(self, x): | |
| # Block 1 | |
| x = self.conv1(x) | |
| x = self.bn1(x) | |
| x = self.relu(x) | |
| x = self.pool(x) | |
| # Block 2 | |
| x = self.conv2(x) | |
| x = self.bn2(x) | |
| x = self.relu(x) | |
| x = self.pool(x) | |
| # Flatten | |
| x = torch.flatten(x, 1) | |
| # Fully connected | |
| x = self.fc1(x) | |
| x = self.relu(x) | |
| x = self.dropout(x) | |
| x = self.fc2(x) | |
| return x | |