5

I'm using numpy.

I have an ndarray with shape of [T, H, W, C] and I want to transpose it to become like: [T, C, H, W]. However, this array is huge and I want to be memory-efficient.

But I just found np.transpose to do this which is not in-place.

Why do operations like np.transpose don't have their in-place counterpart?

I used to think that any operation named np.Bar would have its in-place counterpart named np.Bar_, only to find that this is not the truth.

1 Answer 1

8

From np.transpose docs

A view is returned whenever possible.

meaning no extra memory is allocated for the output array.

>>> import numpy as np

>>> A = np.random.rand(2, 3, 4, 5)
>>> B = np.transpose(A, axes=(0, 3, 1, 2))

>>> A.shape
(2, 3, 4, 5)
>>> B.shape
(2, 5, 3, 4)

You can use np.shares_memory to check if B is a view of A:

>>> np.shares_memory(A, B)
True

So, you can safely transpose your data withnp.transpose.

Sign up to request clarification or add additional context in comments.

8 Comments

The idea was to demonstrate that the kind of transpose OP intends to perform does return a view rather than a copy.
so what happens if I do A = np.transpose(A,axes=xxx)? Does it need memory re-allocation?
@游凯超, If np.transpose(A,axes=xxx) is a view then A is just reassigned (both point to the same data structure).
is there any solution if we want the real transpose rather than a view?
@JingpengWu, a quick-and-dirty approach would be to copy the array and then transpose the copy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.