just your regular denselyconnected nn layer dense implements the operation output activationdotinput kernel bias where activation is the elementwise activation function passed as the activation argument kernel is a weights matrix the layer and bias is a bias vector the layer only applicable if usebias is true note if the input to the layer has a rank greater than 2 dense computes the dot product between the inputs and the kernel along the last axis of the inputs and axis 0 of the kernel using tf tensordot for example if input has dimensions batchsize d0 d1 then we create a kernel with shape d1 units and the kernel operates along axis 2 of the input on every subtensor of shape 1 1 d1 there are batchsize d0 such subtensors the output in this case will have shape batchsize d0 units 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 ax x usebias boolean whether the layer uses a bias vector kernelinitializer initializer for the kernel weights matrix biasinitializer initializer for the bias vector kernelregularizer regularizer function applied to the kernel weights matrix biasregularizer regularizer function applied to the bias vector activityregularizer regularizer function applied to the output of the layer its activation kernelconstraint constraint function applied to the kernel weights matrix biasconstraint constraint function applied to the bias vector input shape nd tensor with shape batchsize inputdim the most common situation would be a 2d input with shape batchsize inputdim output shape nd tensor with shape batchsize units for instance for a 2d input with shape batchsize inputdim the output would have shape batchsize units just your regular densely connected nn layer 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 the layer and bias is a bias vector the layer only applicable if use_bias is true note if the input to the layer has a rank greater than 2 dense computes the dot product between the inputs and the kernel along the last axis of the inputs and axis 0 of the kernel using tf tensordot for example if input has dimensions batch_size d0 d1 then we create a kernel with shape d1 units and the kernel operates along axis 2 of the input on every sub tensor of shape 1 1 d1 there are batch_size d0 such sub tensors the output in this case will have shape batch_size d0 units 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 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
from keras import activations
from keras import constraints
from keras import initializers
from keras import ops
from keras import regularizers
from keras.api_export import keras_export
from keras.layers.input_spec import InputSpec
from keras.layers.layer import Layer


@keras_export("keras.layers.Dense")
class Dense(Layer):
    

    def __init__(
        self,
        units,
        activation=None,
        use_bias=True,
        kernel_initializer="glorot_uniform",
        bias_initializer="zeros",
        kernel_regularizer=None,
        bias_regularizer=None,
        activity_regularizer=None,
        kernel_constraint=None,
        bias_constraint=None,
        **kwargs,
    ):
        super().__init__(activity_regularizer=activity_regularizer, **kwargs)
        self.units = units
        self.activation = activations.get(activation)
        self.use_bias = use_bias
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        self.input_spec = InputSpec(min_ndim=2)
        self.supports_masking = True

    def build(self, input_shape):
        input_dim = input_shape[-1]
        self.kernel = self.add_weight(
            name="kernel",
            shape=(input_dim, self.units),
            initializer=self.kernel_initializer,
            regularizer=self.kernel_regularizer,
        )
        if self.use_bias:
            self.bias = self.add_weight(
                name="bias",
                shape=(self.units,),
                initializer=self.bias_initializer,
                regularizer=self.bias_regularizer,
            )
        else:
            self.bias = None
        self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
        self.built = True

    def call(self, inputs):
        x = ops.matmul(inputs, self.kernel)
        if self.bias is not None:
            x = x + self.bias
        if self.activation is not None:
            x = self.activation(x)
        return x

    def compute_output_shape(self, input_shape):
        output_shape = list(input_shape)
        output_shape[-1] = self.units
        return tuple(output_shape)

    def get_config(self):
        base_config = super().get_config()
        config = {
            "units": self.units,
            "activation": activations.serialize(self.activation),
            "use_bias": self.use_bias,
            "kernel_initializer": initializers.serialize(
                self.kernel_initializer
            ),
            "bias_initializer": initializers.serialize(self.bias_initializer),
            "kernel_regularizer": regularizers.serialize(
                self.kernel_regularizer
            ),
            "bias_regularizer": regularizers.serialize(self.bias_regularizer),
            "kernel_constraint": constraints.serialize(self.kernel_constraint),
            "bias_constraint": constraints.serialize(self.bias_constraint),
        }
        return {**base_config, **config}