I have a multi-dimensional tensor that is torch::kCUDA and I want to change (or, respectively, see) just one element in my non-CUDA, C++ CPU code. I’d like to do the equivalent of myTensor[3][1][4] = 1.1f;
How would I do that?
Hi,
You will need to use a cuda kernel if you want to change one value like that based on a pointer.
Another way around this would be to use .index()
to get a Tensor containing only the one entry that you want and the use .fill_(1.1f)
(that has a kernel already implemented) to fill that Tensor with the value you want.
Would that work for you?
Perfect! For getting and setting respectively I am using
float myValue = myTensor.index( myIndex ).item< float >();
myTensor.index( myIndex ).fill_( myValue );
I realize that one-by-one access to elements is a crazy thing to do with tensors and it should only be used rarely, but now I know how to do it when I need to do it rarely.
1 Like