Dense implements the operation:
output = activation(dot(input, kernel) + bias)
where activation is the element-wise activation function
passed as the activation argument, kernel is a weights matrix
created by the layer, and bias is a bias vector created by the layer
(only applicable if use_bias is True). When this layer is
followed by a BatchNormalization layer, it is recommended to set
use_bias=False as BatchNormalization has its own bias term.
Args
units
Positive integer, dimensionality of the output space.
activation
Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: a(x) = x).
use_bias
Boolean, whether the layer uses a bias vector.
kernel_initializer
Initializer for the kernel weights matrix.
bias_initializer
Initializer for the bias vector.
kernel_regularizer
Regularizer function applied to
the kernel weights matrix.
bias_regularizer
Regularizer function applied to the bias vector.
activity_regularizer
Regularizer function applied to
the output of the layer (its "activation").
kernel_constraint
Constraint function applied to
the kernel weights matrix.
bias_constraint
Constraint function applied to the bias vector.
lora_rank
Optional integer. If set, the layer's forward pass
will implement LoRA (Low-Rank Adaptation)
with the provided rank. LoRA sets the layer's kernel
to non-trainable and replaces it with a delta over the
original kernel, obtained via multiplying two lower-rank
trainable matrices. This can be useful to reduce the
computation cost of fine-tuning large dense layers.
You can also enable LoRA on an existing
Dense layer by calling layer.enable_lora(rank).
lora_alpha
Optional integer. If set, this parameter scales the
low-rank adaptation delta (computed as the product of two lower-rank
trainable matrices) during the forward pass. The delta is scaled by
lora_alpha / lora_rank, allowing you to fine-tune the strength of
the LoRA adjustment independently of lora_rank.
Input shape
N-D tensor with shape: (batch_size, ..., input_dim).
The most common situation would be
a 2D input with shape (batch_size, input_dim).
Output shape
N-D tensor with shape: (batch_size, ..., units).
For instance, for a 2D input with shape (batch_size, input_dim),
the output would have shape (batch_size, units).
Attributes
input
Retrieves the input tensor(s) of a symbolic operation.
Only returns the tensor(s) corresponding to the first time
the operation was called.
Note that for a layer called more than once (a shared layer),
this property returns the inputs of the first call only, even
though the layer has several inputs. There is no public accessor
for the inputs of the other calls. Keep a reference to the
inputs used in each call instead.
kernel
output
Retrieves the output tensor(s) of a layer.
Only returns the tensor(s) corresponding to the first time
the operation was called.
Note that for a layer called more than once (a shared layer),
this property returns the output of the first call only, silently
ignoring the later calls. There is no public accessor for the
outputs of the other calls.
To access the output of a specific call, keep a reference to the
tensor returned by that call:
inputs_a=keras.Input(shape=(3,))inputs_b=keras.Input(shape=(3,))shared=keras.layers.Dense(2)outputs_a=shared(inputs_a)# First call.outputs_b=shared(inputs_b)# Second call.# `shared.output` is `outputs_a`, the first call's output, not# `outputs_b`. Use the returned tensors directly instead.model=keras.Model([inputs_a,inputs_b],[outputs_a,outputs_b])