There is a moveaxis API for NDArray as mx.nd.moveaxis
but cannot find the same API for mx.sym
. Does that mean we should not use F.moveaxis under forward definition of Gluon block, because it couldn’t be hybridized from nd.moveaxis
to sym.moveaxis
Is there any alternative way to moveaxis using symbol.
Hi,
yes that’s correct. If there’s no sym.moveaxis
it woudn’t work in the hybrid_forward
of a gluon HybridBlock
.
However, it seems to me that you can achieve anything you want to achieve with moveaxis
using swapaxes
instead, which is hybridizable as there exists both mx.nd.swapaxes
and mx.sym.swapaxes
.
For example
>>> x = mx.nd.arange(16)
>>> x
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.]
<NDArray 16 @cpu(0)>
>>> x = x.reshape(shape=(2, 2, 2, 2))
>>> x
[[[[ 0. 1.]
[ 2. 3.]]
[[ 4. 5.]
[ 6. 7.]]]
[[[ 8. 9.]
[10. 11.]]
[[12. 13.]
[14. 15.]]]]
<NDArray 2x2x2x2 @cpu(0)>
>>> mx.nd.moveaxis(x, 0, 1)
[[[[ 0. 1.]
[ 2. 3.]]
[[ 8. 9.]
[10. 11.]]]
[[[ 4. 5.]
[ 6. 7.]]
[[12. 13.]
[14. 15.]]]]
<NDArray 2x2x2x2 @cpu(0)>
>>> mx.nd.swapaxes(x, 0, 1)
[[[[ 0. 1.]
[ 2. 3.]]
[[ 8. 9.]
[10. 11.]]]
[[[ 4. 5.]
[ 6. 7.]]
[[12. 13.]
[14. 15.]]]]
<NDArray 2x2x2x2 @cpu(0)>
Hope that helps. There may be a counter-example to the above but I’m pretty sure you could still achieve it with perhaps multiple swapaxes
calls.
1 Like
Thanks for the solution.