Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added doc and examples for tf.keras.losses.get #37538

Merged
merged 5 commits into from
Mar 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions tensorflow/python/keras/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1843,15 +1843,34 @@ def deserialize(name, custom_objects=None):

@keras_export('keras.losses.get')
def get(identifier):
"""Retrieves a Keras loss function.
"""Retrieves a Keras loss as a `function`/`Loss` class instance.

The `identifier` may be the string name of a loss function or `Loss` class.

>>> loss = tf.keras.losses.get("categorical_crossentropy")
ashutosh1919 marked this conversation as resolved.
Show resolved Hide resolved
>>> type(loss)
<class 'function'>
>>> loss = tf.keras.losses.get("CategoricalCrossentropy")
>>> type(loss)
<class '...tensorflow.python.keras.losses.CategoricalCrossentropy'>

You can also specify `config` of the loss to this function by passing dict
containing `class_name` and `config` as an identifier. Also note that the
`class_name` must map to a `Loss` class

>>> identifier = {"class_name": "CategoricalCrossentropy",
... "config": {"from_logits": True}}
>>> loss = tf.keras.losses.get(identifier)
>>> type(loss)
<class '...tensorflow.python.keras.losses.CategoricalCrossentropy'>

Arguments:
identifier: A loss identifier. One of None or string name of a loss
function/class or loss configuration dictionary or a loss function or
a loss class instance

Returns:
A Keras loss function/ `Loss` class instance.
A Keras loss as a `function`/ `Loss` class instance.

Raises:
ValueError: If `identifier` cannot be interpreted.
Expand Down