Tutorial

Types, Shapes & Tensors

1

D

Alrighty, let's get started.

2

W

Can't wait!

3

D

What is 5?

4

W

Is 5 an int?

5

D

Excellent! Do you know other ints?

6

W

Hmm... 0 and -42?

7

D

They are ints too.

8

W

What about -42.42?

9

D

-42.42 is a float, different from int.

10

W

How?

11

D

Machines use different formats to store int values and float values. They may behave differently under the same mathematical operations.*
So they are different types. A type describes a set of values that share the same behavior.

12

W

Interesting. Are there values of other types?

13

D

Here's one: [1, 2, 3].

14

W

It groups three ints together! What do we call it?

15

D

We call [1, 2, 3] a List[int].

16

W

So List[t] is a type as long as t is a type.
Then there must be a type called List[List[int]], right?

17

D

Yes, Lists can be nested, such as [[1, 2, 3], [4, 5, 6]].

18

W

What about [[1, 2, 3], [4, 5]]?

19

D

That's also a List[List[int]], since its first element is a List[int]...

20

W

... because all elements inside that element are ints.
Then its second element is also a List[int], since all elements there are ints.
Phew... that's a lot of reasoning.

21

D

Fortunately, machines can do that reasoning for us, rigorously and efficiently.
Now there is a stricter way to group things.

22

W

Go for it!

23

D

Tensor([[1, 2, 3], [4, 5, 6]])
24

W

It just wraps this Tensor thing around the list.** How is it different?

25

D

It has the type Tensor[int][[2, 3]]. A Tensor knows two things about its elements: their type and their shape.

26

W

[2, 3] is a List[int] that describes the shape of Tensor([[1, 2, 3], [4, 5, 6]]), since the outer layer contains two List[int]s and each inner layer contains three ints?

27

D

Not quite, but close.
Tensor([[1, 2, 3], [4, 5, 6]]) is the shorter way to write Tensor([Tensor([1, 2, 3]), Tensor([4, 5, 6])]).
It contains two elements; both have type Tensor[int][[3]].
Each element also contains three elements; all have type Tensor[int][[]].

28

W

I see.
So int and Tensor[int][[]] are the same type.
Tensor[int][[2, 3]] and Tensor[Tensor[int][[3]]][[2]] are also the same type.

29

D

By convention, values of int or float are called scalars.

30

W

Can we make [[1, 2, 3], [4, 5]] a Tensor?

31

D

Good question. What's its shape?

32

W

Hmm... I don't know how to describe its shape.

33

D

Neither do I. We cannot make [[1, 2, 3], [4, 5]] a Tensor, since it does not have a shape!

34

W

So Tensors are special because of shapes!

35

D

Yes. Tensor[t][s] is a type as long as t is a type and s is a List[int].
With shapes, Tensors describe their values more accurately than Lists do.
This precision enables many cool things.

36

W

Such as?

37

D

Running programs efficiently in parallel.
Since all elements in a tensor share the same shape, it is easier to run commands on all of them together.

38

W

Sounds cool! Let's see an example!

39

D

We will, in the next chapter. Now it's time to take a break.

40

W

See you there!

* In this tutorial, we use int for 64-bit integers and float for 64-bit floating-point numbers for simplicity. In practice, people prefer smaller and more efficient variants, such as float32 or bfloat16, which are available in PyPie.
** Tensor is available through from pypie import Tensor.