initializer that generates tensors with constant values only scalar values are allowed the constant value provided must be convertible to the dtype requested when calling the initializer examples standalone usage initializer constant10 values initializershape2 2 usage in a keras layer initializer constant10 layer dense3 kernelinitializerinitializer args value a python scalar initializer that generates tensors initialized to 0 examples standalone usage initializer zeros values initializershape2 2 usage in a keras layer initializer zeros layer denseunits3 kernelinitializerinitializer returns a tensor object initialized as specified by the initializer args shape shape of the tensor dtype optional dtype of the tensor only numeric or boolean dtypes are supported if not specified keras backend floatx is used which default to float32 unless you configured it otherwise via keras backend setfloatxfloatdtype initializer that generates tensors initialized to 1 also available via the shortcut function ones examples standalone usage initializer ones values initializershape2 2 usage in a keras layer initializer ones layer dense3 kernelinitializerinitializer returns a tensor object initialized as specified by the initializer args shape shape of the tensor dtype optional dtype of the tensor only numeric or boolean dtypes are supported if not specified keras backend floatx is used which default to float32 unless you configured it otherwise via keras backend setfloatxfloatdtype initializer that generates the identity matrix only usable for generating 2d matrices examples standalone usage initializer identity values initializershape2 2 usage in a keras layer initializer identity layer dense3 kernelinitializerinitializer args gain multiplicative factor to apply to the identity matrix returns a tensor object initialized as specified by the initializer args shape shape of the tensor dtype optional dtype of the tensor only numeric or boolean dtypes are supported if not specified keras backend floatx is used which default to float32 unless you configured it otherwise via keras backend setfloatxfloatdtype initializer that generates tensors with constant values only scalar values are allowed the constant value provided must be convertible to the dtype requested when calling the initializer examples standalone usage initializer constant 10 values initializer shape 2 2 usage in a keras layer initializer constant 10 layer dense 3 kernel_initializer initializer args value a python scalar initializer that generates tensors initialized to 0 examples standalone usage initializer zeros values initializer shape 2 2 usage in a keras layer initializer zeros layer dense units 3 kernel_initializer initializer returns a tensor object initialized as specified by the initializer args shape shape of the tensor dtype optional dtype of the tensor only numeric or boolean dtypes are supported if not specified keras backend floatx is used which default to float32 unless you configured it otherwise via keras backend set_floatx float_dtype initializer that generates tensors initialized to 1 also available via the shortcut function ones examples standalone usage initializer ones values initializer shape 2 2 usage in a keras layer initializer ones layer dense 3 kernel_initializer initializer returns a tensor object initialized as specified by the initializer args shape shape of the tensor dtype optional dtype of the tensor only numeric or boolean dtypes are supported if not specified keras backend floatx is used which default to float32 unless you configured it otherwise via keras backend set_floatx float_dtype initializer that generates the identity matrix only usable for generating 2d matrices examples standalone usage initializer identity values initializer shape 2 2 usage in a keras layer initializer identity layer dense 3 kernel_initializer initializer args gain multiplicative factor to apply to the identity matrix returns a tensor object initialized as specified by the initializer args shape shape of the tensor dtype optional dtype of the tensor only numeric or boolean dtypes are supported if not specified keras backend floatx is used which default to float32 unless you configured it otherwise via keras backend set_floatx float_dtype
from keras import ops
from keras.api_export import keras_export
from keras.backend import standardize_dtype
from keras.initializers.initializer import Initializer
from keras.saving import serialization_lib


@keras_export(["keras.initializers.Constant", "keras.initializers.constant"])
class Constant(Initializer):
    

    def __init__(self, value=0.0):
        self.value = value

    def __call__(self, shape, dtype=None):
        dtype = standardize_dtype(dtype)
        return ops.cast(self.value, dtype=dtype) * ops.ones(
            shape=shape, dtype=dtype
        )

    def get_config(self):
        return {"value": serialization_lib.serialize_keras_object(self.value)}

    @classmethod
    def from_config(cls, config):
        value = serialization_lib.deserialize_keras_object(config["value"])
        return cls(value)


@keras_export(["keras.initializers.Zeros", "keras.initializers.zeros"])
class Zeros(Initializer):
    

    def __call__(self, shape, dtype=None):
        
        dtype = standardize_dtype(dtype)
        return ops.zeros(shape, dtype=dtype)


@keras_export(["keras.initializers.Ones", "keras.initializers.ones"])
class Ones(Initializer):
    

    def __call__(self, shape, dtype=None):
        
        dtype = standardize_dtype(dtype)
        return ops.ones(shape, dtype=dtype)


@keras_export(
    [
        "keras.initializers.IdentityInitializer",
        "keras.initializers.Identity",
        "keras.initializers.identity",
    ]
)
class Identity(Initializer):
    

    def __init__(self, gain=1.0):
        self.gain = gain

    def __call__(self, shape, dtype=None):
        
        if len(shape) != 2:
            raise ValueError(
                "Identity matrix initializer can only be used for 2D matrices. "
                f"Received: shape={shape} of rank {len(shape)}."
            )
        dtype = standardize_dtype(dtype)
        return self.gain * ops.eye(*shape, dtype=dtype)