Libtorch Tensor indexing

Hi all,

I am trying to set specific values in a tensor to 0 if they are smaller than a certain threshold. My code looks like this:

float threshold=0.0;
torch::Tensor features = torch::randn({2, 3});
std::cout << "Tensor before clipping: " << std::endl << features << std::endl;
auto indices = features<=threshold;
std::cout << "indices: " << std::endl << indices << std::endl;
features.index({indices}) = 0;
std::cout << "Tensor after clipping: " << std::endl << features << std::endl;

However, this does not work. It generates the following output:

Tensor before clipping:
-1.7993 -1.2228  1.1528
 0.4064  0.4402  1.5282
[ CPUFloatType{2,3} ]
indices:
 1  1  0
 0  0  0
[ CPUBoolType{2,3} ]
Tensor after clipping:
-1.7993 -1.2228  1.1528
 0.4064  0.4402  1.5282
[ CPUFloatType{2,3} ]

While the result should be:

0.0000  0.0000  1.1528
0.4064  0.4402  1.5282
[ CPUFloatType{2,3} ]

What am I doing wrong here?

1 Like

Use clamp_min !

#include <torch/torch.h>
#include <iostream>

int main(){
	torch::Tensor features = torch::randn({2, 3});
	std::cout << "Tensor before clipping: " << std::endl << features << std::endl;
	features.clamp_min_(0);
	std::cout << "Tensor after clipping: " << std::endl << features << std::endl;
	return 0;
}

That would work if the threshold value equals 0. But what if I want to set threshold bigger than 0? Say 0.42? So everything smaller or equal than 0.42 should be set to 0.

float threshold=0.42;
torch::Tensor features = torch::randn({2, 3});
std::cout << "Tensor before clipping: " << std::endl << features << std::endl;
auto indices = features<=threshold;
std::cout << "indices: " << std::endl << indices << std::endl;
features.index({indices}) = 0;
std::cout << "Tensor after clipping: " << std::endl << features << std::endl;

Example:

Tensor before clipping:
-1.7993 -1.2228  1.1528
 0.4064  0.4402  1.5282
[ CPUFloatType{2,3} ]
indices:
 1  1  0
 1  0  0
[ CPUBoolType{2,3} ]

Should return

0.0000  0.0000  1.1528
0.0000  0.4402  1.5282
[ CPUFloatType{2,3} ]

I solved it like this:

float threshold=0.42;
float fill_value=0.0;
torch::Tensor features = torch::randn({2, 3});
std::cout << "Tensor before clipping: " << std::endl << features << std::endl;
auto indices = features<=threshold;
std::cout << "indices: " << std::endl << indices << std::endl;
features.masked_fill_(indices, fill_value);
std::cout << "Tensor after clipping: " << std::endl << features << std::endl;