tf.saved_model.Asset
Stay organized with collections
Save and categorize content based on your preferences.
Represents a file asset to hermetically include in a SavedModel.
tf.saved_model.Asset(
path
)
Used in the notebooks
A SavedModel can include arbitrary files, called assets, that are needed
for its use. For example a vocabulary file used initialize a lookup table.
When a trackable object is exported via tf.saved_model.save()
, all the
Asset
s reachable from it are copied into the SavedModel assets directory.
Upon loading, the assets and the serialized functions that depend on them
will refer to the correct filepaths inside the SavedModel directory.
Example:
filename = tf.saved_model.Asset("file.txt")
@tf.function(input_signature=[])
def func():
return tf.io.read_file(filename)
trackable_obj = tf.train.Checkpoint()
trackable_obj.func = func
trackable_obj.filename = filename
tf.saved_model.save(trackable_obj, "/tmp/saved_model")
# The created SavedModel is hermetic, it does not depend on
# the original file and can be moved to another path.
tf.io.gfile.remove("file.txt")
tf.io.gfile.rename("/tmp/saved_model", "/tmp/new_location")
reloaded_obj = tf.saved_model.load("/tmp/new_location")
print(reloaded_obj.func())
Attributes |
asset_path
|
A path, or a 0-D tf.string tensor with path to the asset.
|
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.saved_model.Asset\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/trackable/asset.py#L30-L112) |\n\nRepresents a file asset to hermetically include in a SavedModel.\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.saved_model.Asset`](https://www.tensorflow.org/api_docs/python/tf/saved_model/Asset)\n\n\u003cbr /\u003e\n\n tf.saved_model.Asset(\n path\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------------------|\n| - [Subword tokenizers](https://www.tensorflow.org/text/guide/subwords_tokenizer) |\n\nA SavedModel can include arbitrary files, called assets, that are needed\nfor its use. For example a vocabulary file used initialize a lookup table.\n\nWhen a trackable object is exported via [`tf.saved_model.save()`](../../tf/saved_model/save), all the\n`Asset`s reachable from it are copied into the SavedModel assets directory.\nUpon loading, the assets and the serialized functions that depend on them\nwill refer to the correct filepaths inside the SavedModel directory.\n\n#### Example:\n\n filename = tf.saved_model.Asset(\"file.txt\")\n\n @tf.function(input_signature=[])\n def func():\n return tf.io.read_file(filename)\n\n trackable_obj = tf.train.Checkpoint()\n trackable_obj.func = func\n trackable_obj.filename = filename\n tf.saved_model.save(trackable_obj, \"/tmp/saved_model\")\n\n # The created SavedModel is hermetic, it does not depend on\n # the original file and can be moved to another path.\n tf.io.gfile.remove(\"file.txt\")\n tf.io.gfile.rename(\"/tmp/saved_model\", \"/tmp/new_location\")\n\n reloaded_obj = tf.saved_model.load(\"/tmp/new_location\")\n print(reloaded_obj.func())\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|--------------|--------------------------------------------------------------------------------|\n| `asset_path` | A path, or a 0-D [`tf.string`](../../tf#string) tensor with path to the asset. |\n\n\u003cbr /\u003e"]]