The torch Tensor add() function is a built-in method in the PyTorch library which is used to perform element-wise addition on two tensors. It takes two tensors with the same shape as input and returns a new tensor that contains the sum of the corresponding elements.
Example 1: Adding two tensors using add() function
import torch x = torch.tensor([1, 2, 3]) y = torch.tensor([4, 5, 6]) z = torch.add(x, y) print(z)
Output: tensor([5, 7, 9])
Example 2: Adding a scalar value to a tensor using add() function
import torch x = torch.tensor([1, 2, 3]) z = torch.add(x, 1) print(z)
Output: tensor([2, 3, 4])
The above code examples use the PyTorch library in Python.
Python Tensor.add - 22 examples found. These are the top rated real world Python examples of torch.Tensor.add extracted from open source projects. You can rate examples to help us improve the quality of examples.