rannet.layers¶
Module Contents¶
Classes¶
Turns positive integers (indexes) into dense vectors of fixed size. |
|
Turns dense vectors into probabilities. |
|
This is the class from which all layers inherit. |
|
This is the class from which all layers inherit. |
|
This is the class from which all layers inherit. |
Functions¶
|
Attributes¶
- rannet.layers.swish(x: langml.tensor_typing.Tensors) langml.tensor_typing.Tensors¶
- class rannet.layers.AdaptiveEmbedding(input_dim, output_dim, embed_dim=None, cutoffs=None, div_val=1, force_projection=None, embeddings_initializer='uniform', embeddings_regularizer=None, embeddings_constraint=None, kernel_initializer='glorot_uniform', kernel_regularizer=None, kernel_constraint=None, mask_zero=False, return_embeddings=False, return_projections=False, **kwargs)¶
Bases:
langml.L.LayerTurns positive integers (indexes) into dense vectors of fixed size. # Arguments
input_dim: int > 0. Size of the vocabulary. output_dim: int > 0. Dimension of the dense embedding after projection if it is not equal to embed_dim. embed_dim: int > 0. Dimension of the dense embedding. cutoffs: list of ints. Indices of splitting points. div_val: int >= 0. The scaling parameter of embedding. force_projection: Boolean. Add projection even if output_dim equals to embed_dim. embeddings_initializer: Initializer for the embeddings matrix. embeddings_regularizer: Regularizer function applied to the embeddings matrix. embeddings_constraint: Constraint function applied to the embeddings matrix. mask_zero: Whether or not the input value 0 is a special “padding”
value that should be masked out. This is useful when using [recurrent layers](recurrent.md) which may take variable length input. If this is True then all subsequent layers in the model need to support masking or an exception will be raised. If mask_zero is set to True, as a consequence, index 0 cannot be used in the vocabulary (input_dim should equal size of vocabulary + 1).
- # Input shape
2D tensor with shape: (batch_size, sequence_length).
- # Output shape
3D tensor with shape: (batch_size, sequence_length, output_dim).
- # References
[Efficient softmax approximation for GPUs](https://arxiv.org/pdf/1609.04309.pdf)
- build(input_shape)¶
Creates the variables of the layer (for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Parameters:
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
- compute_mask(inputs, mask=None)¶
Computes an output mask tensor.
- Parameters:
inputs – Tensor or list of tensors.
mask – Tensor or list of tensors.
- Returns:
- None or a tensor (or list of tensors,
one per output tensor of the layer).
- compute_output_shape(input_shape)¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters:
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- call(inputs, **kwargs)¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Parameters:
inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
**kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- get_config()¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- static get_custom_objects()¶
- class rannet.layers.AdaptiveSoftmax(input_dim, output_dim, embed_dim=None, cutoffs=None, div_val=1, use_bias=True, force_projection=None, embeddings_initializer='uniform', embeddings_regularizer=None, embeddings_constraint=None, kernel_initializer='glorot_uniform', kernel_regularizer=None, kernel_constraint=None, bias_initializer='zeros', bias_regularizer=None, bias_constraint=None, bind_embeddings=False, bind_projections=False, **kwargs)¶
Bases:
langml.L.LayerTurns dense vectors into probabilities. # Arguments
input_dim: int > 0. Dimension of input vectors. output_dim: int > 0. Number of output classes. embed_dim: int > 0. Dimension of the dense embedding. cutoffs: list of ints. Indices of splitting points. div_val: int >= 0. The scaling parameter of embedding. use_bias: Boolean. Whether to bias terms. force_projection: Boolean. Add projection even if output_dim equals to embed_dim. bind_embeddings: list of boolean. Whether to use the existed embeddings as mapping. bind_projections: list of boolean. Whether to use the existed projections as mapping.
- # Input shape
3D tensor with shape: (batch_size, sequence_length, input_dim).
- # Output shape
3D tensor with shape: (batch_size, sequence_length, output_dim).
- # References
[Efficient softmax approximation for GPUs](https://arxiv.org/pdf/1609.04309.pdf)
- build(input_shape)¶
Creates the variables of the layer (for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Parameters:
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
- compute_mask(inputs, mask=None)¶
Computes an output mask tensor.
- Parameters:
inputs – Tensor or list of tensors.
mask – Tensor or list of tensors.
- Returns:
- None or a tensor (or list of tensors,
one per output tensor of the layer).
- compute_output_shape(input_shape)¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters:
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- call(inputs, **kwargs)¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Parameters:
inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
**kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- get_config()¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns:
Python dictionary.
- static get_custom_objects()¶
- class rannet.layers.WithSparseCategoricalCrossEntropy(trainable=True, name=None, dtype=None, dynamic=False, **kwargs)¶
Bases:
langml.L.LayerThis 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 various places, at the convenience of the subclass implementer:
in __init__();
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;
in the first invocation of call(), with some caveats discussed below.
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.
Users will just instantiate a layer and then treat it as a callable.
- Parameters:
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 tf.keras.mixed_precision.Policy, which allows the computation and weight dtype to differ. Default of None means to use tf.keras.mixed_precision.global_policy(), which is a float32 policy unless set to different value.
dynamic – Set this to True if your layer should only be run eagerly, and should not be used to generate a static computation graph. This would be the case for a Tree-RNN or a recursive network, for example, or generally for any layer that manipulates tensors using Python control flow. If False, we assume that the layer can safely be used to generate a static computation graph.
- name¶
The name of the layer (string).
- dtype¶
The dtype of the layer’s weights.
- variable_dtype¶
Alias of dtype.
- 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 tf.keras.mixed_precision.Policy, this will be different than variable_dtype.
- dtype_policy¶
The layer’s dtype policy. See the tf.keras.mixed_precision.Policy documentation for details.
- 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) InputSpec object(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of Layer implement the following methods:
__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, using add_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), using add_weight(), or other state. __call__() will automatically build the layer (if it has not been built yet) by calling build().
call(self, inputs, *args, **kwargs): Called in __call__ after making sure build() has been called. call() performs the logic of applying the layer to the inputs. The first invocation may additionally create state that could not be conveniently created in build(); see its docstring for details. Two reserved keyword arguments you can optionally use in call() are:
training (boolean, whether the call is in inference mode or training mode). See more details in [the layer/model subclassing guide]( https://www.tensorflow.org/guide/keras/custom_layers_and_models#privileged_training_argument_in_the_call_method)
mask (boolean tensor encoding masked timesteps in the input, used in RNN layers). See more details in [the layer/model subclassing guide]( https://www.tensorflow.org/guide/keras/custom_layers_and_models#privileged_mask_argument_in_the_call_method)
A typical signature for this method is call(self, inputs), and user could optionally add training and mask if the layer need them. *args and **kwargs is only useful for future extension when more input parameters are planned to be added.
get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in __init__, then override from_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, w and b, that returns y = w . x + b. It shows how to implement build() and call(). Variables set as attributes of a layer are tracked as weights of the layers (in layer.weights).
```python class SimpleDense(Layer):
- def __init__(self, units=32):
super(SimpleDense, self).__init__() self.units = units
- def build(self, input_shape): # Create the state of the layer (weights)
w_init = tf.random_normal_initializer() self.w = tf.Variable(
- initial_value=w_init(shape=(input_shape[-1], self.units),
dtype=’float32’),
trainable=True)
b_init = tf.zeros_initializer() self.b = tf.Variable(
initial_value=b_init(shape=(self.units,), dtype=’float32’), trainable=True)
- def call(self, inputs): # Defines the computation from inputs to outputs
return tf.matmul(inputs, self.w) + self.b
# Instantiates the layer. linear_layer = SimpleDense(4)
# This will also call build(input_shape) and create the weights. y = linear_layer(tf.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) == 2 ```
Note that the method add_weight() offers a shortcut to create weights:
```python class SimpleDense(Layer):
- def __init__(self, units=32):
super(SimpleDense, self).__init__() self.units = units
- def build(self, input_shape):
- self.w = self.add_weight(shape=(input_shape[-1], self.units),
initializer=’random_normal’, trainable=True)
- self.b = self.add_weight(shape=(self.units,),
initializer=’random_normal’, trainable=True)
- def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
Besides 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:
```python class ComputeSum(Layer):
- def __init__(self, input_dim):
super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = tf.Variable(initial_value=tf.zeros((input_dim,)),
trainable=False)
- def call(self, inputs):
self.total.assign_add(tf.reduce_sum(inputs, axis=0)) return self.total
my_sum = ComputeSum(2) x = tf.ones((2, 2))
y = my_sum(x) print(y.numpy()) # [2. 2.]
y = my_sum(x) print(y.numpy()) # [4. 4.]
assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == [] ```
For more information about creating layers, see the guide [Making new Layers and Models via subclassing](
- call(inputs)¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Parameters:
inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
**kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_mask(inputs, mask=None)¶
Computes an output mask tensor.
- Parameters:
inputs – Tensor or list of tensors.
mask – Tensor or list of tensors.
- Returns:
- None or a tensor (or list of tensors,
one per output tensor of the layer).
- compute_output_shape(input_shape)¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters:
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- static get_custom_objects()¶
- class rannet.layers.WithSparseCategoricalAccuracy(trainable=True, name=None, dtype=None, dynamic=False, **kwargs)¶
Bases:
langml.L.LayerThis 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 various places, at the convenience of the subclass implementer:
in __init__();
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;
in the first invocation of call(), with some caveats discussed below.
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.
Users will just instantiate a layer and then treat it as a callable.
- Parameters:
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 tf.keras.mixed_precision.Policy, which allows the computation and weight dtype to differ. Default of None means to use tf.keras.mixed_precision.global_policy(), which is a float32 policy unless set to different value.
dynamic – Set this to True if your layer should only be run eagerly, and should not be used to generate a static computation graph. This would be the case for a Tree-RNN or a recursive network, for example, or generally for any layer that manipulates tensors using Python control flow. If False, we assume that the layer can safely be used to generate a static computation graph.
- name¶
The name of the layer (string).
- dtype¶
The dtype of the layer’s weights.
- variable_dtype¶
Alias of dtype.
- 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 tf.keras.mixed_precision.Policy, this will be different than variable_dtype.
- dtype_policy¶
The layer’s dtype policy. See the tf.keras.mixed_precision.Policy documentation for details.
- 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) InputSpec object(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of Layer implement the following methods:
__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, using add_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), using add_weight(), or other state. __call__() will automatically build the layer (if it has not been built yet) by calling build().
call(self, inputs, *args, **kwargs): Called in __call__ after making sure build() has been called. call() performs the logic of applying the layer to the inputs. The first invocation may additionally create state that could not be conveniently created in build(); see its docstring for details. Two reserved keyword arguments you can optionally use in call() are:
training (boolean, whether the call is in inference mode or training mode). See more details in [the layer/model subclassing guide]( https://www.tensorflow.org/guide/keras/custom_layers_and_models#privileged_training_argument_in_the_call_method)
mask (boolean tensor encoding masked timesteps in the input, used in RNN layers). See more details in [the layer/model subclassing guide]( https://www.tensorflow.org/guide/keras/custom_layers_and_models#privileged_mask_argument_in_the_call_method)
A typical signature for this method is call(self, inputs), and user could optionally add training and mask if the layer need them. *args and **kwargs is only useful for future extension when more input parameters are planned to be added.
get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in __init__, then override from_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, w and b, that returns y = w . x + b. It shows how to implement build() and call(). Variables set as attributes of a layer are tracked as weights of the layers (in layer.weights).
```python class SimpleDense(Layer):
- def __init__(self, units=32):
super(SimpleDense, self).__init__() self.units = units
- def build(self, input_shape): # Create the state of the layer (weights)
w_init = tf.random_normal_initializer() self.w = tf.Variable(
- initial_value=w_init(shape=(input_shape[-1], self.units),
dtype=’float32’),
trainable=True)
b_init = tf.zeros_initializer() self.b = tf.Variable(
initial_value=b_init(shape=(self.units,), dtype=’float32’), trainable=True)
- def call(self, inputs): # Defines the computation from inputs to outputs
return tf.matmul(inputs, self.w) + self.b
# Instantiates the layer. linear_layer = SimpleDense(4)
# This will also call build(input_shape) and create the weights. y = linear_layer(tf.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) == 2 ```
Note that the method add_weight() offers a shortcut to create weights:
```python class SimpleDense(Layer):
- def __init__(self, units=32):
super(SimpleDense, self).__init__() self.units = units
- def build(self, input_shape):
- self.w = self.add_weight(shape=(input_shape[-1], self.units),
initializer=’random_normal’, trainable=True)
- self.b = self.add_weight(shape=(self.units,),
initializer=’random_normal’, trainable=True)
- def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
Besides 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:
```python class ComputeSum(Layer):
- def __init__(self, input_dim):
super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = tf.Variable(initial_value=tf.zeros((input_dim,)),
trainable=False)
- def call(self, inputs):
self.total.assign_add(tf.reduce_sum(inputs, axis=0)) return self.total
my_sum = ComputeSum(2) x = tf.ones((2, 2))
y = my_sum(x) print(y.numpy()) # [2. 2.]
y = my_sum(x) print(y.numpy()) # [4. 4.]
assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == [] ```
For more information about creating layers, see the guide [Making new Layers and Models via subclassing](
- call(inputs)¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Parameters:
inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
**kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_mask(inputs, mask=None)¶
Computes an output mask tensor.
- Parameters:
inputs – Tensor or list of tensors.
mask – Tensor or list of tensors.
- Returns:
- None or a tensor (or list of tensors,
one per output tensor of the layer).
- compute_output_shape(input_shape)¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters:
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- static get_custom_objects()¶
- class rannet.layers.WithPerplexity(trainable=True, name=None, dtype=None, dynamic=False, **kwargs)¶
Bases:
langml.L.LayerThis 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 various places, at the convenience of the subclass implementer:
in __init__();
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;
in the first invocation of call(), with some caveats discussed below.
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.
Users will just instantiate a layer and then treat it as a callable.
- Parameters:
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 tf.keras.mixed_precision.Policy, which allows the computation and weight dtype to differ. Default of None means to use tf.keras.mixed_precision.global_policy(), which is a float32 policy unless set to different value.
dynamic – Set this to True if your layer should only be run eagerly, and should not be used to generate a static computation graph. This would be the case for a Tree-RNN or a recursive network, for example, or generally for any layer that manipulates tensors using Python control flow. If False, we assume that the layer can safely be used to generate a static computation graph.
- name¶
The name of the layer (string).
- dtype¶
The dtype of the layer’s weights.
- variable_dtype¶
Alias of dtype.
- 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 tf.keras.mixed_precision.Policy, this will be different than variable_dtype.
- dtype_policy¶
The layer’s dtype policy. See the tf.keras.mixed_precision.Policy documentation for details.
- 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) InputSpec object(s) specifying the constraints on inputs that can be accepted by the layer.
We recommend that descendants of Layer implement the following methods:
__init__(): Defines custom layer attributes, and creates layer weights that do not depend on input shapes, using add_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), using add_weight(), or other state. __call__() will automatically build the layer (if it has not been built yet) by calling build().
call(self, inputs, *args, **kwargs): Called in __call__ after making sure build() has been called. call() performs the logic of applying the layer to the inputs. The first invocation may additionally create state that could not be conveniently created in build(); see its docstring for details. Two reserved keyword arguments you can optionally use in call() are:
training (boolean, whether the call is in inference mode or training mode). See more details in [the layer/model subclassing guide]( https://www.tensorflow.org/guide/keras/custom_layers_and_models#privileged_training_argument_in_the_call_method)
mask (boolean tensor encoding masked timesteps in the input, used in RNN layers). See more details in [the layer/model subclassing guide]( https://www.tensorflow.org/guide/keras/custom_layers_and_models#privileged_mask_argument_in_the_call_method)
A typical signature for this method is call(self, inputs), and user could optionally add training and mask if the layer need them. *args and **kwargs is only useful for future extension when more input parameters are planned to be added.
get_config(self): Returns a dictionary containing the configuration used to initialize this layer. If the keys differ from the arguments in __init__, then override from_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, w and b, that returns y = w . x + b. It shows how to implement build() and call(). Variables set as attributes of a layer are tracked as weights of the layers (in layer.weights).
```python class SimpleDense(Layer):
- def __init__(self, units=32):
super(SimpleDense, self).__init__() self.units = units
- def build(self, input_shape): # Create the state of the layer (weights)
w_init = tf.random_normal_initializer() self.w = tf.Variable(
- initial_value=w_init(shape=(input_shape[-1], self.units),
dtype=’float32’),
trainable=True)
b_init = tf.zeros_initializer() self.b = tf.Variable(
initial_value=b_init(shape=(self.units,), dtype=’float32’), trainable=True)
- def call(self, inputs): # Defines the computation from inputs to outputs
return tf.matmul(inputs, self.w) + self.b
# Instantiates the layer. linear_layer = SimpleDense(4)
# This will also call build(input_shape) and create the weights. y = linear_layer(tf.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) == 2 ```
Note that the method add_weight() offers a shortcut to create weights:
```python class SimpleDense(Layer):
- def __init__(self, units=32):
super(SimpleDense, self).__init__() self.units = units
- def build(self, input_shape):
- self.w = self.add_weight(shape=(input_shape[-1], self.units),
initializer=’random_normal’, trainable=True)
- self.b = self.add_weight(shape=(self.units,),
initializer=’random_normal’, trainable=True)
- def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
Besides 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:
```python class ComputeSum(Layer):
- def __init__(self, input_dim):
super(ComputeSum, self).__init__() # Create a non-trainable weight. self.total = tf.Variable(initial_value=tf.zeros((input_dim,)),
trainable=False)
- def call(self, inputs):
self.total.assign_add(tf.reduce_sum(inputs, axis=0)) return self.total
my_sum = ComputeSum(2) x = tf.ones((2, 2))
y = my_sum(x) print(y.numpy()) # [2. 2.]
y = my_sum(x) print(y.numpy()) # [4. 4.]
assert my_sum.weights == [my_sum.total] assert my_sum.non_trainable_weights == [my_sum.total] assert my_sum.trainable_weights == [] ```
For more information about creating layers, see the guide [Making new Layers and Models via subclassing](
- call(inputs)¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Parameters:
inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
**kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns:
A tensor or list/tuple of tensors.
- compute_mask(inputs, mask=None)¶
Computes an output mask tensor.
- Parameters:
inputs – Tensor or list of tensors.
mask – Tensor or list of tensors.
- Returns:
- None or a tensor (or list of tensors,
one per output tensor of the layer).
- compute_output_shape(input_shape)¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters:
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns:
A tf.TensorShape instance or structure of tf.TensorShape instances.
- static get_custom_objects()¶
- rannet.layers.custom_objects¶