Generate sample image dataset using PyTorch

Here is some sample code using PyTorch for generating a sample dataset using the MNIST dataset as an example:

import torch
import torchvision
from torchvision import transforms

# define the transformations to be applied to the data
transform = transforms.Compose([
    transforms.Normalize((0.5,), (0.5,))

# load the MNIST dataset
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader =, batch_size=64, shuffle=True)

# define a function to generate new samples
def generate_samples(num_samples):
    samples = []
    for i in range(num_samples):
        # randomly sample a batch of images from the dataset
        images, labels = iter(trainloader).next()
        # flatten the images
        images = images.view(images.size(0), -1)
        # add the images to the list of samples
    # concatenate the samples into a single tensor
    samples =, dim=0)
    return samples

# generate 100 new samples
new_samples = generate_samples(100)

In this code, we first define the transformations to be applied to the data, which includes converting the data to a PyTorch tensor and normalizing the pixel values. We then load the MNIST dataset and define a PyTorch DataLoader to sample batches of data from the dataset.

We then define a function generate_samples that takes in a number of samples to generate and randomly samples batches of data from the DataLoader. We flatten the images and add them to a list of samples, which we concatenate into a single tensor and return.

Finally, we generate 100 new samples using the generate_samples function and print the shape of the resulting tensor.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.