create a Cifar-10 model and training python script to run on mps
- User
create a Cifar-10 model and training python script to run on mps
- Assistant
Below is a complete Python script for training a simple Convolutional Neural Network (CNN) on the CIFAR-10 dataset using PyTorch. This script is designed to run on Apple's MPS (Metal Performance Shaders) backend, which is available on Apple Silicon (M1/M2/M3 chips). PyTorch supports MPS for GPU acceleration, so ensure you have PyTorch installed with MPS support (e.g., via
pip install torch torchvision torchaudioon a compatible macOS system).Prerequisites
- Python 3.8+ (PyTorch recommends this).
- Install PyTorch:
pip install torch torchvision torchaudio. - Verify MPS availability: Run
import torch; print(torch.backends.mps.is_available())in Python—it should returnTrueon supported hardware. - The script will automatically fall back to CPU if MPS is not available.
Script Explanation
- Dataset: CIFAR-10 (10 classes, 50,000 training images, 10,000 test images).
- Model: A simple CNN with 2 convolutional layers, max-pooling, and fully connected layers.
- Training: 10 epochs with Adam optimizer and CrossEntropyLoss. Batch size 64.
- Device: Uses MPS if available, otherwise CPU.
- Output: Prints training loss per epoch and final test accuracy.
Save the script as
cifar10_mps.pyand run it withpython cifar10_mps.py.import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim import torchvision import torchvision.transforms as transforms # Check for MPS availability and set device device = torch.device("mps" if torch.backends.mps.is_available() else "cpu") print(f"Using device: {device}") # Hyperparameters batch_size = 64 learning_rate = 0.001 num_epochs = 10 # Data transformations (normalize to mean/std of CIFAR-10) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) ]) # Load CIFAR-10 dataset trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False) # Define the CNN model class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) # Input: 3 channels (RGB), Output: 32 channels self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(2, 2) # 2x2 pooling self.fc1 = nn.Linear(64 * 8 * 8, 512) # After two pools: 32x32 -> 16x16 -> 8x8 self.fc2 = nn.Linear(512, 10) # 10 output classes def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 64 * 8 * 8) # Flatten x = F.relu(self.fc1(x)) x = self.fc2(x) return x # Initialize model, loss function, and optimizer model = SimpleCNN().to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=learning_rate) # Training loop print("Starting training...") for epoch in range(num_epochs): model.train() running_loss = 0.0 for i, (inputs, labels) in enumerate(trainloader): inputs, labels = inputs.to(device), labels.to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() avg_loss = running_loss / len(trainloader) print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {avg_loss:.4f}") # Evaluation on test set model.eval() correct = 0 total = 0 with torch.no_grad(): for inputs, labels in testloader: inputs, labels = inputs.to(device), labels.to(device) outputs = model(inputs) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() accuracy = 100 * correct / total print(f"Test Accuracy: {accuracy:.2f}%") # Save the model (optional) torch.save(model.state_dict(), 'cifar10_model.pth') print("Training complete. Model saved as 'cifar10_model.pth'.")How to Run
- Run the script:
python cifar10_mps.py. - It will download the CIFAR-10 dataset automatically to
./dataif not present. - Training time: On an M1/M2 Mac, expect ~1-2 minutes per epoch with MPS (faster than CPU). Total ~10-20 minutes for 10 epochs.
- Expected accuracy: Around 60-70% with this simple model and 10 epochs. You can improve it by adding more layers, data augmentation (e.g., RandomHorizontalFlip), or training longer.
Tips for Improvement
- Better Model: Use ResNet or increase layers for higher accuracy (e.g., 80%+).
- Augmentation: Add to
transformfor training:transforms.RandomHorizontalFlip(),transforms.RandomCrop(32, padding=4). - Learning Rate Scheduler: Add
torch.optim.lr_scheduler.StepLRfor better convergence. - Monitoring: Use TensorBoard for logging if needed.
- Errors: If MPS isn't available, it falls back to CPU. Ensure PyTorch is built with MPS support (check with
torch.__config__).
If you need modifications (e.g., more epochs, different model), let me know!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.