It’s defined as taking elements from an array in the docs, I want to know how to assign value to the specified element of origin array after calling relay.take()?If it’s a copy from origin array, how to assign values to origin array, or it’s a reference then how to do so?Sorry, I don’t know if I’m describing it clearly
IIRC it’s basically the same thing as “Gather” in ONNX
To assign values you should use scatter or scatter_nd in relay.
Thanks for your reply! But I just want to change few elements of a 2D array , scatter() seems difficult to do that. I found one_hot() seemed to work, but is there an easier way?
If you tell me exactly what you are trying to do I can probably help. Do you have an example?
Let’s say that
arr is assigned:
arr = relay.zeros((m, n), dtype="int32")
then I wanna do as in python:
arr[i][j] = 1 arr[i][j] += 1
how to do these in relay?
index_put_() in torch can do so.
Btw, how to initialize an array by custom? THANKS A LOT
For arr[i][j] = 1 just do
relay.ones instead of
For arr[i][j] += 1 use
output[indices[i][j]][j] += updates[i][j] if axis = 0, output[i][indices[i][j]] += updates[i][j] if axis = 1,
If scatter_add is insufficient, then you will need to use scatter_nd with ‘add’ mode.
The scatter_nd is pretty complicated but a few examples and you should know what is going on. It is pretty standard operator in Deep Learning. Here is an example: https://github.com/onnx/onnx/blob/master/docs/Operators.md#scatternd
Hi，@AndrewZhaoLuo ，I also have a problem about relay.take
I have to both do argmax and max to the same tensor as following
tensor1 = relay.op.argmax(prob, axis=2, keepdims=True) tensor2 = relay.op.max(prob, axis=2, keepdims=True)
And they cost too much time since the prob tensor is very large.
Can I use relay.take() and tensor1 to get tensor2 from prob? Or Can I get both tensor1/tensor2 from a single function?
argmax and then using take sounds like the best solution here yes.
You could probably change argmax to give you both the indices and the results, though that is more complicated.
I tried using take but it doesn’t work.
I think what I need here is take_along_axis, right?
I think numpy.take — NumPy v1.23 Manual is what you want.
Example data = [10, 11, 12, 13, 14] indices = [0, -1, 2, 3]
np.take(data, indices) → [10, 14, 12, 13]