Scalars, Vectors, Matrices, and Tensors

Deep learning uses arrays of numbers. The first difference between arrays is the number of axes they have.

scalar7shape: ()vector123shape: (3)matrix123456shape: (2, 3)batch tensorshape: (4, 2, 3)
A tensor is an array with axes. More axes usually mean more organization, not more mystery.

A scalar is one number.

7

A vector is a one-axis array.

[2, -1, 5]

A matrix is a two-axis array.

[
  [2, -1, 5],
  [0,  3, 4]
]

A tensor is the general word for an array with axes. In practice, people often use tensor for any array in a deep learning system, even a scalar, vector, or matrix.

The important question is not “is this a tensor?” The important question is “what do the axes mean?”

Shapes

Shape records the size of each axis.

[2, -1, 5]                 shape: (3)

[
  [2, -1, 5],
  [0,  3, 4]
]                           shape: (2, 3)

The first shape says: one axis with 3 entries.

The second shape says: 2 rows and 3 columns.

In deep learning, shape is not bookkeeping after the real work. Shape is part of the real work. It tells you what can connect to what.

DL-C01-T01-001Exercise: Read a vector shape

A vector has 6 entries. What is its shape size?

Compute it first, then check your number.

HintThink about axes

A vector has one axis. Count the entries along that axis.

SolutionWork it out

The vector has one axis and 6 entries along that axis, so its shape is (6). The numerical size to enter is 6.

DL-C01-T01-002Exercise: Classify the array

The array below has 3 rows and 2 columns.

[
  [1, 4],
  [2, 5],
  [3, 6]
]

Enter 1 for scalar, 2 for vector, 3 for matrix, or 4 for a three-axis tensor.

Compute it first, then check your number.

HintCount axes

Rows and columns are two axes.

SolutionWork it out

The array has a row axis and a column axis. That makes it a matrix, so the correct code is 3.