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?