tf.strings.format
Stay organized with collections
Save and categorize content based on your preferences.
Formats a string template using a list of tensors.
tf.strings.format(
template, inputs, placeholder='{}', summarize=3, name=None
)
Formats a string template using a list of tensors, abbreviating tensors by
only printing the first and last summarize
elements of each dimension
(recursively). If formatting only one tensor into a template, the tensor does
not have to be wrapped in a list.
Example |
Formatting a single-tensor template:
tensor = tf.range(5)
tf.strings.format("tensor: {}, suffix", tensor)
<tf.Tensor: shape=(), dtype=string, numpy=b'tensor: [0 1 2 3 4], suffix'>
Formatting a multi-tensor template:
tensor_a = tf.range(2)
tensor_b = tf.range(1, 4, 2)
tf.strings.format("a: {}, b: {}, suffix", (tensor_a, tensor_b))
<tf.Tensor: shape=(), dtype=string, numpy=b'a: [0 1], b: [1 3], suffix'>
|
Args |
template
|
A string template to format tensor values into.
|
inputs
|
A list of Tensor objects, or a single Tensor.
The list of tensors to format into the template string. If a solitary
tensor is passed in, the input tensor will automatically be wrapped as a
list.
|
placeholder
|
An optional string . Defaults to {} .
At each placeholder occurring in the template, a subsequent tensor
will be inserted.
|
summarize
|
An optional int . Defaults to 3 .
When formatting the tensors, show the first and last summarize
entries of each tensor dimension (recursively). If set to -1, all
elements of the tensor will be shown.
|
name
|
A name for the operation (optional).
|
Returns |
A scalar Tensor of type string .
|
Raises |
ValueError
|
if the number of placeholders does not match the number of
inputs.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.strings.format\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/string_ops.py#L115-L176) |\n\nFormats a string template using a list of tensors.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.strings.format`](https://www.tensorflow.org/api_docs/python/tf/strings/format)\n\n\u003cbr /\u003e\n\n tf.strings.format(\n template, inputs, placeholder='{}', summarize=3, name=None\n )\n\nFormats a string template using a list of tensors, abbreviating tensors by\nonly printing the first and last `summarize` elements of each dimension\n(recursively). If formatting only one tensor into a template, the tensor does\nnot have to be wrapped in a list.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Example ------- ||\n|---|---|\n| Formatting a single-tensor template: \u003cbr /\u003e tensor = tf.range(5) tf.strings.format(\"tensor: {}, suffix\", tensor) \u003ctf.Tensor: shape=(), dtype=string, numpy=b'tensor: [0 1 2 3 4], suffix'\u003e Formatting a multi-tensor template: tensor_a = tf.range(2) tensor_b = tf.range(1, 4, 2) tf.strings.format(\"a: {}, b: {}, suffix\", (tensor_a, tensor_b)) \u003ctf.Tensor: shape=(), dtype=string, numpy=b'a: [0 1], b: [1 3], suffix'\u003e \u003cbr /\u003e ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `template` | A string template to format tensor values into. |\n| `inputs` | A list of `Tensor` objects, or a single Tensor. The list of tensors to format into the template string. If a solitary tensor is passed in, the input tensor will automatically be wrapped as a list. |\n| `placeholder` | An optional `string`. Defaults to `{}`. At each placeholder occurring in the template, a subsequent tensor will be inserted. |\n| `summarize` | An optional `int`. Defaults to `3`. When formatting the tensors, show the first and last `summarize` entries of each tensor dimension (recursively). If set to -1, all elements of the tensor will be shown. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A scalar `Tensor` of type `string`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------------------------------------------------|\n| `ValueError` | if the number of placeholders does not match the number of inputs. |\n\n\u003cbr /\u003e"]]