This object is used to memoize the computation of some function. Upon first
call, the user provided create_value_fn is called and with the args/kwargs
provided to this object's __call__. On subsequent calls the previous result
is returned and regardless of the args/kwargs provided to this object's
__call__. To trigger a new evaluation, invoke this.reset() and to
identify if a new evaluation will execute (on-demand) invoke
this.is_unset(). For an example application of this object, see
help(tfp.experimental.nn.util.RandomVariable) and/or
help(tfp.util.DeferredTensor).
Args
create_value_fn
Python callable which takes any input args/kwargs and
returns a value to memoize. (The value is not presumed to be of any
particular type.)
Attributes
create_value_fn
name
Returns the name of this module as passed or determined in the ctor.
[[["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 2023-11-21 UTC."],[],[],null,["# tfp.experimental.nn.util.CallOnce\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/experimental/nn/util/random_variable.py#L142-L207) |\n\nFunction object which memoizes the result of `create_value_fn()`. \n\n tfp.experimental.nn.util.CallOnce(\n create_value_fn\n )\n\nThis object is used to memoize the computation of some function. Upon first\ncall, the user provided `create_value_fn` is called and with the args/kwargs\nprovided to this object's `__call__`. On subsequent calls the previous result\nis returned and **regardless of the args/kwargs provided to this object's\n`__call__`** . To trigger a new evaluation, invoke `this.reset()` and to\nidentify if a new evaluation will execute (on-demand) invoke\n`this.is_unset()`. For an example application of this object, see\n`help(tfp.experimental.nn.util.RandomVariable)` and/or\n`help(tfp.util.DeferredTensor)`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| `create_value_fn` | Python `callable` which takes any input args/kwargs and returns a value to memoize. (The value is not presumed to be of any particular type.) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `create_value_fn` | \u003cbr /\u003e \u003cbr /\u003e |\n| `name` | Returns the name of this module as passed or determined in the ctor. \u003cbr /\u003e | **Note:** This is not the same as the `self.name_scope.name` which includes parent module names. |\n| `name_scope` | Returns a [`tf.name_scope`](https://www.tensorflow.org/api_docs/python/tf/name_scope) instance for this class. |\n| `non_trainable_variables` | Sequence of non-trainable variables owned by this module and its submodules.**Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |\n| `submodules` | Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on). a = tf.Module() b = tf.Module() c = tf.Module() a.b = b b.c = c list(a.submodules) == [b, c] True list(b.submodules) == [c] True list(c.submodules) == [] True \u003cbr /\u003e |\n| `trainable_variables` | Sequence of trainable variables owned by this module and its submodules. \u003cbr /\u003e | **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |\n| `value` | \u003cbr /\u003e |\n| `variables` | Sequence of variables owned by this module and its submodules. \u003cbr /\u003e | **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |\n\nMethods\n-------\n\n### `is_unset`\n\n[View source](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/experimental/nn/util/random_variable.py#L180-L182) \n\n is_unset()\n\nReturns `True` if there is no memoized value and `False` otherwise.\n\n### `reset`\n\n[View source](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/experimental/nn/util/random_variable.py#L205-L207) \n\n reset()\n\nRemoves memoized value which triggers re-eval on subsequent reads.\n\n### `with_name_scope`\n\n @classmethod\n with_name_scope(\n method\n )\n\nDecorator to automatically enter the module name scope. \n\n class MyModule(tf.Module):\n @tf.Module.with_name_scope\n def __call__(self, x):\n if not hasattr(self, 'w'):\n self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))\n return tf.matmul(x, self.w)\n\nUsing the above module would produce [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable)s and [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s whose\nnames included the module name: \n\n mod = MyModule()\n mod(tf.ones([1, 2]))\n \u003ctf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)\u003e\n mod.w\n \u003ctf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,\n numpy=..., dtype=float32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|---------------------|\n| `method` | The method to wrap. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| The original method wrapped such that it enters the module's name scope. ||\n\n\u003cbr /\u003e\n\n### `__call__`\n\n[View source](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/experimental/nn/util/random_variable.py#L184-L203) \n\n __call__(\n *args, **kwargs\n )\n\nReturn the memoized value."]]