Yiling Liu
Yiling Liu

Reputation: 686

What is the time complexity for tf.transpose in tensorflow?

For example, assume we have an tensor a with input shape (10,32,32,3) Which means 10 32*32 RGB pictures with NHWC format

and I want to use tf.transpose(b, perm=[0, 3, 1, 2]) to convert it into NCHW format

What is the time complexity of this operation?

---------FORWARD---------

I found that the time complexity of numpy transpose function is O(1), according to the following link

https://www.thetopsites.net/article/58279082.shtml

Is that the same in tf?

Upvotes: 0

Views: 657

Answers (2)

Valdrinium
Valdrinium

Reputation: 1416

According to the source:

@compatibility(numpy)
  In `numpy` transposes are memory-efficient constant time operations as they
  simply return a new view of the same data with adjusted `strides`.
  TensorFlow does not support strides, so `transpose` returns a new tensor with
  the items permuted.
@end_compatibility

That means that the complexity depends on the dimensions of your tensor. Assuming that you have k dimensions of n-size each, it would mean O(n^k).

Upvotes: 0

karlphillip
karlphillip

Reputation: 93468

Matrix transposition for a 2D matrix is O(dim1*dim2).

For a 3D matrix it would be O(dim1*dim2*dim3) and so on.

Upvotes: 0

Related Questions