This wraps func_ in a Template and partially evaluates it. Templates are
functions that create variables the first time they are called and reuse them
thereafter. In order for func_ to be compatible with a Template it must
have the following properties:
The function should create all trainable variables and any variables that
should be reused by calling tf.compat.v1.get_variable. If a trainable
variable is
created using tf.Variable, then a ValueError will be thrown. Variables
that are intended to be locals can be created by specifying
tf.Variable(..., trainable=false).
The function may use variable scopes and other templates internally to
create and reuse variables, but it shouldn't use
tf.compat.v1.global_variables to
capture variables that are defined outside of the scope of the function.
Internal scopes and variable names should not depend on any arguments that
are not supplied to make_template. In general you will get a ValueError
telling you that you are trying to reuse a variable that doesn't exist
if you make a mistake.
In the following example, both z and w will be scaled by the same y. It
is important to note that if we didn't assign scalar_name and used a
different name for z and w that a ValueError would be thrown because it
couldn't reuse the variable.
As a safe-guard, the returned function will raise a ValueError after the
first call if trainable variables are created by calling tf.Variable.
If all of these are true, then 2 properties are enforced by the template:
Calling the same template multiple times will share all non-local
variables.
Two different templates are guaranteed to be unique, unless you reenter the
same variable scope as the initial definition of a template and redefine
it. An examples of this exception:
defmy_op(x,scalar_name):var1=tf.compat.v1.get_variable(scalar_name,shape=[],initializer=tf.compat.v1.constant_initializer(1))returnx*var1withtf.compat.v1.variable_scope('scope')asvs:scale_by_y=tf.compat.v1.make_template('scale_by_y',my_op,scalar_name='y')z=scale_by_y(input1)w=scale_by_y(input2)# Creates a template that reuses the variables above.withtf.compat.v1.variable_scope(vs,reuse=True):scale_by_y2=tf.compat.v1.make_template('scale_by_y',my_op,scalar_name='y')z2=scale_by_y2(input1)w2=scale_by_y2(input2)
Depending on the value of create_scope_now_, the full variable scope may be
captured either at the time of first call or at the time of construction. If
this option is set to True, then all Tensors created by repeated calls to the
template will have an extra trailing _N+1 to their name, as the first time the
scope is entered in the Template constructor no Tensors are created.
Args
name_
A name for the scope created by this template. If necessary, the name
will be made unique by appending _N to the name.
func_
The function to wrap.
create_scope_now_
Boolean controlling whether the scope should be created
when the template is constructed or when the template is called. Default
is False, meaning the scope is created when the template is called.
unique_name_
When used, it overrides name_ and is not made unique. If a
template of the same scope/unique_name already exists and reuse is false,
an error is raised. Defaults to None.
custom_getter_
Optional custom getter for variables used in func_. See
the tf.compat.v1.get_variablecustom_getter documentation for more
information.
**kwargs
Keyword arguments to apply to func_.
Returns
A function to encapsulate a set of variables which should be created once
and reused. An enclosing scope will be created either when make_template
is called or when the result is called, depending on the value of
create_scope_now_. Regardless of the value, the first time the template
is called it will enter the scope with no reuse, and call func_ to create
variables, which are guaranteed to be unique. All subsequent calls will
re-enter the scope and reuse those variables.
[[["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 2020-10-01 UTC."],[],[],null,["# tf.compat.v1.make_template\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/template.py#L39-L161) |\n\nGiven an arbitrary function, wrap it so that it does variable sharing. \n\n tf.compat.v1.make_template(\n name_, func_, create_scope_now_=False, unique_name_=None, custom_getter_=None,\n **kwargs\n )\n\nThis wraps `func_` in a Template and partially evaluates it. Templates are\nfunctions that create variables the first time they are called and reuse them\nthereafter. In order for `func_` to be compatible with a `Template` it must\nhave the following properties:\n\n- The function should create all trainable variables and any variables that should be reused by calling [`tf.compat.v1.get_variable`](../../../tf/compat/v1/get_variable). If a trainable variable is created using [`tf.Variable`](../../../tf/Variable), then a ValueError will be thrown. Variables that are intended to be locals can be created by specifying [`tf.Variable(..., trainable=false)`](../../../tf/Variable).\n- The function may use variable scopes and other templates internally to create and reuse variables, but it shouldn't use [`tf.compat.v1.global_variables`](../../../tf/compat/v1/global_variables) to capture variables that are defined outside of the scope of the function.\n- Internal scopes and variable names should not depend on any arguments that are not supplied to `make_template`. In general you will get a ValueError telling you that you are trying to reuse a variable that doesn't exist if you make a mistake.\n\nIn the following example, both `z` and `w` will be scaled by the same `y`. It\nis important to note that if we didn't assign `scalar_name` and used a\ndifferent name for z and w that a `ValueError` would be thrown because it\ncouldn't reuse the variable. \n\n def my_op(x, scalar_name):\n var1 = tf.compat.v1.get_variable(scalar_name,\n shape=[],\n initializer=tf.compat.v1.constant_initializer(1))\n return x * var1\n\n scale_by_y = tf.compat.v1.make_template('scale_by_y', my_op, scalar_name='y')\n\n z = scale_by_y(input1)\n w = scale_by_y(input2)\n\nAs a safe-guard, the returned function will raise a `ValueError` after the\nfirst call if trainable variables are created by calling [`tf.Variable`](../../../tf/Variable).\n\nIf all of these are true, then 2 properties are enforced by the template:\n\n1. Calling the same template multiple times will share all non-local variables.\n2. Two different templates are guaranteed to be unique, unless you reenter the same variable scope as the initial definition of a template and redefine it. An examples of this exception:\n\n def my_op(x, scalar_name):\n var1 = tf.compat.v1.get_variable(scalar_name,\n shape=[],\n initializer=tf.compat.v1.constant_initializer(1))\n return x * var1\n\n with tf.compat.v1.variable_scope('scope') as vs:\n scale_by_y = tf.compat.v1.make_template('scale_by_y', my_op,\n scalar_name='y')\n z = scale_by_y(input1)\n w = scale_by_y(input2)\n\n # Creates a template that reuses the variables above.\n with tf.compat.v1.variable_scope(vs, reuse=True):\n scale_by_y2 = tf.compat.v1.make_template('scale_by_y', my_op,\n scalar_name='y')\n z2 = scale_by_y2(input1)\n w2 = scale_by_y2(input2)\n\nDepending on the value of `create_scope_now_`, the full variable scope may be\ncaptured either at the time of first call or at the time of construction. If\nthis option is set to True, then all Tensors created by repeated calls to the\ntemplate will have an extra trailing _N+1 to their name, as the first time the\nscope is entered in the Template constructor no Tensors are created.\n| **Note:** `name_`, `func_` and `create_scope_now_` have a trailing underscore to reduce the likelihood of collisions with kwargs.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `name_` | A name for the scope created by this template. If necessary, the name will be made unique by appending `_N` to the name. |\n| `func_` | The function to wrap. |\n| `create_scope_now_` | Boolean controlling whether the scope should be created when the template is constructed or when the template is called. Default is False, meaning the scope is created when the template is called. |\n| `unique_name_` | When used, it overrides name_ and is not made unique. If a template of the same scope/unique_name already exists and reuse is false, an error is raised. Defaults to None. |\n| `custom_getter_` | Optional custom getter for variables used in `func_`. See the [`tf.compat.v1.get_variable`](../../../tf/compat/v1/get_variable) `custom_getter` documentation for more information. |\n| `**kwargs` | Keyword arguments to apply to `func_`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A function to encapsulate a set of variables which should be created once and reused. An enclosing scope will be created either when `make_template` is called or when the result is called, depending on the value of `create_scope_now_`. Regardless of the value, the first time the template is called it will enter the scope with no reuse, and call `func_` to create variables, which are guaranteed to be unique. All subsequent calls will re-enter the scope and reuse those variables. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------|\n| `ValueError` | if `name_` is None. |\n\n\u003cbr /\u003e"]]