Spaces:
Sleeping

ml-api / ml /digits /mnist_model.py
Eric Dyke
FastAPI ML API for Spaceship Titanic and Digit Recognition
0de3c15
Raw History Blame Contribute Delete
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