Module riid.models.layers
This module contains custom Keras layers.
Expand source code Browse git
# Copyright 2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS,
# the U.S. Government retains certain rights in this software.
"""This module contains custom Keras layers."""
import tensorflow as tf
from keras.api.layers import Layer
class ReduceSumLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x, axis):
return tf.reduce_sum(x, axis=axis)
class ReduceMaxLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x):
return tf.reduce_max(x)
class DivideLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x):
return tf.divide(x[0], x[1])
class ExpandDimsLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x, axis):
return tf.expand_dims(x, axis=axis)
class ClipByValueLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x, clip_value_min, clip_value_max):
return tf.clip_by_value(x, clip_value_min=clip_value_min, clip_value_max=clip_value_max)
class PoissonLogProbabilityLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x):
exp, value = x
log_probas = tf.math.xlogy(value, exp) - exp - tf.math.lgamma(value + 1)
return log_probas
class SeedLayer(Layer):
def __init__(self, seeds, **kwargs):
super(SeedLayer, self).__init__(**kwargs)
self.seeds = tf.convert_to_tensor(seeds)
def get_config(self):
config = super().get_config()
config.update({
"seeds": self.seeds.numpy().tolist(),
})
return config
def call(self, inputs):
return self.seeds
class L1NormLayer(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, inputs):
sums = tf.reduce_sum(inputs, axis=-1)
l1_norm = inputs / tf.reshape(sums, (-1, 1))
return l1_norm
Classes
class ClipByValueLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class ClipByValueLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, x, clip_value_min, clip_value_max): return tf.clip_by_value(x, clip_value_min=clip_value_min, clip_value_max=clip_value_max)Ancestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, x, clip_value_min, clip_value_max)-
Expand source code Browse git
def call(self, x, clip_value_min, clip_value_max): return tf.clip_by_value(x, clip_value_min=clip_value_min, clip_value_max=clip_value_max)
- in
class DivideLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class DivideLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, x): return tf.divide(x[0], x[1])Ancestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, x)-
Expand source code Browse git
def call(self, x): return tf.divide(x[0], x[1])
- in
class ExpandDimsLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class ExpandDimsLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, x, axis): return tf.expand_dims(x, axis=axis)Ancestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, x, axis)-
Expand source code Browse git
def call(self, x, axis): return tf.expand_dims(x, axis=axis)
- in
class L1NormLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class L1NormLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, inputs): sums = tf.reduce_sum(inputs, axis=-1) l1_norm = inputs / tf.reshape(sums, (-1, 1)) return l1_normAncestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, inputs)-
Expand source code Browse git
def call(self, inputs): sums = tf.reduce_sum(inputs, axis=-1) l1_norm = inputs / tf.reshape(sums, (-1, 1)) return l1_norm
- in
class PoissonLogProbabilityLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class PoissonLogProbabilityLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, x): exp, value = x log_probas = tf.math.xlogy(value, exp) - exp - tf.math.lgamma(value + 1) return log_probasAncestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, x)-
Expand source code Browse git
def call(self, x): exp, value = x log_probas = tf.math.xlogy(value, exp) - exp - tf.math.lgamma(value + 1) return log_probas
- in
class ReduceMaxLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class ReduceMaxLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, x): return tf.reduce_max(x)Ancestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, x)-
Expand source code Browse git
def call(self, x): return tf.reduce_max(x)
- in
class ReduceSumLayer (**kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class ReduceSumLayer(Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, x, axis): return tf.reduce_sum(x, axis=axis)Ancestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, x, axis)-
Expand source code Browse git
def call(self, x, axis): return tf.reduce_sum(x, axis=axis)
- in
class SeedLayer (seeds, **kwargs)-
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the
call()method, and a state (weight variables). State can be created:- in
__init__(), for instance viaself.add_weight(); - in the optional
build()method, which is invoked by the first__call__()to the layer, and supplies the shape(s) of the input(s), which may not have been known at initialization time.
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the
__init__()method orbuild()method.Users will just instantiate a layer and then treat it as a callable.
Args
trainable- Boolean, whether the layer's variables should be trainable.
name- String name of the layer.
dtype- The dtype of the layer's computations and weights. Can also be a
keras.DTypePolicy, which allows the computation and weight dtype to differ. Defaults toNone.Nonemeans to usekeras.config.dtype_policy(), which is afloat32policy unless set to different value (viakeras.config.set_dtype_policy()).
Attributes
name- The name of the layer (string).
dtype- Dtype of the layer's weights. Alias of
layer.variable_dtype. variable_dtype- Dtype of the layer's weights.
compute_dtype- The dtype of the layer's computations.
Layers automatically cast inputs to this dtype, which causes
the computations and output to also be in this dtype.
When mixed precision is used with a
keras.DTypePolicy, this will be different thanvariable_dtype. trainable_weights- List of variables to be included in backprop.
non_trainable_weights- List of variables that should not be included in backprop.
weights- The concatenation of the lists trainable_weights and non_trainable_weights (in this order).
trainable- Whether the layer should be trained (boolean), i.e.
whether its potentially-trainable weights should be returned
as part of
layer.trainable_weights. input_spec- Optional (list of)
InputSpecobject(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of
Layerimplement the following methods:__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, usingadd_weight(), or other state.build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), usingadd_weight(), or other state.__call__()will automatically build the layer (if it has not been built yet) by callingbuild().call(self, *args, **kwargs): Called in__call__after making surebuild()has been called.call()performs the logic of applying the layer to the input arguments. Two reserved keyword arguments you can optionally use incall()are: 1.training(boolean, whether the call is in inference mode or training mode). 2.mask(boolean tensor encoding masked timesteps in the input, used e.g. in RNN layers). A typical signature for this method iscall(self, inputs), and user could optionally addtrainingandmaskif the layer need them.get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in__init__(), then overridefrom_config(self)as well. This method is used when saving the layer or a model that contains this layer.
Examples:
Here's a basic example: a layer with two variables,
wandb, that returnsy = w . x + b. It shows how to implementbuild()andcall(). Variables set as attributes of a layer are tracked as weights of the layers (inlayer.weights).class SimpleDense(Layer): def __init__(self, units=32): super().__init__() self.units = units # Create the state of the layer (weights) def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer="glorot_uniform", trainable=True, name="kernel", ) self.bias = self.add_weight( shape=(self.units,), initializer="zeros", trainable=True, name="bias", ) # Defines the computation def call(self, inputs): return ops.matmul(inputs, self.kernel) + self.bias # Instantiates the layer. linear_layer = SimpleDense(4) # This will also call `build(input_shape)` and create the weights. y = linear_layer(ops.ones((2, 2))) assert len(linear_layer.weights) == 2 # These weights are trainable, so they're listed in `trainable_weights`: assert len(linear_layer.trainable_weights) == 2Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during
call(). Here's a example layer that computes the running sum of its inputs:class ComputeSum(Layer): def __init__(self, input_dim): super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = self.add_weight( shape=(), initializer="zeros", trainable=False, name="total", ) def call(self, inputs): self.total.assign(self.total + ops.sum(inputs)) return self.total my_sum = ComputeSum(2) x = ops.ones((2, 2)) y = my_sum(x) assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == []Expand source code Browse git
class SeedLayer(Layer): def __init__(self, seeds, **kwargs): super(SeedLayer, self).__init__(**kwargs) self.seeds = tf.convert_to_tensor(seeds) def get_config(self): config = super().get_config() config.update({ "seeds": self.seeds.numpy().tolist(), }) return config def call(self, inputs): return self.seedsAncestors
- keras.src.layers.layer.Layer
- keras.src.backend.tensorflow.layer.TFLayer
- keras.src.backend.tensorflow.trackable.KerasAutoTrackable
- tensorflow.python.trackable.autotrackable.AutoTrackable
- tensorflow.python.trackable.base.Trackable
- keras.src.ops.operation.Operation
- keras.src.saving.keras_saveable.KerasSaveable
Methods
def call(self, inputs)-
Expand source code Browse git
def call(self, inputs): return self.seeds def get_config(self)-
Returns the config of the object.
An object config is a Python dictionary (serializable) containing the information needed to re-instantiate it.
Expand source code Browse git
def get_config(self): config = super().get_config() config.update({ "seeds": self.seeds.numpy().tolist(), }) return config
- in