View source on GitHub
|
RNNCell wrapper that ensures cell inputs are added to the outputs. (deprecated)
Inherits From: Module
tf.nn.RNNCellResidualWrapper(
*args, **kwargs
)
Args |
|---|
cell
RNNCell.
residual_fn
**kwargs
Attributes |
|---|
activity_regularizer
compute_dtype
This is equivalent to Layer.dtype_policy.compute_dtype. Unless
mixed precision is used, this is the same as Layer.dtype, the dtype of
the weights.
Layers automatically cast their inputs to the compute dtype, which causes
computations and the output to be in the compute dtype as well. This is done
by the base Layer class in Layer.call, so you do not have to insert
these casts if implementing your own layer.
Layers often perform certain internal computations in higher precision when
compute_dtype is float16 or bfloat16 for numeric stability. The output
will still typically be float16 or bfloat16 in such cases.
dtype
This is equivalent to Layer.dtype_policy.variable_dtype. Unless
mixed precision is used, this is the same as Layer.compute_dtype, the
dtype of the layer's computations.
dtype_policy
This is an instance of a tf.keras.mixed_precision.Policy.
dynamic
input
Only applicable if the layer has exactly one input, i.e. if it is connected to one incoming layer.
input_spec
InputSpec instance(s) describing the input format for this layer.When you create a layer subclass, you can set self.input_spec to enable
the layer to run input compatibility checks when it is called.
Consider a Conv2D layer: it can only be called on a single input tensor
of rank 4. As such, you can set, in __init__():
self.input_spec = tf.keras.layers.InputSpec(ndim=4)
Now, if you try to call the layer on an input that isn't rank 4
(for instance, an input of shape (2,), it will raise a nicely-formatted
error:
ValueError: Input 0 of layer conv2d is incompatible with the layer:
expected ndim=4, found ndim=1. Full shape received: [2]
Input checks that can be specified via input_spec include:
- Structure (e.g. a single input, a list of 2 inputs, etc)
- Shape
- Rank (ndim)
- Dtype
For more information, see tf.keras.layers.InputSpec.
losses
add_loss() API.Variable regularization tensors are created when this property is accessed,
so it is eager safe: accessing losses under a tf.GradientTape will
propagate gradients back to the corresponding variables.
class MyLayer(tf.keras.layers.Layer):def call(self, inputs):self.add_loss(tf.abs(tf.reduce_mean(inputs)))return inputsl = MyLayer()l(np.ones((10, 1)))l.losses[1.0]
inputs = tf.keras.Input(shape=(10,))x = tf.keras.layers.Dense(10)(inputs)outputs = tf.keras.layers.Dense(1)(x)model = tf.keras.Model(inputs, outputs)# Activity regularization.len(model.losses)0model.add_loss(tf.abs(tf.reduce_mean(x)))len(model.losses)1
inputs = tf.keras.Input(shape=(10,))d = tf.keras.layers.Dense(10, kernel_initializer='ones')x = d(inputs)outputs = tf.keras.layers.Dense(1)(x)model = tf.keras.Model(inputs, outputs)# Weight regularization.model.add_loss(lambda: tf.reduce_mean(d.kernel))model.losses[<tf.Tensor: shape=(), dtype=float32, numpy=1.0>]
metrics
add_metric() API.input = tf.keras.layers.Input(shape=(3,))d = tf.keras.layers.Dense(2)output = d(input)d.add_metric(tf.reduce_max(output), name='max')d.add_metric(tf.reduce_min(output), name='min')[m.name for m in d.metrics]['max', 'min']
non_trainable_weights
Non-trainable weights are not updated during training. They are expected
to be updated manually in call().
output
Only applicable if the layer has exactly one output, i.e. if it is connected to one incoming layer.
output_size
state_size
supports_masking
compute_mask.
trainable
trainable_weights
Trainable weights are updated via gradient descent during training.
variable_dtype
Layer.dtype, the dtype of the weights.
weights
Methods
add_loss
add_loss(
losses, **kwargs
)
Add loss tensor(s), potentially dependent on layer inputs.
Some losses (for instance, activity regularization losses) may be dependent
on the inputs passed when calling a layer. Hence, when reusing the same
layer on different inputs a and b, some entries in layer.losses may
be dependent on a and some on b. This method automatically keeps track
of dependencies.
This method can be used inside a subclassed layer or model's call
function, in which case losses should be a Tensor or list of Tensors.
Example:
class MyLayer(tf.keras.layers.Layer):
def call(self, inputs):
self.add_loss(tf.abs(tf.reduce_mean(inputs)))
return inputs
This method can also be called directly on a Functional Model during
construction. In this case, any loss Tensors passed to this Model must
be symbolic and be able to be traced back to the model's Inputs. These
losses become part of the model's topology and are tracked in get_config.
Example:
inputs = tf.keras.Input(shape=(10,))
x = tf.keras.layers.Dense(10)(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
# Activity regularization.
model.add_loss(tf.abs(tf.reduce_mean(x)))
If this is not the case for your loss (if, for example, your loss references
a Variable of one of the model's layers), you can wrap your loss in a
zero-argument lambda. These losses are not tracked as part of the model's
topology since they can't be serialized.
Example:
inputs = tf.keras.Input(shape=(10,))
d = tf.keras.layers.Dense(10)
x = d(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
# Weight regularization.
model.add_loss(lambda: tf.reduce_mean(d.kernel))
| Args |
|---|
losses
**kwargs
add_metric
add_metric(
value, name=None, **kwargs
)
Adds metric tensor to the layer.
This method can be used inside the call() method of a subclassed layer
or model.
class MyMetricLayer(tf.keras.layers.Layer):
def __init__(self):
super(MyMetricLayer, self).__init__(name='my_metric_layer')
self.mean = tf.keras.metrics.Mean(name='metric_1')
def call(self, inputs):
self.add_metric(self.mean(inputs))
self.add_metric(tf.reduce_sum(inputs), name='metric_2')
return inputs
This method can also be called directly on a Functional Model during
construction. In this case, any tensor passed to this Model must
be symbolic and be able to be traced back to the model's Inputs. These
metrics become part of the model's topology and are tracked when you
save the model via save().
inputs = tf.keras.Input(shape=(10,))
x = tf.keras.layers.Dense(10)(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
model.add_metric(math_ops.reduce_sum(x), name='metric_1')
inputs = tf.keras.Input(shape=(10,))
x = tf.keras.layers.Dense(10)(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
model.add_metric(tf.keras.metrics.Mean()(x), name='metric_1')
| Args |
|---|
value
name
**kwargs
aggregation - When the value tensor provided is not the result of
calling a keras.Metric instance, it will be aggregated by default
using a keras.Metric.Mean.
build
build(
inputs_shape
)
Builds the wrapped cell.
compute_mask
compute_mask(
inputs, mask=None
)
Computes an output mask tensor.
| Args |
|---|
inputs
mask
| Returns | |
|---|---|
| None or a tensor (or list of tensors, one per output tensor of the layer). |
compute_output_shape
compute_output_shape(
input_shape
)
Computes the output shape of the layer.
If the layer has not been built, this method will call build on the
layer. This assumes that the layer will later be used with inputs that
match the input shape provided here.
| Args |
|---|
input_shape
| Returns | |
|---|---|
| An input shape tuple. |
count_params
count_params()
Count the total number of scalars composing the weights.
| Returns | |
|---|---|
| An integer count. |
| Raises |
|---|
ValueError
from_config
@classmethodfrom_config( config, custom_objects=None )
Creates a layer from its config.
This method is the reverse of get_config,
capable of instantiating the same layer from the config
dictionary. It does not handle layer connectivity
(handled by Network), nor weights (handled by set_weights).
| Args |
|---|
config
| Returns | |
|---|---|
| A layer instance. |
get_config
get_config()
Returns the config of the residual wrapper.
get_initial_state
get_initial_state(
inputs=None, batch_size=None, dtype=None
)
get_weights
get_weights()
Returns the current weights of the layer, as NumPy arrays.
The weights of a layer represent the state of the layer. This function returns both trainable and non-trainable weight values associated with this layer as a list of NumPy arrays, which can in turn be used to load state into similarly parameterized layers.
For example, a Dense layer returns a list of two values: the kernel matrix
and the bias vector. These can be used to set the weights of another
Dense layer:
layer_a = tf.keras.layers.Dense(1,kernel_initializer=tf.constant_initializer(1.))a_out = layer_a(tf.convert_to_tensor([[1., 2., 3.]]))layer_a.get_weights()[array([[1.],[1.],[1.]], dtype=float32), array([0.], dtype=float32)]layer_b = tf.keras.layers.Dense(1,kernel_initializer=tf.constant_initializer(2.))b_out = layer_b(tf.convert_to_tensor([[10., 20., 30.]]))layer_b.get_weights()[array([[2.],[2.],[2.]], dtype=float32), array([0.], dtype=float32)]layer_b.set_weights(layer_a.get_weights())layer_b.get_weights()[array([[1.],[1.],[1.]], dtype=float32), array([0.], dtype=float32)]
| Returns | |
|---|---|
| Weights values as a list of NumPy arrays. |
set_weights
set_weights(
weights
)
Sets the weights of the layer, from NumPy arrays.
The weights of a layer represent the state of the layer. This function sets the weight values from numpy arrays. The weight values should be passed in the order they are created by the layer. Note that the layer's weights must be instantiated before calling this function, by calling the layer.
For example, a Dense layer returns a list of two values: the kernel matrix
and the bias vector. These can be used to set the weights of another
Dense layer:
layer_a = tf.keras.layers.Dense(1,kernel_initializer=tf.constant_initializer(1.))a_out = layer_a(tf.convert_to_tensor([[1., 2., 3.]]))layer_a.get_weights()[array([[1.],[1.],[1.]], dtype=float32), array([0.], dtype=float32)]layer_b = tf.keras.layers.Dense(1,kernel_initializer=tf.constant_initializer(2.))b_out = layer_b(tf.convert_to_tensor([[10., 20., 30.]]))layer_b.get_weights()[array([[2.],[2.],[2.]], dtype=float32), array([0.], dtype=float32)]layer_b.set_weights(layer_a.get_weights())layer_b.get_weights()[array([[1.],[1.],[1.]], dtype=float32), array([0.], dtype=float32)]
| Args |
|---|
weights
get_weights).
| Raises |
|---|
ValueError
zero_state
zero_state(
batch_size, dtype
)
__call__
__call__(
*args, **kwargs
)
Wraps call, applying pre- and post-processing steps.
| Args |
|---|
*args
self.call.
**kwargs
self.call.
| Returns | |
|---|---|
| Output tensor(s). |
| Note | |
|---|---|
- The following optional keyword arguments are reserved for specific uses:
training: Boolean scalar tensor of Python boolean indicating whether thecallis meant for training or inference.mask: Boolean input mask.
- If the layer's
callmethod takes amaskargument (as some Keras layers do), its default value will be set to the mask generated forinputsby the previous layer (ifinputdid come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support. - If the layer is not built, the method will call
build.
| Raises |
|---|
ValueError
call method returns None (an invalid value).
RuntimeError
super().__init__() was not called in the constructor.
View source on GitHub