I have a 1D tensor of numbers in LibTorch, in C++ and I would like to evaluate each number with a >
condition.
This is my attempt. Inside the if
statement the condition is whether the ith tensor is greater than 0.5
.
#include <torch/torch.h>
using namespace torch::indexing;
torch::Tensor frogs;
int main() {
frogs = torch::rand({11});
for (int i = 0; i<10; ++i) {
if (frogs.index({i}).item() > 0.5) {
std::cout << frogs.index({i}).item() << " \n";
}
}
return 0;
}
It returns the error...
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>’ (operand types are ‘c10::Scalar’ and ‘double’)
18 | if (frogs.index({i}).item() > 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<class _Iterator> constexpr bool c10::operator>(const c10::reverse_iterator<_Iterator>&, const c10::reverse_iterator<_Iterator>&)’
200 | inline constexpr bool operator>(
| ^~~~~~~~
/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<_Iterator>’
18 | if (frogs.index({i}).item() > 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,
You can use operator[]
to access tensor elements, no need to use data_ptr
(avoid it actually) :
std::cout << frogs[i];
You can directly compare a tensor to a scalar, it will return a boolean tensor with all coefficients being the result of the comparison with the scalar. Try this to see what I mean :
auto t = torch::randn({3,4});
std::cout << t << (t > 0);
You can use argwhere
to get a tensor with all the indices of non-zero elements. For example :
auto t = torch::randn({3,4});
// this will output the list of positive entries in the tensor t
std::cout << torch::argwhere(t > 0);