Specify PyTorch Tensor Minimum Value Threshold by using the PyTorch clamp operation
Specify PyTorch Tensor Minimum Value Threshold by using the PyTorch clamp operation
This video will show you how to specify a PyTorch tensor’s minimum value threshold by using the torch.clamp operation.
First, we import PyTorch.
import torch
Then we print the PyTorch version we are using.
print(torch.__version__)
We are using PyTorch 0.3.1.post2.
Let’s now create a PyTorch tensor full of random floating point numbers.
pt_tensor_no_min_val_threshold_ex = torch.rand(2, 4, 6)
We use torch.rand, we want the shape to be 2x4x6, and we assign it to the Python variable pt_tensor_no_min_val_threshold_ex.
Let’s print this Python variable to see what we get.
print(pt_tensor_no_min_val_threshold_ex)
We get a torch.FloatTensor of size 2x4x6, we see that it is in fact 2x4x6, and we see that all the numbers are between zero and one.
Next, let’s create a PyTorch tensor based on our pt_tensor_no_min_val_threshold_ex Python variable, and we want to set the minimum value as 0.5.
pt_tensor_zero_point_five_min_val_threshold_ex = torch.clamp(pt_tensor_no_min_val_threshold_ex, min=0.5)
So if anything is below 0.5 in this tensor, we want it to be clamped and we want it to be replaced by 0.5.
So we use the torch.clamp operation, and we assign that to the Python variable pt_tensor_zero_point_five_min_val_threshold_ex.
Let’s print our variable to see what we get.
print(pt_tensor_zero_point_five_min_val_threshold_ex)
We see that it’s a PyTorch FloatTensor of size 2x4x6.
Let’s just look at the second array versus this one.
So the first element, 0.523, that’s above 0.5, so we see the value is the same.
This element is 0.19.
That is below our minimum threshold of 0.5, so it has been updated to 0.5.
This value, 0.757 is above our threshold of 0.5, so it comes through as it was before.
So you can see that any value above 0.5 in the original tensor remains the same.
However, any value below 0.5 has now been clamped and updated to be 0.5.
Perfect - We were able to specify a PyTorch tensor’s minimum value threshold by using the torch.clamp operation.
Receive the Data Science Weekly Newsletter every Thursday
Easy to unsubscribe at any time. Your e-mail address is safe.