How do I swap tensor's axes in TensorFlow?

后端 未结 2 1983
广开言路
广开言路 2020-12-05 06:59

I have a tensor of shape (30, 116, 10), and I want to swap the first two dimensions, so that I have a tensor of shape (116, 30, 10)

I saw t

相关标签:
2条回答
  • 2020-12-05 07:33

    It is possible to use tf.einsum to swap axes if the number of input dimensions is unknown. For example:

    • tf.einsum("ij...->ji...", input) will swap the first two dimensions of input;
    • tf.einsum("...ij->...ji", input) will swap the last two dimensions;
    • tf.einsum("aij...->aji...", input) will swap the second and the third dimension;
    • tf.einsum("ijk...->kij...", input) will permute the first three dimensions;

    and so on.

    0 讨论(0)
  • 2020-12-05 07:40

    tf.transpose provides the same functionality as np.swapaxes, although in a more generalized form. In your case, you can do tf.transpose(orig_tensor, [1, 0, 2]) which would be equivalent to np.swapaxes(orig_np_array, 0, 1).

    0 讨论(0)
提交回复
热议问题