tf.keras.Initializer
Stay organized with collections
Save and categorize content based on your preferences.
Initializer base class: all Keras initializers inherit from this class.
Initializers should implement a __call__()
method with the following
signature:
def __call__(self, shape, dtype=None, **kwargs):
# returns a tensor of shape `shape` and dtype `dtype`
# containing values drawn from a distribution of your choice.
Optionally, you an 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, **kwargs):
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
clone()
from_config
View source
@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)
Args |
config
|
A Python dictionary, the output of get_config() .
|
Returns |
An Initializer instance.
|
get_config
View source
get_config()
Returns the initializer's configuration as a JSON-serializable dict.
Returns |
A JSON-serializable Python dict.
|
__call__
View source
__call__(
shape, dtype=None
)
Returns a tensor object initialized as specified by the initializer.
Args |
shape
|
Shape of the tensor.
|
dtype
|
Optional dtype of the tensor.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.Initializer\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/initializers/initializer.py#L4-L84) |\n\nInitializer base class: all Keras initializers inherit from this class.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.initializers.Initializer`](https://www.tensorflow.org/api_docs/python/tf/keras/Initializer)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.Initializer`](https://www.tensorflow.org/api_docs/python/tf/keras/Initializer)\n\n\u003cbr /\u003e\n\nInitializers should implement a `__call__()` method with the following\nsignature: \n\n def __call__(self, shape, dtype=None, **kwargs):\n # returns a tensor of shape `shape` and dtype `dtype`\n # containing values drawn from a distribution of your choice.\n\nOptionally, you an also implement the method `get_config()` and the class\nmethod `from_config` in order to support serialization -- just like with\nany Keras object.\n\nHere's a simple example: a random normal initializer. \n\n class ExampleRandomNormal(Initializer):\n def __init__(self, mean, stddev):\n self.mean = mean\n self.stddev = stddev\n\n def __call__(self, shape, dtype=None, **kwargs):\n return keras.random.normal(\n shape, mean=self.mean, stddev=self.stddev, dtype=dtype\n )\n\n def get_config(self): # To support serialization\n return {\"mean\": self.mean, \"stddev\": self.stddev}\n\nNote that we don't have to implement `from_config()` in the example above\nsince the constructor arguments of the class the keys in the config returned\nby `get_config()` are the same. In this case, the default `from_config()`\nworks fine.\n\nMethods\n-------\n\n### `clone`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/initializers/initializer.py#L83-L84) \n\n clone()\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/initializers/initializer.py#L63-L81) \n\n @classmethod\n from_config(\n config\n )\n\nInstantiates an initializer from a configuration dictionary.\n\n#### Example:\n\n initializer = RandomUniform(-1, 1)\n config = initializer.get_config()\n initializer = RandomUniform.from_config(config)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|----------------------------------------------------|\n| `config` | A Python dictionary, the output of `get_config()`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| An `Initializer` instance. ||\n\n\u003cbr /\u003e\n\n### `get_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/initializers/initializer.py#L55-L61) \n\n get_config()\n\nReturns the initializer's configuration as a JSON-serializable dict.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A JSON-serializable Python dict. ||\n\n\u003cbr /\u003e\n\n### `__call__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/initializers/initializer.py#L44-L53) \n\n __call__(\n shape, dtype=None\n )\n\nReturns a tensor object initialized as specified by the initializer.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|---------|-------------------------------|\n| `shape` | Shape of the tensor. |\n| `dtype` | Optional dtype of the tensor. |\n\n\u003cbr /\u003e"]]