<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/initializers/initializer.py#L4-L97) |

Initializer base class: all Keras initializers inherit from this class.

#### View aliases


**Main aliases**

[`tf.keras.initializers.Initializer`](https://www.tensorflow.org/api_docs/python/tf/keras/Initializer)
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.keras.Initializer`](https://www.tensorflow.org/api_docs/python/tf/keras/Initializer)

<br />

Initializers should implement a `__call__()` method with one of two possible
signatures. If your initializer does not handle distribution layouts, it
should use this signature:

    def __call__(self, shape, dtype=None):
        # returns a tensor of shape `shape` and dtype `dtype`
        # containing values drawn using a function of your choice.

If your initializer handles distribution layouts, it should use this
signature. `layout` can either be a [`keras.distribution.TensorLayout`](https://www.tensorflow.org/api_docs/python/tf/keras/distribution/TensorLayout) or a
backend-specific layout. If the given `layout` is not `None`, the returned
value must be distributed according to `layout`.

    def __call__(self, shape, dtype=None, layout=None):
        # returns a tensor of shape `shape`, dtype `dtype` and distributed
        # across devices according to the layout `layout`.
        # containing values drawn using a function of your choice.

Optionally, you can also implement the method `get_config()` and the class
method `from_config` in order to support serialization, just like with
any Keras object.

Here's a simple example: a random normal initializer.

    class ExampleRandomNormal(Initializer):
        def __init__(self, mean, stddev):
            self.mean = mean
            self.stddev = stddev

        def __call__(self, shape, dtype=None):
            return keras.random.normal(
                shape, mean=self.mean, stddev=self.stddev, dtype=dtype
            )

        def get_config(self):  # To support serialization
            return {"mean": self.mean, "stddev": self.stddev}

Note that we don't have to implement `from_config()` in the example above
since the constructor arguments of the class the keys in the config returned
by `get_config()` are the same. In this case, the default `from_config()`
works fine.

## Methods

### `clone`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/initializers/initializer.py#L96-L97)

    clone()

### `from_config`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/initializers/initializer.py#L76-L94)

    @classmethod
    from_config(
        config
    )

Instantiates an initializer from a configuration dictionary.

#### Example:

    initializer = RandomUniform(-1, 1)
    config = initializer.get_config()
    initializer = RandomUniform.from_config(config)

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | A Python dictionary, the output of `get_config()`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An `Initializer` instance. ||

<br />

### `get_config`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/initializers/initializer.py#L68-L74)

    get_config()

Returns the initializer's configuration as a JSON-serializable dict.

<br />

<br />

<br />

| Returns ||
|---|---|
| A JSON-serializable Python dict. ||

<br />

### `__call__`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/initializers/initializer.py#L57-L66)

    __call__(
        shape, dtype=None
    )

Returns a tensor object initialized as specified by the initializer.

<br />

<br />

<br />

| Args ||
|---|---|
| `shape` | Shape of the tensor. |
| `dtype` | Optional dtype of the tensor. |

<br />