PythiloLesson 1 of 18 · progress kept in this browser

Tensors

A tensor is PyTorch's array: numbers of one type, in a shape. It works like a NumPy array, and it can also remember how it was computed, which is what lets a network learn.

import torch

x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(x.shape)        # torch.Size([2, 2])
print(x.dtype)        # torch.float32
print(x.mean(dim=0))  # tensor([2., 3.])
print(x[:, 1])        # second column

dim=0 runs down the rows, so the result has one value per column. Operations between a (rows, cols) tensor and a (cols,) tensor apply the second to every row: this is broadcasting.

Models expect inputs on similar scales. Standardising each column, so it has mean 0 and standard deviation 1, is the usual first step.

Your task

Write standardize(x) returning x with every column shifted to mean 0 and scaled to standard deviation 1 (use unbiased=False). Write first_rows(x, n) returning the first n rows.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.

Next: Autograd