Understanding torch.Tensor vs torch.tensor in PyTorch

by | Machine Learning, Programming, PyTorch

In PyTorch, both torch.Tensor and torch.tensor can be used to create tensors. However, there are significant differences in their behavior and intended use cases. This guide explains these differences in detail.

Key Difference Between torch.Tensor and torch.tensor

The key difference lies in how torch.Tensor and torch.tensor initialize tensors:

  • torch.Tensor: The main tensor class in PyTorch. Calling torch.Tensor() creates an empty, uninitialized tensor.
  • torch.tensor: A factory function introduced in PyTorch 0.4.0. It constructs a tensor from data and allows specifying attributes like dtype and requires_grad.

Behavior of torch.Tensor

torch.Tensor can be used to create an empty tensor without providing any data:

Example: torch.Tensor
import torch

# Create an uninitialized tensor
tensor_without_data = torch.Tensor()
print(tensor_without_data)  # Output: tensor([])

This can lead to unexpected behavior, as the tensor is uninitialized and may contain random values if given a shape:

Uninitialized Tensor
tensor = torch.Tensor(2, 3)
print(tensor)  # Output: Random, uninitialized values

Note: Due to these quirks, using torch.Tensor is not recommended for most scenarios.

Behavior of torch.tensor

torch.tensor, on the other hand, constructs a tensor from given data:

Example: torch.tensor
import torch

# Create a tensor with data
tensor_with_data = torch.tensor([1, 2, 3], dtype=torch.float32, requires_grad=True)
print(tensor_with_data)
# Output: tensor([1., 2., 3.], requires_grad=True)

If no data is provided, calling torch.tensor() will result in an error:

Error Example
tensor_without_data = torch.tensor()
# TypeError: tensor() missing 1 required positional argument: 'data'

Key Features of torch.tensor:

  • Always initializes with data.
  • Allows specifying attributes such as dtype, device, and requires_grad.

Comparison Table

Feature torch.Tensor torch.tensor
Initialization Creates an empty, uninitialized tensor. Requires data for initialization.
Error Behavior No error if called without arguments. Raises TypeError if called without arguments.
Recommended Use Not recommended for new code. Preferred for creating tensors with known data.

Recommendation for Initialization of Tensors

💡 Tip: To avoid confusion and ensure code clarity:

  • Use torch.empty() for uninitialized tensors.
  • Use torch.zeros() or torch.ones() for explicit zero or one initialization.
  • Prefer torch.tensor() for initializing tensors with specific data.

By following these practices, you can write code that is robust and compatible across PyTorch versions.

Further Reading

If you found this post helpful and want to deepen your understanding of PyTorch and tensor operations, here are some additional resources and topics to explore:

These resources will help you gain a deeper understanding of tensor manipulation, PyTorch’s capabilities, and advanced topics in deep learning.

Summary

To ensure clarity and consistency in your code:

  • Use torch.tensor when creating tensors with known data.
  • Avoid torch.Tensor in new code due to its unpredictable behavior with uninitialized values.

By using torch.tensor, you benefit from explicit control over the tensor’s attributes and prevent potential bugs in your PyTorch applications.

Congratulations on reading to the end of this tutorial! For further reading on PyTorch, go to the Deep Learning Frameworks page.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨