🎓 A1Learner
🔍

PyTorch Data Augmentation on Tensors

Data augmentation is the practice of making new training examples by changing old ones slightly. A flipped or shifted image still carries the same label, so one dataset can teach more.

A flip

flip reverses the order along one axis. The L shape leans the other way after the flip.

import torch

image = torch.tensor([[1.0, 0.0, 0.0],
                      [1.0, 0.0, 0.0],
                      [1.0, 1.0, 0.0]])
print(torch.flip(image, dims=[1]).tolist())

A shift

roll moves every value by one step. Values that leave one edge come back on the other edge.

import torch

image = torch.tensor([[1.0, 0.0, 0.0],
                      [1.0, 0.0, 0.0],
                      [1.0, 1.0, 0.0]])
print(torch.roll(image, shifts=1, dims=[1]).tolist())

A little noise

Noise adds a small random amount to each value. The seed is set first, so the same noisy version appears on every run.

import torch

image = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
torch.manual_seed(0)
noisy = image + torch.randn(2, 2) * 0.1
print([[round(v, 2) for v in row] for row in noisy.tolist()])

Example

import torch


def show(name, image):
    print(name)
    for row in image:
        print("".join("#" if value > 0.5 else "." for value in row))


image = torch.zeros(6, 6)
image[1:5, 1] = 1.0
image[4, 1:4] = 1.0

show("Original:", image)
show("Flipped:", torch.flip(image, dims=[1]))
show("Shifted right:", torch.roll(image, shifts=1, dims=[1]))

torch.manual_seed(2)
noisy = image + torch.randn(6, 6) * 0.45
show("With noise:", (noisy > 0.5).float())

Key points

  • Augmentation: a small change to an example that keeps its label true
  • Use torch.flip(x, dims=[...]) to reverse a tensor along an axis
  • Use torch.roll(x, shifts=..., dims=[...]) to shift values, wrapping them around the edge
  • Noise: small random values added to the data, drawn here with torch.randn
  • Set a seed before drawing noise, so an augmented example can be reproduced
  • Augmentation makes more training variety from the data already collected, with no new files

In this example, one 6 by 6 image holds an L shape made of ones. A show function prints each cell as a hash or a dot. The flip mirrors the L to the right side of the grid. The roll shift moves the whole L one column to the right. For the noise form, the seed is set to 2 and random values are added. The noisy grid keeps the full L and gains one stray hash near the top. All four forms still show the same L, so all four keep the same label.

Note

Only changes that keep the label true are safe, and a flipped digit may change its meaning.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Why does a flipped image usually keep the same label?