PyTorch .positive()
Published Oct 31, 2025
Contribute to Docs
The .positive() method in PyTorch returns a new tensor containing the same values as the input tensor. It implements the unary plus operation (+tensor), creating a copy of the tensor without modifying the values. This method is useful when an explicit copy or consistency with other unary operations in mathematical expressions is needed.
Syntax
torch.positive(input)
Parameters:
input(Tensor): The input tensor.
Return value:
The .positive() method returns a new tensor with the same values and data type as the input tensor.
Example
This example demonstrates how to use the .positive() method to create a copy of a tensor:
import torch# Define a tensor with positive and negative valuestensor = torch.tensor([[-3.5, 2.1], [0.0, -7.8]])# Apply the positive operationpositive_tensor = torch.positive(tensor)# Verify they contain the same valuesprint("Original Tensor:")print(tensor)print("\nPositive Tensor:")print(positive_tensor)# Check if they are the same objectprint("\nAre they the same object?", tensor is positive_tensor)
This example results in the following output:
Original Tensor:tensor([[-3.5000, 2.1000],[ 0.0000, -7.8000]])Positive Tensor:tensor([[-3.5000, 2.1000],[ 0.0000, -7.8000]])Are they the same object? False
In this example:
- Value Preservation: All values remain unchanged, including negative numbers (-3.5 stays -3.5, -7.8 stays -7.8).
- New Tensor: The
.positive()method creates a new tensor object, as confirmed by theiscomparison returningFalse. - Use Case: This operation is equivalent to the unary
+operator and is primarily used for creating explicit copies or maintaining consistency in mathematical operations.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours