Align the axes of two tensors so they may be broadcast to each other. Axes are ordered by the current axis order scope, if present, or by the left- most consistent ordering. An exception is raised if it is impossible to align the tensors without a transpose (align never copies the input data)
(labeled_tensor_0, labeled_tensor_1, name=None)
| 966 | @tc.returns(LabeledTensor, LabeledTensor, Axes) |
| 967 | @tc.accepts(LabeledTensorLike, LabeledTensorLike, tc.Optional(string_types)) |
| 968 | def align(labeled_tensor_0, labeled_tensor_1, name=None): |
| 969 | """Align the axes of two tensors so they may be broadcast to each other. |
| 970 | |
| 971 | Axes are ordered by the current axis order scope, if present, or by the left- |
| 972 | most consistent ordering. An exception is raised if it is impossible to align |
| 973 | the tensors without a transpose (align never copies the input data). |
| 974 | |
| 975 | Example usage: |
| 976 | |
| 977 | >>> a = lt.LabeledTensor(tf.ones((2, 4)), ['x', 'z']) |
| 978 | >>> b = lt.LabeledTensor(tf.ones((3, 4)), ['y', 'z']) |
| 979 | >>> a2, b2, axes = lt.align(a, b) |
| 980 | >>> a2 |
| 981 | <LabeledTensor 'lt_align_1/lt_align_1/0:...' shape=(2, 1, 4) dtype=float32 |
| 982 | axes=[('x', Dimension(2)), |
| 983 | ('y', Dimension(1)), |
| 984 | ('z', Dimension(4))]> |
| 985 | >>> b2 |
| 986 | <LabeledTensor 'lt_align_1/lt_align_1/1:...' shape=(1, 3, 4) dtype=float32 |
| 987 | axes=[('x', Dimension(1)), |
| 988 | ('y', Dimension(3)), |
| 989 | ('z', Dimension(4))]> |
| 990 | >>> axes |
| 991 | Axes([('x', Dimension(2)), |
| 992 | ('y', Dimension(3)), |
| 993 | ('z', Dimension(4))]) |
| 994 | |
| 995 | Args: |
| 996 | labeled_tensor_0: An input tensor. |
| 997 | labeled_tensor_1: An input tensor. |
| 998 | name: Optional op name. |
| 999 | |
| 1000 | Returns: |
| 1001 | The aligned tensors and the axes the resulting tensor would have if the two |
| 1002 | aligned tensors were broadcast to each other. The aligned tensors have the |
| 1003 | same rank but not necessarily the same shape, with axes in the same order. |
| 1004 | |
| 1005 | Raises: |
| 1006 | ValueError: If axes with the same name on the inputs are not equal. |
| 1007 | AxisOrderError: If there is no way to reshape the input tensors into the |
| 1008 | output without a transpose. |
| 1009 | """ |
| 1010 | with ops.name_scope(name, 'lt_align', |
| 1011 | [labeled_tensor_0, labeled_tensor_1]) as scope: |
| 1012 | |
| 1013 | labeled_tensor_0 = convert_to_labeled_tensor(labeled_tensor_0) |
| 1014 | labeled_tensor_1 = convert_to_labeled_tensor(labeled_tensor_1) |
| 1015 | |
| 1016 | axes_0 = labeled_tensor_0.axes |
| 1017 | axes_1 = labeled_tensor_1.axes |
| 1018 | for axis_name in axes_0: |
| 1019 | if axis_name in axes_1: |
| 1020 | if axes_0[axis_name] != axes_1[axis_name]: |
| 1021 | raise ValueError('Mismatched %r axis on input tensors: %r and %r' % |
| 1022 | (axis_name, axes_0[axis_name], axes_1[axis_name])) |
| 1023 | |
| 1024 | axis_scope_order = get_axis_order() |
| 1025 | if axis_scope_order is not None: |
no test coverage detected