Skip to content

Instantly share code, notes, and snippets.

@V0XNIHILI
Last active September 28, 2022 19:30
Show Gist options
  • Save V0XNIHILI/15ecdbf7521e0d427cdfe5afa691a70f to your computer and use it in GitHub Desktop.
Save V0XNIHILI/15ecdbf7521e0d427cdfe5afa691a70f to your computer and use it in GitHub Desktop.
Simple sequential MNIST dataset implementation in Python and PyTorch for usage in recurrent neural networks. See for project using this dataset here: https://github.com/V0XNIHILI/LSTM-Sequential-MNIST
import torchvision
import torchvision.transforms as transforms
MNIST_MEAN = 0.1307
MNIST_STD = 0.3081
mnist_transforms = transforms.Compose([transforms.ToTensor(), transforms.Normalize((MNIST_MEAN,), (MNIST_STD,))])
train_data = SequentialMNIST(torchvision.datasets.MNIST('.', train=True, download=True, transform=mnist_transforms))
train_data_loader = DataLoader(train_data, batch_size=64)
import torch
import torch.nn as nn
import torchvision
from torch.utils.data import Dataset
class SequentialMNIST(Dataset):
def __init__(self, MNIST_dataset: torchvision.datasets.MNIST):
self.MNIST_dataset = MNIST_dataset
def __len__(self):
return len(self.MNIST_dataset)
def __getitem__(self, idx: int):
image, target = self.MNIST_dataset[idx]
# Return flattened image in shape (time steps = 784, dimensionality = 1)
return image.view(-1, 1), target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment