tf.experimental.ExtensionType
Stay organized with collections
Save and categorize content based on your preferences.
Base class for TensorFlow ExtensionType
classes.
tf.experimental.ExtensionType(
*args, **kwargs
)
Used in the notebooks
Tensorflow ExtensionType
classes are specialized Python classes that can be
used transparently with TensorFlow -- e.g., they can be used with ops
such as tf.cond
or tf.while_loop
and used as inputs or outputs for
tf.function
and Keras layers.
New ExtensionType
classes are defined by creating a subclass of
tf.ExtensionType
that
contains type annotations for all instance variables. The following type
annotations are supported:
Type |
Example |
Python integers |
i: int |
Python floats |
f: float |
Python strings |
s: str |
Python booleans |
b: bool |
Python None |
n: None |
Python tuple |
params: tuple[int, float, int, int] |
Python tuple w/ Ellipsis |
lengths: tuple[int, ...] |
Tensors |
t: tf.Tensor |
Composite Tensors |
rt: tf.RaggedTensor |
Extension Types |
m: MyMaskedTensor |
Tensor shapes |
shape: tf.TensorShape |
Tensor dtypes |
dtype: tf.DType |
Type unions |
length: typing.Union[int, float] |
Tuples |
params: typing.Tuple[int, float, int, int] |
Tuples w/ Ellipsis |
lengths: typing.Tuple[int, ...] |
Mappings |
tags: typing.Mapping[str, str] |
Fields annotated with typing.Mapping
will be stored using an immutable
mapping type.
ExtensionType values are immutable -- i.e., once constructed, you can not
modify or delete any of their instance members.
Examples
class MaskedTensor(ExtensionType):
values: tf.Tensor
mask: tf.Tensor
class Toy(ExtensionType):
name: str
price: tensor.Tensor
features: typing.Mapping[str, tf.Tensor]
class ToyStore(ExtensionType):
name: str
toys: typing.Tuple[Toy, ...]
Methods
__eq__
View source
__eq__(
other
)
Return self==value.
__ne__
View source
__ne__(
other
)
Return self!=value.
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.experimental.ExtensionType\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/extension_type.py#L99-L329) |\n\nBase class for TensorFlow `ExtensionType` classes.\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.experimental.ExtensionType`](https://www.tensorflow.org/api_docs/python/tf/experimental/ExtensionType)\n\n\u003cbr /\u003e\n\n tf.experimental.ExtensionType(\n *args, **kwargs\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------|\n| - [Extension types](https://www.tensorflow.org/guide/extension_type) |\n\nTensorflow `ExtensionType` classes are specialized Python classes that can be\nused transparently with TensorFlow -- e.g., they can be used with ops\nsuch as [`tf.cond`](../../tf/cond) or [`tf.while_loop`](../../tf/while_loop) and used as inputs or outputs for\n[`tf.function`](../../tf/function) and Keras layers.\n\nNew `ExtensionType` classes are defined by creating a subclass of\n`tf.ExtensionType` that\ncontains type annotations for all instance variables. The following type\nannotations are supported:\n\n| Type | Example |\n|--------------------------|----------------------------------------------|\n| Python integers | `i: int` |\n| Python floats | `f: float` |\n| Python strings | `s: str` |\n| Python booleans | `b: bool` |\n| Python None | `n: None` |\n| Python tuple | `params: tuple[int, float, int, int]` |\n| Python tuple w/ Ellipsis | `lengths: tuple[int, ...]` |\n| Tensors | `t: tf.Tensor` |\n| Composite Tensors | `rt: tf.RaggedTensor` |\n| Extension Types | `m: MyMaskedTensor` |\n| Tensor shapes | `shape: tf.TensorShape` |\n| Tensor dtypes | `dtype: tf.DType` |\n| Type unions | `length: typing.Union[int, float]` |\n| Tuples | `params: typing.Tuple[int, float, int, int]` |\n| Tuples w/ Ellipsis | `lengths: typing.Tuple[int, ...]` |\n| Mappings | `tags: typing.Mapping[str, str]` |\n\nFields annotated with `typing.Mapping` will be stored using an immutable\nmapping type.\n\nExtensionType values are immutable -- i.e., once constructed, you can not\nmodify or delete any of their instance members.\n\n### Examples\n\n class MaskedTensor(ExtensionType):\n values: tf.Tensor\n mask: tf.Tensor\n\n class Toy(ExtensionType):\n name: str\n price: tensor.Tensor\n features: typing.Mapping[str, tf.Tensor]\n\n class ToyStore(ExtensionType):\n name: str\n toys: typing.Tuple[Toy, ...]\n\nMethods\n-------\n\n### `__eq__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/extension_type.py#L276-L305) \n\n __eq__(\n other\n )\n\nReturn self==value.\n\n### `__ne__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/extension_type.py#L307-L312) \n\n __ne__(\n other\n )\n\nReturn self!=value."]]