liveBook · Manning

Scalar

As explained in Deep Learning with PyTorch, Second Edition

A single numeric value, contrasted here with multi-dimensional feature representations.

A scalar is a single numeric value. In PyTorch, a scalar is represented as a zero-dimensional tensor, making it suitable for model parameters such as a weight and bias. section 5.3.1

Scalars work naturally with tensors through broadcasting: multiplying a scalar scales every element, while adding one offsets every element. Broadcasting also aligns compatible dimensions, including dimensions of size 1. section 5.3.1 section 3.4

Scalar parameters in a PyTorch model

The zero-dimensional tensors w and b broadcast across the input vector t_u: w * t_u + b. A mean operation can then reduce element-wise squared differences to a scalar loss. section 5.3.1

# In[5]:
w = torch.ones(())
b = torch.zeros(())

t_p = model(t_u, w, b)
t_p

# Out[5]:
tensor([35.7000, 55.9000, 58.2000, 81.9000, 56.3000, 48.9000, 33.9000,
        21.8000, 48.4000, 60.4000, 68.4000])

Inspect scalar tensor initialization and its broadcasted use as model weight and bias. section 5.3.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains zero-dimensional representation and broadcasting mechanics, while deferring additional broadcasting practice.
Examples
Demonstrates scalar addition, multiplication, model parameters, mean squared loss, and TensorBoard scalar plots.
What you can do after
Apply scalar parameters and operations in PyTorch models using broadcasting and tensor reduction.
How it connects
Connects scalars to tensors, broadcasting, model parameters, loss functions, and TensorBoard scalar displays.

Also explained in

Compare how 4 books explain scalar →

Related concepts

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