1. Type convertion in Numpy
Here is my code:
import numpy as np a = np.asarray([1, 2]) b =  c = np.concatenate((a, b)) print(c.dtype)
Guess what? The type of variable ‘c’ is ‘float64’! Seems Numpy automatically considers a empty array of Python as ‘float64’ type. So the correct code should be:
import numpy as np a = np.asarray([1, 2]) b =  c = np.concatenate((a, np.asarray(b, dtype=a.dtype))
This time, the type of ‘c’ is ‘int64’
2. Convert a tensor of PyTorch to ‘uint8’
If we want to convert a tensor of PyTorch to ‘float’, we can use tensor.float(). If we want to convert it to ‘int32’, we can use tensor.int().
But if we want to convert the type to ‘uint8’, what should we do? There isn’t any function named ‘uint8()’ for a tensor.
Actually, it’s much quite simple than I expect:
import torch tensor = torch.tensor([1, 2, 3]) tensor.byte() # to uint8