tensor.add(tensor2)
– Element-wise addition (can also use+
).tensor.sub(tensor2)
– Element-wise subtraction (can also use-
).tensor.mul(tensor2)
– Element-wise multiplication (can also use*
).tensor.div(tensor2)
– Element-wise division (can also use/
).torch.mm(tensor1, tensor2)
– Matrix multiplication.tensor.sum()
– Returns the sum of all elements.tensor.mean()
– Returns the mean of all elements.tensor.max()
– Returns the maximum value.tensor.min()
– Returns the minimum value.torch.cat(tensors, dim)
– Concatenates a sequence of tensors along a given dimension.torch.stack(tensors)
– Stacks tensors along a new dimension.
Here are examples for the basic Tensor Operations in PyTorch:
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)
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)
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)
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)
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]])
tensor.sum()
(Sum of all elements)
tensor = torch.tensor([1, 2, 3])
result = tensor.sum()
print(result)
Output:
tensor(6)
tensor.mean()
(Mean of all elements)
tensor = torch.tensor([1.0, 2.0, 3.0])
result = tensor.mean()
print(result)
Output:
tensor(2.)
tensor.max()
(Maximum value)
tensor = torch.tensor([1, 2, 3])
result = tensor.max()
print(result)
Output:
tensor(3)
tensor.min()
(Minimum value)
tensor = torch.tensor([1, 2, 3])
result = tensor.min()
print(result)
Output:
tensor(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]])
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.