tf.compat.v1.layers.experimental.keras_style_scope
Stay organized with collections
Save and categorize content based on your preferences.
Use Keras-style variable management.
@tf_contextlib.contextmanager
tf.compat.v1.layers.experimental.keras_style_scope()
All tf.layers and tf RNN cells created in this scope use Keras-style
variable management. Creating such layers with a scope= argument is
disallowed, and reuse=True is disallowed.
The purpose of this scope is to allow users of existing layers to
slowly transition to a Keras layers API without breaking existing
functionality.
One example of this is when using TensorFlow's RNN classes with Keras
Models or Networks. Because Keras models do not properly set variable
scopes, users of RNNs may either accidentally share scopes between two
different models, or get errors about variables that already exist.
Example:
class RNNModel(tf.keras.Model):
def __init__(self, name):
super(RNNModel, self).__init__(name=name)
self.rnn = tf.compat.v1.nn.rnn_cell.MultiRNNCell(
[tf.compat.v1.nn.rnn_cell.LSTMCell(64) for _ in range(2)])
def call(self, input, state):
return self.rnn(input, state)
model_1 = RNNModel("model_1")
model_2 = RNNModel("model_2")
# OK
output_1, next_state_1 = model_1(input, state)
# Raises an error about trying to create an already existing variable.
output_2, next_state_2 = model_2(input, state)
The solution is to wrap the model construction and execution in a keras-style
scope:
with keras_style_scope():
model_1 = RNNModel("model_1")
model_2 = RNNModel("model_2")
# model_1 and model_2 are guaranteed to create their own variables.
output_1, next_state_1 = model_1(input, state)
output_2, next_state_2 = model_2(input, state)
assert len(model_1.weights) > 0
assert len(model_2.weights) > 0
assert(model_1.weights != model_2.weights)
Yields:
A keras layer style scope.
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.
Last updated 2020-10-01 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 2020-10-01 UTC."],[],[],null,["# tf.compat.v1.layers.experimental.keras_style_scope\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/layers/base.py#L45-L111) |\n\nUse Keras-style variable management. \n\n @tf_contextlib.contextmanager\n tf.compat.v1.layers.experimental.keras_style_scope()\n\nAll tf.layers and tf RNN cells created in this scope use Keras-style\nvariable management. Creating such layers with a scope= argument is\ndisallowed, and reuse=True is disallowed.\n\nThe purpose of this scope is to allow users of existing layers to\nslowly transition to a Keras layers API without breaking existing\nfunctionality.\n\nOne example of this is when using TensorFlow's RNN classes with Keras\nModels or Networks. Because Keras models do not properly set variable\nscopes, users of RNNs may either accidentally share scopes between two\ndifferent models, or get errors about variables that already exist.\n\n#### Example:\n\n class RNNModel(tf.keras.Model):\n\n def __init__(self, name):\n super(RNNModel, self).__init__(name=name)\n self.rnn = tf.compat.v1.nn.rnn_cell.MultiRNNCell(\n [tf.compat.v1.nn.rnn_cell.LSTMCell(64) for _ in range(2)])\n\n def call(self, input, state):\n return self.rnn(input, state)\n\n model_1 = RNNModel(\"model_1\")\n model_2 = RNNModel(\"model_2\")\n\n # OK\n output_1, next_state_1 = model_1(input, state)\n # Raises an error about trying to create an already existing variable.\n output_2, next_state_2 = model_2(input, state)\n\nThe solution is to wrap the model construction and execution in a keras-style\nscope: \n\n with keras_style_scope():\n model_1 = RNNModel(\"model_1\")\n model_2 = RNNModel(\"model_2\")\n\n # model_1 and model_2 are guaranteed to create their own variables.\n output_1, next_state_1 = model_1(input, state)\n output_2, next_state_2 = model_2(input, state)\n\n assert len(model_1.weights) \u003e 0\n assert len(model_2.weights) \u003e 0\n assert(model_1.weights != model_2.weights)\n\n#### Yields:\n\nA keras layer style scope."]]