How to use a > operator on a number inside a tensor?

username: Alio
cooked: <p>I have a 1D tensor of numbers in LibTorch, in C++ and I would like to evaluate each number with a <code>&gt;</code> condition.</p>
<p>This is my attempt. Inside the <code>if</code> statement the condition is whether the ith tensor is greater than <code>0.5</code>.</p>
<pre><code>#include &lt;torch/torch.h&gt;
using namespace torch::indexing;

torch::Tensor frogs;   

int main() {

    frogs = torch::rand({11});

    for (int i = 0; i&lt;10; ++i) {

        if (frogs.index({i}).item() &gt; 0.5) {

            std::cout &lt;&lt; frogs.index({i}).item() &lt;&lt; " \n";
        }
    }   

    return 0;
}
</code></pre>
<p>It returns the error…</p>
<pre><code>Consolidate compiler generated dependencies of target mujoco_gym
[ 50%] Building CXX object CMakeFiles/mujoco_gym.dir/tester.cpp.o
/home/iii/tor/m_gym/tester.cpp: In function ‘int main()’:
/home/iii/tor/m_gym/tester.cpp:18:37: error: no match for ‘operator&gt;’ (operand types are ‘c10::Scalar’ and ‘double’)
   18 |         if (frogs.index({i}).item() &gt; 0.5) {
      |             ~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
      |                                  |    |
      |                                  |    double
      |                                  c10::Scalar
In file included from /home/iii/tor/m_gym/libtorch/include/c10/util/string_view.h:5,
                 from /home/iii/tor/m_gym/libtorch/include/c10/util/StringUtil.h:6,
                 from /home/iii/tor/m_gym/libtorch/include/c10/util/Exception.h:6,
                 from /home/iii/tor/m_gym/libtorch/include/c10/core/Device.h:5,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/core/TensorBody.h:11,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/core/Tensor.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/Tensor.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/function_hook.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/cpp_hook.h:2,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/variable.h:6,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/autograd.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/api/include/torch/autograd.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/api/include/torch/all.h:7,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
                 from /home/iii/tor/m_gym/tester.cpp:1:
/home/iii/tor/m_gym/libtorch/include/c10/util/reverse_iterator.h:200:23: note: candidate: ‘template&lt;class _Iterator&gt; constexpr bool c10::operator&gt;(const c10::reverse_iterator&lt;_Iterator&gt;&amp;, const c10::reverse_iterator&lt;_Iterator&gt;&amp;)’
  200 | inline constexpr bool operator&gt;(
      |                       ^~~~~~~~
/home/iii/tor/m_gym/libtorch/include/c10/util/reverse_iterator.h:200:23: note:   template argument deduction/substitution failed:
/home/iii/tor/m_gym/tester.cpp:18:39: note:   ‘c10::Scalar’ is not derived from ‘const c10::reverse_iterator&lt;_Iterator&gt;’
   18 |         if (frogs.index({i}).item() &gt; 0.5) {
      |                                       ^~~
In file included from /home/iii/tor/m_gym/libtorch/include/c10/util/string_view.h:5,
                 from /home/iii/tor/m_gym/libtorch/include/c10/util/StringUtil.h:6,
                 from /home/iii/tor/m_gym/libtorch/include/c10/util/Exception.h:6,
                 from /home/iii/tor/m_gym/libtorch/include/c10/core/Device.h:5,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/core/TensorBody.h:11,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/core/Tensor.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/ATen/Tensor.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/function_hook.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/cpp_hook.h:2,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/variable.h:6,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/autograd.h:3,
                 from /home/iii/tor/m_gym/libtorch/include/torch/csrc/api/include/torch/autograd.h:3,
</code></pre>

username: Alio
cooked: <pre><code class="lang-auto">
int main() {

    frogs = torch::rand({11});

    float* temp_arr = frogs.data_ptr&lt;float&gt;();


    for (int i = 0; i&lt;10; ++i) {

        if (temp_arr[i] &gt; 0.5) {

            std::cout &lt;&lt; temp_arr[i] &lt;&lt; " \n";
        }
     }   

    return 0;
}
</code></pre>