liveBook · Manning

Tensor shape

As explained in Deep Learning with R, Third Edition

An output shape is the ordered set of dimension sizes describing the tensor produced by a layer.

A tensor shape is the ordered list of integer sizes along its axes. A matrix with shape (2, 3) has two axes, containing two rows and three columns; a rank-3 tensor with shape (4, 3, 2) has three dimensions. Vectors have one size, such as (5), while scalars have an empty shape, (). section 2.2.5

MNIST image data

The MNIST training images have shape (60000, 28, 28): 60,000 grayscale images, each represented by a 28 × 28 matrix. The first dimension indexes samples, and the remaining dimensions describe each image. section 2.2.5

Common data layouts make the role of each dimension explicit:

  • Vector data uses rank-2 tensors shaped (samples, features), such as (100000, 3) for people described by three attributes. section 2.2.8
  • Sequence data uses rank-3 tensors shaped (samples, timesteps, features), with time conventionally occupying the second axis. section 2.2.8
  • Images use rank-4 tensors shaped (samples, height, width, channels), while video uses rank-5 tensors shaped (samples, frames, height, width, channels). section 2.2.8
op_shape(train_images)

shape(60000, 28, 28)

Inspect a tensor’s shape directly with op_shape(train_images), which reports shape(60000, 28, 28). section 2.2.5

In Keras, layer output shapes help verify that a model’s dimensions change as intended. Declaring input_shape builds a Sequential model early, allowing summaries to display each layer’s output shape and supporting debugging of complex transformations such as convolutions. section 7.2.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines ordered dimensions, works through MNIST and data-layout examples, and applies output-shape summaries to model debugging.
Examples
Shows MNIST images, vector datasets, sequences, images, video, and Keras model summaries with changing output shapes.
What it assumes
Assumes no prior tensor knowledge and introduces axes, rank, shapes, data layouts, and shape inspection from concrete examples.
What you can do after
Diagnose/debug with it by inspecting tensor dimensions and checking layer output shapes before or during model construction.
How it connects
Connects tensor shape with rank, dtype, tensors, input shape, layers, Sequential models, and convolutional layers.

Also explained in

Compare how 5 books explain tensor shape →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.