Dense
- class opennmt.layers.Dense(*args, **kwargs)[source]
Small
tf.keras.layers.Dense
extension to possibly reuse an existing weight matrix.Inherits from:
keras.src.layers.core.dense.Dense
- __init__(units, weight=None, transpose=False, **kwargs)[source]
Initializes the layer.
- Parameters
unit – Positive integer, dimensionality of the output space.
weight – The weight to reuse.
transpose – Whether
weight
should be transposed or not.kwargs – Additional layers arguments.
- set_kernel(weight, transpose=False)[source]
Use
weight
as the kernel weights matrix.- Parameters
weight – The weight to use.
transpose – Whether
weight
should be transposed or not.
- Raises
ValueError – if the layer is already built.
- add_weight(name, *args, **kwargs)[source]
Adds a new variable to the layer.
- Parameters
name – Variable name.
shape – Variable shape. Defaults to scalar if unspecified.
dtype – The type of the variable. Defaults to self.dtype.
initializer – Initializer instance (callable).
regularizer – Regularizer instance (callable).
trainable – Boolean, whether the variable should be part of the layer’s “trainable_variables” (e.g. variables, biases) or “non_trainable_variables” (e.g. BatchNorm mean and variance). Note that trainable cannot be True if synchronization is set to ON_READ.
constraint – Constraint instance (callable).
use_resource –
Whether to use a ResourceVariable or not. See [this guide]( https://www.tensorflow.org/guide/migrate/tf1_vs_tf2#resourcevariables_instead_of_referencevariables)
for more information.
synchronization – Indicates when a distributed a variable will be aggregated. Accepted values are constants defined in the class tf.VariableSynchronization. By default the synchronization is set to AUTO and the current DistributionStrategy chooses when to synchronize. If synchronization is set to ON_READ, trainable must not be set to True.
aggregation – Indicates how a distributed variable will be aggregated. Accepted values are constants defined in the class tf.VariableAggregation.
**kwargs – Additional keyword arguments. Accepted values are getter, collections, experimental_autocast and caching_device.
- Returns
The variable created.
- Raises
ValueError – When giving unsupported dtype and no initializer or when trainable has been set to True with synchronization set as ON_READ.
- call(inputs)[source]
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.