Repeats elements of `data`. Args: data: An `N`-dimensional tensor. repeats: A 1-D integer tensor specifying how many times each element in `axis` should be repeated. `len(repeats)` must equal `data.shape[axis]`. Supports broadcasting from a scalar value. axis: `int`. The
(data, repeats, axis, name=None)
| 4864 | # External (OSS) `tf.repeat` feature request: |
| 4865 | # https://github.com/tensorflow/tensorflow/issues/8246 |
| 4866 | def repeat_with_axis(data, repeats, axis, name=None): |
| 4867 | """Repeats elements of `data`. |
| 4868 | |
| 4869 | Args: |
| 4870 | data: An `N`-dimensional tensor. |
| 4871 | repeats: A 1-D integer tensor specifying how many times each element in |
| 4872 | `axis` should be repeated. `len(repeats)` must equal `data.shape[axis]`. |
| 4873 | Supports broadcasting from a scalar value. |
| 4874 | axis: `int`. The axis along which to repeat values. Must be less than |
| 4875 | `max(N, 1)`. |
| 4876 | name: A name for the operation. |
| 4877 | |
| 4878 | Returns: |
| 4879 | A tensor with `max(N, 1)` dimensions. Has the same shape as `data`, |
| 4880 | except that dimension `axis` has size `sum(repeats)`. |
| 4881 | #### Examples: |
| 4882 | ```python |
| 4883 | >>> repeat(['a', 'b', 'c'], repeats=[3, 0, 2], axis=0) |
| 4884 | ['a', 'a', 'a', 'c', 'c'] |
| 4885 | >>> repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=0) |
| 4886 | [[1, 2], [1, 2], [3, 4], [3, 4], [3, 4]] |
| 4887 | >>> repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=1) |
| 4888 | [[1, 1, 2, 2, 2], [3, 3, 4, 4, 4]] |
| 4889 | ``` |
| 4890 | """ |
| 4891 | if not isinstance(axis, int): |
| 4892 | raise TypeError("axis must be an int; got %s" % type(axis).__name__) |
| 4893 | |
| 4894 | with ops.name_scope(name, "Repeat", [data, repeats]): |
| 4895 | data = ops.convert_to_tensor(data, name="data") |
| 4896 | repeats = convert_to_int_tensor(repeats, name="repeats") |
| 4897 | repeats.shape.with_rank_at_most(1) |
| 4898 | |
| 4899 | # If `data` is a scalar, then upgrade it to a vector. |
| 4900 | data = _with_nonzero_rank(data) |
| 4901 | data_shape = shape(data) |
| 4902 | |
| 4903 | # If `axis` is negative, then convert it to a positive value. |
| 4904 | axis = get_positive_axis(axis, data.shape.ndims) |
| 4905 | |
| 4906 | # Check data Tensor shapes. |
| 4907 | if repeats.shape.ndims == 1: |
| 4908 | data.shape.dims[axis].assert_is_compatible_with(repeats.shape[0]) |
| 4909 | |
| 4910 | # If we know that `repeats` is a scalar, then we can just tile & reshape. |
| 4911 | if repeats.shape.ndims == 0: |
| 4912 | expanded = expand_dims(data, axis + 1) |
| 4913 | tiled = tile_one_dimension(expanded, axis + 1, repeats) |
| 4914 | result_shape = concat([data_shape[:axis], [-1], data_shape[axis + 1:]], |
| 4915 | axis=0) |
| 4916 | return reshape(tiled, result_shape) |
| 4917 | |
| 4918 | # Broadcast the `repeats` tensor so rank(repeats) == axis + 1. |
| 4919 | if repeats.shape.ndims != axis + 1: |
| 4920 | repeats_shape = shape(repeats) |
| 4921 | repeats_ndims = rank(repeats) |
| 4922 | broadcast_shape = concat( |
| 4923 | [data_shape[:axis + 1 - repeats_ndims], repeats_shape], axis=0) |
no test coverage detected