VOOZH about

URL: https://dzone.com/articles/norm-of-a-one-dimensional-tensor-in-python-libraries

โ‡ฑ Norm of a One-Dimensional Tensor in Python Libraries


Related

  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Norm of a One-Dimensional Tensor in Python Libraries

Norm of a One-Dimensional Tensor in Python Libraries

Learn how to calculate the Euclidean (norm/distance) of a single-dimensional (1D) tensor in NumPy, SciPy, Scikit-Learn, TensorFlow, and PyTorch.

Likes
Comment
Save
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

The calculation of the norm of vectors is essential in both artificial intelligence and quantum computing for tasks such as feature scaling, regularization, distance metrics, convergence criteria, representing quantum states, ensuring unitarity of operations, error correction, and designing quantum algorithms and circuits.

You will learn how to calculate the Euclidean (norm/distance), also known as the L2 norm, of a single-dimensional (1D) tensor in Python libraries like NumPy, SciPy, Scikit-Learn, TensorFlow, and PyTorch

Understand Norm vs Distance

Before we begin, let's understand the difference between Euclidean norm vs Euclidean distance.

  1. Norm is the distance/length/size of the vector from the origin (0,0).
  2. Distance is the distance/length/size between two vectors.

Prerequisites

  • Install Jupyter.
  • Run the code below in a Jupyter Notebook to install the prerequisites.
Python
# Install the prerequisites for you to run the notebook
!pip install numpy
!pip install scipy
%pip install torch
!pip install tensorflow


You will use Jupyter Notebook to run the Python code cells to calculate the L2 norm in different Python libraries.

Let's Get Started

Now that you have Jupyter set up on your machine and installed the required Python libraries, let's get started by defining a 1D tensor using NumPy.

NumPy

NumPy is a Python library used for scientific computing. NumPy provides a multidimensional array and other derived objects.

Tensor ranks

Python
# Define a single dimensional (1D) tensor 
import numpy as np

vector1 = np.array([3,7]) #np.random.randint(1,5,2)
vector2 = np.array([5,2]) #np.random.randint(1,5,2)
print("Vector 1:",vector1)
print("Vector 2:",vector2)
print(f"shape & size of Vector1 & Vector2:", vector1.shape, vector1.size)

Print the vectors


Plain Text
Vector 1: [3 7]
Vector 2: [5 2]
shape & size of Vector1 & Vector2: (2,) 2


Matplotlib 

Matplotlib is a Python visualization library for creating static, animated, and interactive visualizations. You will use Matplotlib's quiver to plot the vectors.

Python
# Draw the vectors using MatplotLib
import matplotlib.pyplot as plt
%matplotlib inline

origin = np.array([0,0])
plt.quiver(*origin, vector1[0],vector1[1], angles='xy', color='r', scale_units='xy', scale=1)
plt.quiver(*origin, vector2[0],vector2[1], angles='xy', color='b', scale_units='xy', scale=1)
plt.plot([vector1[0],vector2[0]], [vector1[1],vector2[1]], 'go', linestyle="--")
plt.title('Vector Representation') 

plt.xlim([0,10])
plt.ylim([0,10])

plt.grid() 
plt.show()


Vector representation using Matplolib

Python
# L2 (Euclidean) norm of a vector
# NumPy
norm1 = np.linalg.norm(vector1, ord=2)
print("The magnitude / distance from the origin",norm1)

norm2 = np.linalg.norm(vector2, ord=2)
print("The magnitude / distance from the origin",norm2)


The output once you run this in the Jupyter Notebook:

Plain Text
The magnitude / distance from the origin 7.615773105863909
The magnitude / distance from the origin 5.385164807134504


SciPy

SciPy is built on NumPy and is used for mathematical computations. If you observe, SciPy uses the same linalg functions as NumPy.

Python
# SciPy
import scipy 
norm_vector1 = scipy.linalg.norm(vector1, ord=2)
print("L2 norm in scipy for vector1:", norm_vector1)

norm_vector2 = scipy.linalg.norm(vector2, ord=2)
print("L2 norm in scipy for vector2:", norm_vector2)


Output: 

Plain Text
L2 norm in scipy for vector1: 7.615773105863909
L2 norm in scipy for vector2: 5.385164807134504


Scikit-Learn

As the Scikit-learn documentation says:

Scikit-learn is an open source machine learning library that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection, model evaluation, and many other utilities.

We reshape the vector as Scikit-learn expects the vector to be 2-dimensional.

Python
# Sklearn
from sklearn.metrics.pairwise import euclidean_distances
vector1_reshape = vector1.reshape(1,-1)
## Scikit-learn expects the vector to be 2-Dimensional
euclidean_distances(vector1_reshape, [[0, 0]])[0,0]


Output

Plain Text
7.615773105863909


TensorFlow

TensorFlow is an end-to-end machine learning platform. 

Python
# TensorFlow
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' 

import tensorflow as tf
print("TensorFlow version:", tf.__version__)

## Tensorflow expects Tensor of types float32, float64, complex64, complex128
vector1_tf = vector1.astype(np.float64)
tf_norm = tf.norm(vector1_tf, ord=2)
print("Euclidean(l2) norm in TensorFlow:",tf_norm.numpy())


Output

The output prints the version of TensorFlow and the L2 norm:

Plain Text
TensorFlow version: 2.15.0
Euclidean(l2) norm in TensorFlow: 7.615773105863909


PyTorch

PyTorch is an optimized tensor library for deep learning using GPUs and CPUs.

Python
# PyTorch
import torch
print("PyTorch version:", torch.__version__)

norm_torch = torch.linalg.norm(torch.from_numpy(vector1_tf), ord=2)
norm_torch.item()


The output prints the PyTorch version and the norm:

Plain Text
PyTorch version: 2.1.2

7.615773105863909


Euclidean Distance

Euclidean distance is calculated in the same way as a norm, except that you calculate the difference between the vectors before passing the difference - vector_diff, in this case, to the respective libraries.

Python
# Euclidean distance between the vectors
import math
vector_diff = vector1 - vector2

# Using norm
euclidean_distance = np.linalg.norm(vector_diff, ord=2)
print(euclidean_distance)

# Using dot product
norm_dot = math.sqrt(np.dot(vector_diff.T,vector_diff))
print(norm_dot)


Output

Output using the norm and dot functions of NumPy libraries:

Plain Text
5.385164807134504
5.385164807134504


Python
# SciPy
from scipy.spatial import distance
distance.euclidean(vector1,vector2)


Output Using SciPy

5.385164807134504


The Jupyter Notebook with the outputs is available on the GitHub repository. You can run the Jupyter Notebook on Colab following the instructions on the GitHub repository.

NumPy SciPy TensorFlow jupyter notebook Norm (artificial intelligence) Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Essential Python Libraries: Introduction to NumPy and Pandas
  • Step-By-Step Guide To Setting up and Training GANs for Image Generation
  • How To Implement Cosine Similarity in Python
  • Improving Sentiment Score Accuracy With FinBERT and Embracing SOLID Principles

Partner Resources

ร—

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: