A tuple in Python is a data structure that stores the data in a sequence and is immutable. A PyTorch tensor is like a NumPy array but the computations on tensors can utilize the GPUs whereas the numpy array can't.
To convert a tuple to a PyTorch Tensor, we use torch.tensor(tuple)
. It takes a tuple as input and returns a PyTorch tensor.
Python 3 example 1.
import torchtpl = (1,2,3,4,5) # a tuple with 5 valuesprint("Tuple:", tpl)tens = torch.tensor(tpl) # tuple converted to pytorch tensorprint("Tensor:", tens)
Output:
Tuple: (1, 2, 3, 4, 5) Tensor: tensor([1, 2, 3, 4, 5])
Comments
Post a Comment