Menu

Scalars – Understanding Scalars (Zero-Order Tensors)

Understanding Scalars (Zero-Order Tensors)

Written by Jagdeesh | 3 min read

What is a Scalar?

A scalar is the simplest form of a tensor. It’s a single number, without direction. Scalars contrast with higher order tensors like vectors (1st order), matrices (2nd order), and so on. In other words, a scalar has zero dimensions.

Why are Scalars Important?

Foundation of Math Operations: When we work with high-dimensional data, the operations often boil down to scalar computations. For instance, when you multiply two matrices, the individual operations involve multiplying scalars.

Understanding Basic Properties: Concepts like magnitude, units, and identity elements are best understood using scalars before they’re applied to vectors and matrices.

Performance Metrics: In machine learning, metrics such as loss, accuracy, or precision are often represented as scalars.

Interactions with Higher-Order Tensors

Scalars frequently interact with vectors, matrices, and higher-order tensors. For instance:

Scalar Multiplication: Multiplying a matrix by a scalar involves multiplying each element of the matrix by the scalar.

NumPy:

python
matrix = np.array([[1, 2], [3, 4]])
result = matrix * 5
print(result)  
python
[[ 5 10]
 [15 20]]

Advanced Properties of Scalars

Identity: The number 1 is often called a multiplicative identity because multiplying any number by 1 doesn’t change that number. Likewise, 0 is an additive identity because adding 0 to a number doesn’t change it.

Inverse: Every scalar has a multiplicative inverse, such that when it’s multiplied by its inverse, the result is 1. For example, the inverse of 5 is 1/5

Absolute Value: It represents the magnitude of a scalar. In many libraries, it’s computed using the abs function.

Scalars in Different Frameworks

We will be illustrating scalar operations in three prominent libraries:

  • NumPy
  • PyTorch
  • TensorFlow

Scalars in NumPy

python
import numpy as np

# Creating a scalar
scalar = np.array(5)
print(scalar)  # Output: 5

# Checking its dimensions
print(scalar.shape)  # Output: ()
python
5
()

Scalars in PyTorch

python
import torch

# Creating a scalar
scalar = torch.tensor(5)
print(scalar)  # Output: tensor(5)

# Checking its dimensions
print(scalar.size())  # Output: torch.Size([])
python
tensor(5)
torch.Size([])

Scalars in TensorFlow

python
import tensorflow as tf

# Creating a scalar
scalar = tf.constant(5)
print(scalar)  # Output: tf.Tensor(5, shape=(), dtype=int32)

# Checking its dimensions
print(scalar.shape)  # Output: ()
python
tf.Tensor(5, shape=(), dtype=int32)
()

Scalar Operations

Addition

NumPy:

python
result = np.add(5, 3)
print(result)  # Output: 8
python
8

PyTorch:

python
result = torch.add(torch.tensor(5), torch.tensor(3))
print(result)  # Output: tensor(8)
python
tensor(8)

TensorFlow:

python
result = tf.add(tf.constant(5), tf.constant(3))
print(result)  # Output: tf.Tensor(8, shape=(), dtype=int32)
python
tf.Tensor(8, shape=(), dtype=int32)

Multiplication

NumPy:

python
result = np.multiply(5, 3)
print(result)  # Output: 15
python
15

PyTorch:

python
result = torch.mul(torch.tensor(5), torch.tensor(3))
print(result)  # Output: tensor(15)
python
tensor(15)

TensorFlow:

python
result = tf.multiply(tf.constant(5), tf.constant(3))
print(result)  # Output: tf.Tensor(15, shape=(), dtype=int32)
python
tf.Tensor(15, shape=(), dtype=int32)

Division

NumPy:

python
result = np.divide(10, 2)

print(result)  # Output: 5.0
python
5.0

PyTorch:

python
result = torch.div(torch.tensor(10), torch.tensor(2))
print(result)  # Output: tensor(5)
python
tensor(5.)

TensorFlow:

python
result = tf.divide(tf.constant(10), tf.constant(2))
print(result)  # Output: tf.Tensor(5.0, shape=(), dtype=float64)
python
tf.Tensor(5.0, shape=(), dtype=float64)

Conclusion

More you understand about scalars, the more solid your foundational knowledge will be as you delve into vectors, matrices, and more advanced multi-dimensional tensors. Whether you’re working with NumPy, TensorFlow, or PyTorch, remember the essential role these zero-order tensors play in complex operations and computations.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master Linear Algebra — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science