tf.type_spec_from_value
Stay organized with collections
Save and categorize content based on your preferences.
Returns a tf.TypeSpec
that represents the given value
.
tf.type_spec_from_value(
value
) -> tf.TypeSpec
Used in the notebooks
Examples |
>>> tf.type_spec_from_value(tf.constant([1, 2, 3]))
TensorSpec(shape=(3,), dtype=tf.int32, name=None)
>>> tf.type_spec_from_value(np.array([4.0, 5.0], np.float64))
TensorSpec(shape=(2,), dtype=tf.float64, name=None)
>>> tf.type_spec_from_value(tf.ragged.constant([[1, 2], [3, 4, 5]]))
RaggedTensorSpec(TensorShape([2, None]), tf.int32, 1, tf.int64)
example_input = tf.ragged.constant([[1, 2], [3]])
@tf.function(input_signature=[tf.type_spec_from_value(example_input)])
def f(x):
return tf.reduce_sum(x, axis=1)
|
Returns |
A TypeSpec that is compatible with value .
|
Raises |
TypeError
|
If a TypeSpec cannot be built for value , because its type
is not supported.
|
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.type_spec_from_value\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/type_spec.py#L958-L1004) |\n\nReturns a [`tf.TypeSpec`](../tf/TypeSpec) that represents the given `value`.\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.type_spec_from_value`](https://www.tensorflow.org/api_docs/python/tf/type_spec_from_value)\n\n\u003cbr /\u003e\n\n tf.type_spec_from_value(\n value\n ) -\u003e ../tf/TypeSpec\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------|\n| - [Extension types](https://www.tensorflow.org/guide/extension_type) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Examples -------- ||\n|---|---|\n| \u003cbr /\u003e \u003e\u003e\u003e tf.type_spec_from_value(tf.constant([1, 2, 3])) TensorSpec(shape=(3,), dtype=tf.int32, name=None) \u003e\u003e\u003e tf.type_spec_from_value(np.array([4.0, 5.0], np.float64)) TensorSpec(shape=(2,), dtype=tf.float64, name=None) \u003e\u003e\u003e tf.type_spec_from_value(tf.ragged.constant([[1, 2], [3, 4, 5]])) RaggedTensorSpec(TensorShape([2, None]), tf.int32, 1, tf.int64) example_input = tf.ragged.constant([[1, 2], [3]]) @tf.function(input_signature=[tf.type_spec_from_value(example_input)]) def f(x): return tf.reduce_sum(x, axis=1) \u003cbr /\u003e ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `value` | A value that can be accepted or returned by TensorFlow APIs. Accepted types for `value` include [`tf.Tensor`](../tf/Tensor), any value that can be converted to [`tf.Tensor`](../tf/Tensor) using [`tf.convert_to_tensor`](../tf/convert_to_tensor), and any subclass of `CompositeTensor` (such as [`tf.RaggedTensor`](../tf/RaggedTensor)). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `TypeSpec` that is compatible with `value`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|-------------------------------------------------------------------------------|\n| `TypeError` | If a TypeSpec cannot be built for `value`, because its type is not supported. |\n\n\u003cbr /\u003e"]]