One-Dimensional Tensor - ( Tensors in PyTorch )
Heading h2
Syntax
import torch
# Creating a one-dimensional tensor
x = torch.tensor([1, 2, 3, 4, 5])
Example
import torch
# Creating a one-dimensional tensor
x = torch.tensor([1, 2, 3, 4, 5])
# Printing the tensor
print(x)
Output
tensor([1, 2, 3, 4, 5])
Explanation
In PyTorch, tensors are similar to NumPy arrays but are designed to utilize GPUs for faster computation. A one-dimensional tensor is a tensor with only one dimension, similar to a one-dimensional array in NumPy. They can be created using the torch.tensor()
function and passing a list of values.
Use
One-dimensional tensors can be used to represent vectors and are commonly used for tasks such as linear regression and neural networks.
Important Points
- A one-dimensional tensor has only one dimension.
- They can be created using the
torch.tensor()
function and passing a list of values. - One-dimensional tensors are commonly used for tasks such as linear regression and neural networks.
Summary
One-dimensional tensors are a fundamental data structure in PyTorch and are used to represent vectors. They can be created using the torch.tensor()
function and passed a list of values. One-dimensional tensors are commonly used for tasks such as linear regression and neural networks.