def expand_dims_v2(input, axis, name=None): """Returns a tensor with an additional dimension inserted at index `axis`. Given a tensor `input`, this operation inserts a dimension of size 1 at the dimension index `axis` of `input`'s shape. The dimension index `axis` starts at zero; if you specify a negative number for `axis` it is counted backward from the end. This operation is useful if you want to add a batch dimension to a single element. For example, if you have a single image of shape `[height, width, channels]`, you can make it a batch of one image with `expand_dims(image, 0)`, which will make the shape `[1, height, width, channels]`. Examples: >>> t = [[1, 2, 3],[4, 5, 6]] # shape [2, 3] >>> tf.expand_dims(t, 0) <tf.Tensor: shape=(1, 2, 3), dtype=int32, numpy= array([[[1, 2, 3], [4, 5, 6]]], dtype=int32)> >>> tf.expand_dims(t, 1) <tf.Tensor: shape=(2, 1, 3), dtype=int32, numpy= array([[[1, 2, 3]], [[4, 5, 6]]], dtype=int32)> >>> tf.expand_dims(t, 2) <tf.Tensor: shape=(2, 3, 1), dtype=int32, numpy= array([[[1], [2], [3]], [[4], [5], [6]]], dtype=int32)> >>> tf.expand_dims(t, -1) # Last dimension index. In this case, same as 2. <tf.Tensor: shape=(2, 3, 1), dtype=int32, numpy= array([[[1], [2], [3]], [[4], [5], [6]]], dtype=int32)> This operation is related to: * `tf.squeeze`, which removes dimensions of size 1. * `tf.reshape`, which provides more flexible reshaping capability Args: input: A `Tensor`. axis: Integer specifying the dimension index at which to expand the shape of `input`. Given an input of D dimensions, `axis` must be in range `[-(D+1), D]` (inclusive). name: Optional string. The name of the output `Tensor`. Returns: A tensor with the same data as `input`, with an additional dimension inserted at the index specified by `axis`. Raises: ValueError: If `axis` is not specified. InvalidArgumentError: If `axis` is out of range `[-(D+1), D]`. """ return gen_array_ops.expand_dims(input, axis, name)
def strings_split_v1( input=None, sep=None, maxsplit=-1, # pylint: disable=redefined-builtin result_type="SparseTensor", source=None, name=None): """Split elements of `input` based on `sep`. Let N be the size of `input` (typically N will be the batch size). Split each element of `input` based on `sep` and return a `SparseTensor` or `RaggedTensor` containing the split tokens. Empty tokens are ignored. Examples: >>> print(tf.compat.v1.strings.split(['hello world', 'a b c'])) SparseTensor(indices=tf.Tensor( [[0 0] [0 1] [1 0] [1 1] [1 2]], ...), values=tf.Tensor([b'hello' b'world' b'a' b'b' b'c'], ...), dense_shape=tf.Tensor([2 3], shape=(2,), dtype=int64)) >>> print(tf.compat.v1.strings.split(['hello world', 'a b c'], ... result_type="RaggedTensor")) <tf.RaggedTensor [[b'hello', b'world'], [b'a', b'b', b'c']]> If `sep` is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings. For example, `input` of `"1<>2<><>3"` and `sep` of `"<>"` returns `["1", "2", "", "3"]`. If `sep` is None or an empty string, consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Note that the above mentioned behavior matches python's str.split. Args: input: A string `Tensor` of rank `N`, the strings to split. If `rank(input)` is not known statically, then it is assumed to be `1`. sep: `0-D` string `Tensor`, the delimiter character. maxsplit: An `int`. If `maxsplit > 0`, limit of the split of the result. result_type: The tensor type for the result: one of `"RaggedTensor"` or `"SparseTensor"`. source: alias for "input" argument. name: A name for the operation (optional). Raises: ValueError: If sep is not a string. Returns: A `SparseTensor` or `RaggedTensor` of rank `N+1`, the strings split according to the delimiter. """ input = deprecation.deprecated_argument_lookup("input", input, "source", source) with ops.name_scope(name, "StringSplit", [input]): input = ragged_tensor.convert_to_tensor_or_ragged_tensor( input, dtype=dtypes.string, name="input") if input.shape.rank == 0: input = gen_array_ops.expand_dims(input, 0) if result_type == "SparseTensor": if input.shape.rank == 1: return string_ops.string_split_v2(input, sep=sep, maxsplit=maxsplit) else: return string_split_v2(input, sep=sep, maxsplit=maxsplit).to_sparse() elif result_type == "RaggedTensor": return string_split_v2(input, sep=sep, maxsplit=maxsplit) else: raise ValueError( "result_type must be 'RaggedTensor' or 'SparseTensor'.")
def expand_dims(input, axis=None, name=None, dim=None): axis = deprecation.deprecated_argument_lookup("axis", axis, "dim", dim) return gen_array_ops.expand_dims(input, axis, name)