Permute a tensor's axes. See tf.transpose. Args: labeled_tensor: The input tensor. axis_order: Optional desired axis order, as a list of names. By default, the order of axes is reversed. name: Optional op name. Returns: The permuted tensor. Raises: ValueError: I
(labeled_tensor, axis_order=None, name=None)
| 676 | @tc.accepts(LabeledTensorLike, tc.Optional(tc.Collection(string_types)), |
| 677 | tc.Optional(string_types)) |
| 678 | def transpose(labeled_tensor, axis_order=None, name=None): |
| 679 | """Permute a tensor's axes. |
| 680 | |
| 681 | See tf.transpose. |
| 682 | |
| 683 | Args: |
| 684 | labeled_tensor: The input tensor. |
| 685 | axis_order: Optional desired axis order, as a list of names. By default, the |
| 686 | order of axes is reversed. |
| 687 | name: Optional op name. |
| 688 | |
| 689 | Returns: |
| 690 | The permuted tensor. |
| 691 | |
| 692 | Raises: |
| 693 | ValueError: If axis_order isn't a permutation of the existing axes. |
| 694 | """ |
| 695 | with ops.name_scope(name, 'lt_transpose', [labeled_tensor]) as scope: |
| 696 | labeled_tensor = convert_to_labeled_tensor(labeled_tensor) |
| 697 | |
| 698 | original_order = list(labeled_tensor.axes.keys()) |
| 699 | if axis_order is None: |
| 700 | axis_order = list(reversed(original_order)) |
| 701 | elif sorted(axis_order) != sorted(original_order): |
| 702 | raise ValueError( |
| 703 | 'The new axis order must have the same names as the original axes, ' |
| 704 | 'but the new order is %r while the original order is %r' % |
| 705 | (axis_order, original_order)) |
| 706 | |
| 707 | axis_names = list(labeled_tensor.axes.keys()) |
| 708 | permutation = [axis_names.index(n) for n in axis_order] |
| 709 | |
| 710 | # Note: TensorFlow doesn't copy data for the identity transpose. |
| 711 | transpose_tensor = array_ops.transpose( |
| 712 | labeled_tensor.tensor, permutation, name=scope) |
| 713 | |
| 714 | permuted_axes = [labeled_tensor.axes[n] for n in axis_order] |
| 715 | |
| 716 | return LabeledTensor(transpose_tensor, permuted_axes) |
| 717 | |
| 718 | |
| 719 | @tc.returns(LabeledTensor) |