If you’re working with PyTorch, you know how central tensors are to everything — from model training to deep learning computations. But when writing complex code or debugging, you often need to verify whether a variable is actually a tensor.
That’s where torch.is_tensor() comes in handy!
In this guide, we’ll explore everything you need to know about the torch.is_tensor() function — including its syntax, examples, use cases, and best practices.
In simple terms, torch.is_tensor() is a built-in PyTorch function that checks whether a given object is a tensor or not.
It returns a Boolean value:
True → if the object is a PyTorch tensor
False → if the object is not a tensor
This is useful when you’re writing dynamic code or data pipelines where the input type might not be guaranteed.
Parameters:
obj: The object you want to check.
Returns:
bool: True if obj is a tensor, otherwise False.
Let’s look at a quick example to understand how it works.
Explanation:
x is a PyTorch tensor → so the output is True
y is a Python list, not a tensor → output is False
When building neural networks, preprocessing data, or debugging models, you may not always know what type of object your function receives.
Here’s why torch.is_tensor() is incredibly useful:
✅ Error Prevention – Prevents type-related runtime errors.
⚙️ Flexible Code – Helps create adaptable and reusable functions.
🧪 Debugging Aid – Quickly confirm data types while troubleshooting.
🧱 Data Validation – Ensures input data matches the expected tensor format.
🚀 Saves Time – Reduces manual checks and unnecessary crashes.
Let’s say you’re writing a custom function to normalize input data. You want it to work whether the input is a tensor or not.
Explanation:
If the input is already a tensor → normalize directly.
If not → convert it to a tensor before normalizing.
This makes your function robust and flexible for various input types.
PyTorch provides a few similar utilities, and it’s easy to confuse them. Let’s clarify the difference.
| Function | Purpose | Returns |
|---|---|---|
torch.is_tensor(obj) |
Checks if obj is a PyTorch tensor |
True or False |
torch.is_storage(obj) |
Checks if obj is a storage object |
True or False |
isinstance(obj, torch.Tensor) |
Python’s built-in method for class checking | True or False |
💡 Pro Tip:
You can use torch.is_tensor(obj) and isinstance(obj, torch.Tensor) interchangeably in most cases. However, torch.is_tensor() is more readable and consistent across PyTorch’s API.
PyTorch supports multiple tensor types such as:
torch.FloatTensor
torch.IntTensor
torch.DoubleTensor
torch.cuda.FloatTensor (for GPU)
The good news?torch.is_tensor() works for all of them!
This makes it an all-in-one tensor detector regardless of the device or data type.
Here’s a more practical use case — ensuring safe tensor operations in custom modules:
✅ Why it’s good practice:
You ensure both inputs are tensors before performing an operation, avoiding type errors and ensuring smooth execution.
While torch.is_tensor() is simple, developers occasionally run into small pitfalls.
Here are common mistakes to avoid:
❌ Passing NumPy Arrays Directly
👉 NumPy arrays are not PyTorch tensors. Use torch.from_numpy(np_arr) first.
❌ Assuming All Lists Are Tensors
Python lists are not tensors until explicitly converted.
❌ Confusing torch.is_tensor() with torch.is_floating_point()
torch.is_tensor() → checks if object is tensor
torch.is_floating_point() → checks if tensor has a floating dtype
They serve completely different purposes.
Both work similarly, but here’s a quick comparison:
| Criteria | torch.is_tensor() | isinstance(obj, torch.Tensor) |
|---|---|---|
| Simplicity | ✅ Short and clear | 🟡 Slightly longer |
| PyTorch consistency | ✅ Part of PyTorch API | ❌ Python built-in |
| Performance | ⚡ Same speed | ⚡ Same speed |
Conclusion:
If you’re writing PyTorch-specific code, it’s cleaner and more readable to use torch.is_tensor().
Here are the main advantages of using this function:
✅ Quick and lightweight tensor check
⚙️ Ensures type consistency in your models
🧱 Prevents runtime errors due to unexpected data types
💻 Compatible with both CPU and GPU tensors
🔁 Simplifies input validation logic
🧩 Integrates easily into custom modules or layers
When designing models, your input data may be batched tensors. Here’s an example of verifying tensors in a batch-processing context.
If the input is not a tensor, this function raises a clear error, preventing silent bugs.
Here are some expert recommendations:
✅ Always validate user inputs before performing tensor operations.
🧩 Combine with type conversion for robust functions (torch.tensor() or torch.as_tensor()).
💡 Use during debugging to confirm data types at runtime.
🧠 Prefer torch.is_tensor() over raw Python type checks when writing PyTorch code.
🚀 Use in utility libraries to make your deep learning pipelines error-free.
Here are other helpful PyTorch utilities related to tensor checking:
| Function | Description |
|---|---|
torch.is_storage(obj) |
Checks if obj is a storage type |
torch.is_floating_point(tensor) |
Checks if tensor’s dtype is float |
torch.is_complex(tensor) |
Checks if tensor’s dtype is complex |
torch.is_grad_enabled() |
Returns if autograd is enabled |
torch.isinf(tensor) |
Checks if elements are infinite |
It returns a Boolean value — True if the input object is a PyTorch tensor, otherwise False.
Both work similarly, but torch.is_tensor() is preferred for PyTorch code because it’s part of the PyTorch API and improves code readability.
Yes! It works for both CPU and GPU tensors. Whether your tensor is on cuda or cpu, torch.is_tensor() will still return True.
The torch.is_tensor() function is small but powerful.
It helps ensure your data types are consistent, prevents runtime errors, and keeps your PyTorch code clean and robust.
Whether you’re preprocessing input data, designing neural networks, or debugging tensor operations, torch.is_tensor() should always be part of your toolkit.
Use it smartly, and you’ll save hours of debugging time while writing safer, more reliable PyTorch code. 🚀