Insert dimensions of size 1. See tf.expand_dims. Args: labeled_tensor: The input tensor. axes: The desired axis names as strings or tuples of (name, label), where `label` is the coordinate name for the new dimension `name`. These must include the existing axis names, and th
(labeled_tensor, axes, name=None)
| 723 | tc.Tuple(string_types, collections_abc.Hashable))), |
| 724 | tc.Optional(string_types)) |
| 725 | def expand_dims(labeled_tensor, axes, name=None): |
| 726 | """Insert dimensions of size 1. |
| 727 | |
| 728 | See tf.expand_dims. |
| 729 | |
| 730 | Args: |
| 731 | labeled_tensor: The input tensor. |
| 732 | axes: The desired axis names as strings or tuples of (name, label), where |
| 733 | `label` is the coordinate name for the new dimension `name`. These must |
| 734 | include the existing axis names, and the existing names must appear in the |
| 735 | same order in this list as they do in the input tensor. |
| 736 | name: Optional op name. |
| 737 | |
| 738 | Returns: |
| 739 | A tensor with an axis for each axis in axes. |
| 740 | New axes are created with size 1 and do not have labeled coordinates. |
| 741 | |
| 742 | Raises: |
| 743 | AxisOrderError: If axis names don't appear in the same order in axes |
| 744 | and the labeled tensor. |
| 745 | """ |
| 746 | with ops.name_scope(name, 'lt_expand_dims', [labeled_tensor]) as scope: |
| 747 | labeled_tensor = convert_to_labeled_tensor(labeled_tensor) |
| 748 | |
| 749 | axis_names = [a if isinstance(a, string_types) else a[0] for a in axes] |
| 750 | check_axis_order(labeled_tensor, axis_names) |
| 751 | |
| 752 | reshaped_axes = [] |
| 753 | shape = [] |
| 754 | for axis_spec in axes: |
| 755 | if axis_spec in labeled_tensor.axes: |
| 756 | axis = labeled_tensor.axes[axis_spec] |
| 757 | reshaped_axes.append(axis) |
| 758 | shape.append(-1 if axis.size is None else axis.size) |
| 759 | else: |
| 760 | if isinstance(axis_spec, string_types): |
| 761 | reshaped_axes.append((axis_spec, 1)) |
| 762 | else: |
| 763 | (name, label) = axis_spec |
| 764 | reshaped_axes.append((name, (label,))) |
| 765 | |
| 766 | shape.append(1) |
| 767 | |
| 768 | reshaped_tensor = array_ops.reshape( |
| 769 | labeled_tensor.tensor, shape, name=scope) |
| 770 | |
| 771 | return LabeledTensor(reshaped_tensor, reshaped_axes) |
| 772 | |
| 773 | |
| 774 | # This should only be added to a graph collection once. |
no test coverage detected