.is_tensor()
Anonymous contributor
Published Feb 8, 2025
Contribute to Docs
In PyTorch, the .is_tensor()
function checks if the given object is a PyTorch tensor. It returns True
if the object is a tensor; otherwise, it returns False
. This function is part of the torch
module.
Syntax
torch.is_tensor(obj)
obj
: The object to check whether it is a PyTorch tensor.
Example
The following example demonstrates the usage of the .is_tensor()
function:
import torch# Define objectstensor_obj = torch.tensor([11, 22, 33, 44, 55]) # Tensor objectlist_obj = [11, 22, 33, 44, 55] # Non-tensor object# Check if the objects are PyTorch tensorsprint(torch.is_tensor(tensor_obj)) # Trueprint(torch.is_tensor(list_obj)) # False
The above code produces the following output:
TrueFalse
All contributors
- Anonymous contributor
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours