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} ]