PyTorch Tensor Operations song & examples

  1. tensor.add(tensor2) – Element-wise addition (can also use +).
  2. tensor.sub(tensor2) – Element-wise subtraction (can also use -).
  3. tensor.mul(tensor2) – Element-wise multiplication (can also use *).
  4. tensor.div(tensor2) – Element-wise division (can also use /).
  5. torch.mm(tensor1, tensor2) – Matrix multiplication.
  6. tensor.sum() – Returns the sum of all elements.
  7. tensor.mean() – Returns the mean of all elements.
  8. tensor.max() – Returns the maximum value.
  9. tensor.min() – Returns the minimum value.
  10. torch.cat(tensors, dim) – Concatenates a sequence of tensors along a given dimension.
  11. torch.stack(tensors) – Stacks tensors along a new dimension.

Here are examples for the basic Tensor Operations in PyTorch:

  1. tensor.add(tensor2) (Element-wise addition)
import torch

tensor1 = torch.tensor([1, 2, 3])
tensor2 = torch.tensor([4, 5, 6])
result = tensor1.add(tensor2)
print(result)

Output:

tensor([5, 7, 9])

Alternatively, you can use +:

result = tensor1 + tensor2
print(result)

  1. tensor.sub(tensor2) (Element-wise subtraction)
result = tensor1.sub(tensor2)
print(result)

Output:

tensor([-3, -3, -3])

Alternatively, you can use -:

result = tensor1 - tensor2
print(result)

  1. tensor.mul(tensor2) (Element-wise multiplication)
result = tensor1.mul(tensor2)
print(result)

Output:

tensor([ 4, 10, 18])

Alternatively, you can use *:

result = tensor1 * tensor2
print(result)

  1. tensor.div(tensor2) (Element-wise division)
result = tensor1.div(tensor2)
print(result)

Output:

tensor([0.2500, 0.4000, 0.5000])

Alternatively, you can use /:

result = tensor1 / tensor2
print(result)

  1. torch.mm(tensor1, tensor2) (Matrix multiplication)
tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[5, 6], [7, 8]])
result = torch.mm(tensor1, tensor2)
print(result)

Output:

tensor([[19, 22],
        [43, 50]])

  1. tensor.sum() (Sum of all elements)
tensor = torch.tensor([1, 2, 3])
result = tensor.sum()
print(result)

Output:

tensor(6)

  1. tensor.mean() (Mean of all elements)
tensor = torch.tensor([1.0, 2.0, 3.0])
result = tensor.mean()
print(result)

Output:

tensor(2.)

  1. tensor.max() (Maximum value)
tensor = torch.tensor([1, 2, 3])
result = tensor.max()
print(result)

Output:

tensor(3)

  1. tensor.min() (Minimum value)
tensor = torch.tensor([1, 2, 3])
result = tensor.min()
print(result)

Output:

tensor(1)

  1. torch.cat(tensors, dim) (Concatenation along a given dimension)
tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[5, 6], [7, 8]])
result = torch.cat((tensor1, tensor2), dim=0)  # Concatenate along rows
print(result)

Output:

tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])

Concatenating along columns (dim=1):

result = torch.cat((tensor1, tensor2), dim=1)
print(result)

Output:

tensor([[1, 2, 5, 6],
        [3, 4, 7, 8]])

  1. torch.stack(tensors) (Stack tensors along a new dimension)
tensor1 = torch.tensor([1, 2])
tensor2 = torch.tensor([3, 4])
result = torch.stack((tensor1, tensor2), dim=0)  # Stacks tensors into a 2D array
print(result)

Output:

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

Alternatively, stack along a new dimension:

result = torch.stack((tensor1, tensor2), dim=1)
print(result)

Output:

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

These examples demonstrate basic tensor operations in PyTorch, which are fundamental for tensor manipulation, especially in deep learning.


Discover more from Science Comics

Subscribe to get the latest posts sent to your email.

Leave a Reply

error: Content is protected !!