I am trying to do something like [0 if i < 1 else 1] for PyTorch tensor in C++. I tried to use tensor.accessor(), but it seem like it require you to know the dimension beforehand. While I want to pass it dynamically.
Is there anyway I could do this in C++ for Pytorch?
See if this helps:
Using TensorIterator:
https://labs.quansight.org/blog/2020/04/pytorch-tensoriterator-internals/
Or leveraging t.is_contiguous() / t.contiguous() to simplify transversal:
https://discuss.pytorch.org/t/iterating-over-tensor-in-c/60333/2
For loop in c++ torch is like this. Also, you can try torch.where. Be advised that for loop might be slow compared to built-in operations.
auto answer = torch::zeros_like(x);
auto batchCount = x.sizes()[0];
auto pointCount = x.sizes()[1];
// auto nextPoint = torch::zeros({batchCount, _pointDim});
for (int i = 0; i < batchCount; ++i)
{
for (int j = 1; j < _pointCount; ++j)
{
answer[i][j] = answer[i][j - 1] + x[i][j - 1];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.