tf.saved_model.experimental.TrackableResource
Stay organized with collections
Save and categorize content based on your preferences.
Holds a Tensor which a tf.function can capture.
tf.saved_model.experimental.TrackableResource(
device=''
)
A TrackableResource is most useful for stateful Tensors that require
initialization, such as tf.lookup.StaticHashTable
. TrackableResource
s
are discovered by traversing the graph of object attributes, e.g. during
tf.saved_model.save
.
A TrackableResource has three methods to override:
_create_resource
should create the resource tensor handle.
_initialize
should initialize the resource held at self.resource_handle
.
_destroy_resource
is called upon a TrackableResource
's destruction
and should decrement the resource's ref count. For most resources, this
should be done with a call to tf.raw_ops.DestroyResourceOp
.
Example usage:
class DemoResource(tf.saved_model.experimental.TrackableResource):
def __init__(self):
super().__init__()
self._initialize()
def _create_resource(self):
return tf.raw_ops.VarHandleOp(dtype=tf.float32, shape=[2])
def _initialize(self):
tf.raw_ops.AssignVariableOp(
resource=self.resource_handle, value=tf.ones([2]))
def _destroy_resource(self):
tf.raw_ops.DestroyResourceOp(resource=self.resource_handle)
class DemoModule(tf.Module):
def __init__(self):
self.resource = DemoResource()
def increment(self, tensor):
return tensor + tf.raw_ops.ReadVariableOp(
resource=self.resource.resource_handle, dtype=tf.float32)
demo = DemoModule()
demo.increment([5, 1])
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([6., 2.], dtype=float32)>
Args |
device
|
A string indicating a required placement for this resource,
e.g. "CPU" if this resource must be created on a CPU device. A blank
device allows the user to place resource creation, so generally this
should be blank unless the resource only makes sense on one device.
|
Attributes |
resource_handle
|
Returns the resource handle associated with this Resource.
|
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 2022-11-04 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2022-11-04 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 2022-11-04 UTC."],[],[]]