Libtorch Tensor indexing

Joris_Mollinga: <p>Hi all,</p>
<p>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:</p>
<pre><code class="lang-auto">float threshold=0.0;
torch::Tensor features = torch::randn({2, 3});
std::cout &lt;&lt; "Tensor before clipping: " &lt;&lt; std::endl &lt;&lt; features &lt;&lt; std::endl;
auto indices = features&lt;=threshold;
std::cout &lt;&lt; "indices: " &lt;&lt; std::endl &lt;&lt; indices &lt;&lt; std::endl;
features.index({indices}) = 0;
std::cout &lt;&lt; "Tensor after clipping: " &lt;&lt; std::endl &lt;&lt; features &lt;&lt; std::endl;
</code></pre>
<p>However, this does not work. It generates the following output:</p>
<pre><code class="lang-auto">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} ]
</code></pre>
<p>While the result <strong>should</strong> be:</p>
<pre><code class="lang-auto">0.0000  0.0000  1.1528
0.4064  0.4402  1.5282
[ CPUFloatType{2,3} ]
</code></pre>
<p>What am I doing wrong here?</p>

LeviViana: <p>Use clamp_min !</p>
<pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

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

Joris_Mollinga: <p>That would work if the <code>threshold</code> value equals 0. But what if I want to set <code>threshold</code> bigger than 0? Say 0.42? So everything smaller or equal than 0.42 should be set to 0.</p>
<pre><code class="lang-auto">float threshold=0.42;
torch::Tensor features = torch::randn({2, 3});
std::cout &lt;&lt; "Tensor before clipping: " &lt;&lt; std::endl &lt;&lt; features &lt;&lt; std::endl;
auto indices = features&lt;=threshold;
std::cout &lt;&lt; "indices: " &lt;&lt; std::endl &lt;&lt; indices &lt;&lt; std::endl;
features.index({indices}) = 0;
std::cout &lt;&lt; "Tensor after clipping: " &lt;&lt; std::endl &lt;&lt; features &lt;&lt; std::endl;
</code></pre>
<p>Example:</p>
<pre><code class="lang-auto">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} ]
</code></pre>
<p><strong>Should return</strong></p>
<pre><code class="lang-auto">0.0000  0.0000  1.1528
0.0000  0.4402  1.5282
[ CPUFloatType{2,3} ]
</code></pre>

Joris_Mollinga: <p>I solved it like this:</p>
<pre><code class="lang-auto">float threshold=0.42;
float fill_value=0.0;
torch::Tensor features = torch::randn({2, 3});
std::cout &lt;&lt; "Tensor before clipping: " &lt;&lt; std::endl &lt;&lt; features &lt;&lt; std::endl;
auto indices = features&lt;=threshold;
std::cout &lt;&lt; "indices: " &lt;&lt; std::endl &lt;&lt; indices &lt;&lt; std::endl;
features.masked_fill_(indices, fill_value);
std::cout &lt;&lt; "Tensor after clipping: " &lt;&lt; std::endl &lt;&lt; features &lt;&lt; std::endl;
</code></pre>