Tensor Calculations in Python

Interactive showcase of tensor operations with NumPy and PyTorch

Introduction to Tensors

Tensors are multi-dimensional arrays that generalize scalars, vectors, and matrices to higher dimensions. They are fundamental in machine learning and deep learning frameworks.

NumPy Tensors

NumPy provides powerful N-dimensional array objects that we can use as tensors. Here are some basic operations:

Creating Tensors

import numpy as np

# Scalar (0D tensor)
scalar = np.array(5)

# Vector (1D tensor)
vector = np.array([1, 2, 3])

# Matrix (2D tensor)
matrix = np.array([[1, 2], [3, 4]])

# 3D tensor
tensor_3d = np.array([[[1, 2], [3, 4]], 
                     [[5, 6], [7, 8]]])

Basic Operations

# Element-wise addition
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a + b  # [5, 7, 9]

# Matrix multiplication
mat_a = np.array([[1, 2], [3, 4]])
mat_b = np.array([[5, 6], [7, 8]])
result = np.dot(mat_a, mat_b)

# Reshaping
tensor = np.arange(8)  # [0,1,2,3,4,5,6,7]
reshaped = tensor.reshape((2, 4))

PyTorch Tensors

PyTorch tensors are similar to NumPy arrays but with GPU acceleration support and automatic differentiation capabilities.

Creating PyTorch Tensors

import torch

# Create tensors
scalar = torch.tensor(5)
vector = torch.tensor([1., 2., 3.])
matrix = torch.tensor([[1, 2], [3, 4]])

# GPU tensor (if available)
if torch.cuda.is_available():
    gpu_tensor = torch.tensor([1, 2, 3], device='cuda')

# With gradient tracking
x = torch.tensor(2., requires_grad=True)

Autograd Example

# Automatic differentiation example
x = torch.tensor(2., requires_grad=True)
y = x**2 + 3*x + 1

# Compute gradient
y.backward()

print(x.grad)  # dy/dx = 2x + 3 → 7

Tensor Visualization

Understanding tensor shapes and dimensions is crucial. Here's how different rank tensors look:

Scalar (0D)

5

Shape: ()

Vector (1D)

1
2
3

Shape: (3,)