# Creating Tensors in PyTorch

Let’s begin by importing PyTorch and verifying the version we’re working with.

import torch
torch.__version__

You can create tensors using various methods in PyTorch , let’s break down different types of tensors:

# 0-D Tensor (Scalar)

A scalar is a single number. It’s the simplest form of a tensor.

import torch

# Create a 0-D tensor (scalar)
scalar = torch.tensor(5)
print(scalar)

Output:

tensor(5)

# 1-D Tensor (Vector)

A vector is a one-dimensional tensor that can hold multiple numbers in a single line.

import torch

# Create a 1-D tensor (vector)
vector = torch.tensor([1, 2, 3, 4])
print(vector)

Output:

tensor([1, 2, 3, 4])

# 2-D Tensor (Matrix)

A matrix is a two-dimensional tensor. It has rows and columns, like a table of numbers.

import torch

# Create a 2-D tensor (matrix)
matrix = torch.tensor([[1, 2], [3, 4]])
print(matrix)

Output:

 tensor([[1, 2],
         [3, 4]])

# 3-D Tensor

A 3-D tensor is like a stack of matrices. It has depth, along with rows and columns.

import torch

# Create a 3-D tensor
tensor_3d = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(tensor_3d)

Output:

 tensor([[[1, 2],
         [3, 4]],

        [[5, 6],
         [7, 8]]])

Let’s review the key points

  • 0-D Tensor (Scalar): A single number.
  • 1-D Tensor (Vector): A list of numbers.
  • 2-D Tensor (Matrix): A grid of numbers with rows and columns.
  • 3-D Tensor: A stack of matrices.

# Random Tensors

In machine learning, tensors are often initialized with random values. This randomness is useful for initializing model parameters before training. PyTorch provides a function to generate tensors with random numbers:

import torch

# Create a 2x3 tensor with random values between 0 and 1
random_tensor = torch.rand(2, 3)
print("Random Tensor:")
print(random_tensor)

Output:

Random Tensor:
tensor([[0.5647, 0.1234, 0.9987],
        [0.4568, 0.7890, 0.3456]])

In this example, torch.rand(2, 3) creates a tensor of shape [2, 3] with random values between 0 and 1.

# Zeros and Ones

Sometimes, you need tensors filled with zeros or ones, especially when initializing weights or masks. PyTorch offers functions to create such tensors:

import torch

# Create a 2x3 tensor filled with zeros
zeros_tensor = torch.zeros(2, 3)
print("Zeros Tensor:")
print(zeros_tensor)

# Create a 2x3 tensor filled with ones
ones_tensor = torch.ones(2, 3)
print("Ones Tensor:")
print(ones_tensor)

Output:

Zeros Tensor:
tensor([[0., 0., 0.],
        [0., 0., 0.]])

Ones Tensor:
tensor([[1., 1., 1.],
        [1., 1., 1.]])

Here, torch.zeros(2, 3) creates a tensor of shape [2, 3] filled with zeros, while torch.ones(2, 3) creates one filled with ones.

# Creating a Range of Values

Sometimes, you might need to create a tensor with a range of values. This is useful for creating sequences or grids of values:

import torch

# Create a tensor with values from 0 to 4
range_tensor = torch.arange(5)
print("Range Tensor:")
print(range_tensor)

Output:

Range Tensor:
tensor([0, 1, 2, 3, 4])

The torch.arange(5) function creates a tensor with values [0, 1, 2, 3, 4].


Sure! Here are two exercises focusing on 1-D and 2-D tensors, with their solutions provided:


# Exercise 1: Creating and Inspecting Tensors

Task:

  1. Create a 2-D tensor with the following values:
    [[2, 4, 6],
     [8, 10, 12]]
  2. Create a 1-D tensor with values ranging from 0 to 4.
  3. Print both tensors .
import torch

# 1. Create a 2-D tensor
matrix = torch.tensor([[2, 4, 6], [8, 10, 12]])
print("2-D Tensor:")
print(matrix)

# 2. Create a 1-D tensor with values from 0 to 4
vector = torch.arange(5)
print("1-D Tensor:")
print(vector)

# Exercise 2: Tensor Initialization and Operations

Task:

  1. Create a 2-D tensor of shape [3, 2] filled with zeros.
  2. Create a 2-D tensor of shape [3, 2] filled with ones.
  3. Create a 1-D tensor with 6 random values between 0 and 1.
  4. Print all three tensors.
import torch

# 1. Create a 2-D tensor filled with zeros
zeros_tensor = torch.zeros(3, 2)
print("2-D Tensor filled with zeros:")
print(zeros_tensor)

# 2. Create a 2-D tensor filled with ones
ones_tensor = torch.ones(3, 2)
print("2-D Tensor filled with ones:")
print(ones_tensor)

# 3. Create a 1-D tensor with random values between 0 and 1
random_tensor = torch.rand(6)
print("1-D Tensor with random values:")
print(random_tensor)