tf.keras.regularizers.L2
Stay organized with collections
Save and categorize content based on your preferences.
A regularizer that applies a L2 regularization penalty.
Inherits From: Regularizer
tf.keras.regularizers.L2(
l2=0.01
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
The L2 regularization penalty is computed as:
loss = l2 * reduce_sum(square(x))
L2 may be passed to a layer as a string identifier:
dense = Dense(3, kernel_regularizer='l2')
In this case, the default value used is l2=0.01
.
Arguments |
l2
|
float, L2 regularization factor.
|
Methods
from_config
View source
@classmethod
from_config(
config
)
Creates a regularizer from its config.
This method is the reverse of get_config
,
capable of instantiating the same regularizer from the config
dictionary.
This method is used by Keras model_to_estimator
, saving and
loading models to HDF5 formats, Keras model cloning, some visualization
utilities, and exporting models to and from JSON.
Args |
config
|
A Python dictionary, typically the output of get_config.
|
Returns |
A regularizer instance.
|
get_config
View source
get_config()
Returns the config of the regularizer.
An regularizer config is a Python dictionary (serializable)
containing all configuration parameters of the regularizer.
The same regularizer can be reinstantiated later
(without any saved state) from this configuration.
This method is optional if you are just training and executing models,
exporting to and from SavedModels, or using weight checkpoints.
This method is required for Keras model_to_estimator
, saving and
loading models to HDF5 formats, Keras model cloning, some visualization
utilities, and exporting models to and from JSON.
Returns |
Python dictionary.
|
__call__
View source
__call__(
x
)
Compute a regularization penalty from an input 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.regularizers.L2\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/regularizers/regularizers.py#L242-L268) |\n\nA regularizer that applies a L2 regularization penalty.\n\nInherits From: [`Regularizer`](../../../tf/keras/Regularizer)\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.regularizers.l2`](https://www.tensorflow.org/api_docs/python/tf/keras/regularizers/L2)\n\n\u003cbr /\u003e\n\n tf.keras.regularizers.L2(\n l2=0.01\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Distributed training with TensorFlow](https://www.tensorflow.org/guide/distributed_training) - [Effective Tensorflow 2](https://www.tensorflow.org/guide/effective_tf2) - [Use TF1.x models in TF2 workflows](https://www.tensorflow.org/guide/migrate/model_mapping) | - [Overfit and underfit](https://www.tensorflow.org/tutorials/keras/overfit_and_underfit) - [Custom training with tf.distribute.Strategy](https://www.tensorflow.org/tutorials/distribute/custom_training) - [Custom training loop with Keras and MultiWorkerMirroredStrategy](https://www.tensorflow.org/tutorials/distribute/multi_worker_with_ctl) - [Parameter server training with ParameterServerStrategy](https://www.tensorflow.org/tutorials/distribute/parameter_server_training) - [Retraining an Image Classifier](https://www.tensorflow.org/hub/tutorials/tf2_image_retraining) |\n\nThe L2 regularization penalty is computed as:\n`loss = l2 * reduce_sum(square(x))`\n\nL2 may be passed to a layer as a string identifier: \n\n dense = Dense(3, kernel_regularizer='l2')\n\nIn this case, the default value used is `l2=0.01`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|------|----------------------------------|\n| `l2` | float, L2 regularization factor. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/regularizers/regularizers.py#L127-L145) \n\n @classmethod\n from_config(\n config\n )\n\nCreates a regularizer from its config.\n\nThis method is the reverse of `get_config`,\ncapable of instantiating the same regularizer from the config\ndictionary.\n\nThis method is used by Keras `model_to_estimator`, saving and\nloading models to HDF5 formats, Keras model cloning, some visualization\nutilities, and exporting models to and from JSON.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|----------------------------------------------------------|\n| `config` | A Python dictionary, typically 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| A regularizer 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/regularizers/regularizers.py#L267-L268) \n\n get_config()\n\nReturns the config of the regularizer.\n\nAn regularizer config is a Python dictionary (serializable)\ncontaining all configuration parameters of the regularizer.\nThe same regularizer can be reinstantiated later\n(without any saved state) from this configuration.\n\nThis method is optional if you are just training and executing models,\nexporting to and from SavedModels, or using weight checkpoints.\n\nThis method is required for Keras `model_to_estimator`, saving and\nloading models to HDF5 formats, Keras model cloning, some visualization\nutilities, and exporting models to and from JSON.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| Python dictionary. ||\n\n\u003cbr /\u003e\n\n### `__call__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/regularizers/regularizers.py#L264-L265) \n\n __call__(\n x\n )\n\nCompute a regularization penalty from an input tensor."]]