In PyTorch, we can cast a tensor to another type using the Tensor.type() method. This method accepts dtype as a parameter and return a copy of the original tensor. The dtype of the return tensor is new dtype passed as the parameter. There are 10 tensor types in PyTorch. Have a look on these datatypes for better understanding this post.
Examples - Casting a tensor to another types |
To cast a PyTorch tensor to another type, we have the following syntax:
Syntax
tensor.type(new_dtype)
Here the tensor is cast to new_dtype.
Parameter
- new_type: This is the new type of the tensor after casting.
Return: It cast the tensor to new_dtype. It creates a copy of the tensor with new_dtype and returns it.
Note:
- If new_dtype is not passed it return the dtype of the tensor
- If new_dtype is same as the dtype of the tensor, no copy is made
Examples (CPU Tensors)
Lets have some examples to convert a 32-bit int tensor to different types:
tens.type('torch.FloatTensor') # to 32-bit float
tens.type('torch.DoubleTensor') # to 64-bit float (double)
tens.type('torch.LongTensor') # to 64-bit int (long)
tens.type('torch.CharTensor') # to 8-bit integer
tens.type('torch.ShortTensor') # to 16-bit int
tens.type('torch.BoolTensor') # to Boolean
We can also pass the parameter to type() method without quotation marks.
tens.type(torch.FloatTensor)
tens.type(torch.DoubleTensor)
tens.type(torch.LongTensor)
tens.type(torch.CharTensor)
tens.type(torch.ShortTensor)
Examples (GPU Tensors)
tens.type(torch.cuda.FloatTensor)
tens.type(torch.cuda.DoubleTensor)
tens.type(torch.cuda.LongTensor)
tens.type(torch.cuda.CharTensor)
tens.type(torch.cuda.ShortTensor)
Casting a 32-bit Integer Tensor to a 32-bit Floating Point Tensor
Syntax
tens.type('torch.FloatTensor')
Here tens is a 32-bit int tensor. It is to be cast to a 32-bit float tensor.
32-bit integer
dtype = torch.int32, CPU tensor = torch.IntTensor, GPU tensor = torch.cuda.IntTensor.
32-bit floating point
dtype = torch.float32, CPU tensor = torch.FloatTensor, GPU tensor = torch.cuda.FloatTensor.
Example 1
# import required libraries/ modules
import torch
import numpy as np
# create an ndarray
x = np.array([1,2,3,4])
# Convert the array to a tensor (32-bit int tensor)
tens = torch.tensor(x)
print(tens)
# cast it to 32-bit floating point tensor
tens = tens.type('torch.FloatTensor')
print(tens)
Output
tensor([1, 2, 3, 4], dtype=torch.int32)Casting a 32-bit Integer Tensor to a 64-bit Floating Point Tensor
Syntax
tens.type('torch.DoubleTensor')
Here tens is a 32-bit int tensor. It is to be cast to a 64-bit float (double) tensor.
64-bit floating point
dtype = torch.float42, CPU tensor = torch.FloatTensor, GPU tensor = torch.cuda.FloatTensor.
Example 2
# import required libraries/ modules
import torch
# Create a tensor (32-bit int tensor)
tens = torch.tensor([1,2,3,4], dtype=torch.int32)
print(tens)
# cast it to 64-bit floating point (double) tensor
tens = tens.type('torch.DoubleTensor')
print(tens)
Output
tensor([1, 2, 3, 4], dtype=torch.int32)Casting a 32-bit Integer Tensor to a Boolean Tensor
Syntax
tens.type('torch.BooleanTensor')
Here tens is a 32-bit int tensor. It is to be cast to a Boolean tensor.
Boolean
dtype = torch.bool, CPU tensor = torch.BooleanTensor, GPU tensor = torch.cuda.BooleanTensor.
Example 3
# import required libraries/ modules
import torch
# Create a tensor (32-bit int tensor)
tens = torch.tensor([1,0,3,0, -1], dtype=torch.int32)
print(tens)
# cast it to Boolean tensor
tens = tens.type('torch.BoolTensor')
print(tens)
Output
Example 4 :Casting a 32-bit Integer Tensor to a 8-bit integer (CharTensor)
# import required libraries/ modules
import torch
# Create a tensor (32-bit int tensor)
tens = torch.tensor([1,0,3,0, -1], dtype=torch.int32)
print(tens)
# cast it to 8-bit int tensor (CharTensor)
tens = tens.type('torch.CharTensor')
print(tens)
Output
tensor([ 1, 0, 3, 0, -1], dtype=torch.int32)Example 5 : Casting 46-bit Floating Point (Double) Tensor to 16-bit int (Short) Tensor
# import required libraries/ modules
import torch
# Create a 64-bit floating point tensor
tens = torch.tensor([1, 0, 3, 0, -1], dtype=torch.float64)
print(tens)
# cast it to 16-bit int (Short) tensor
tens = tens.type('torch.ShortTensor')
print(tens)
Output
tensor([ 1., 0., 3., 0., -1.], dtype=torch.float64)tensor([ 1, 0, 3, 0, -1], dtype=torch.int16)
Example 6: Casting a 32-bit int tensor to a 32-bit floating point tensor
# import required libraries/ modules
import torch
# Create a 64-bit floating point tensor
tens = torch.tensor([1, 0, 3, 0, -1], dtype=torch.int32)
print(tens)
# cast it to 32-bit floating point tensor
tens = tens.type('torch.FloatTensor')
print(tens)
Output
tensor([ 1, 0, 3, 0, -1], dtype=torch.int32)Useful Resources:
Next Post: PyTorch - How to concatenate tensors along a given dimension?
Previous Post: How Do Neural Networks Learn?
Next Post: PyTorch - How to concatenate tensors along a given dimension?
Previous Post: How Do Neural Networks Learn?
Comments
Post a Comment