question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom loss function NOT found

See original GitHub issue

I’ve defined several custom loss functions:


dice_loss = dice_loss(num_classes=args.num_classes,name='dice_loss')
tversky_loss = tversky_loss(beta=0.7,num_classes = args.num_classes,name='tversky_loss)

and my build model function is like this:

def build_model(hp):
        model = models.segmentationModel(output_channels=args.num_classes)
        model.build(input_shape=((1,256,256,3)))
        model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
                    loss = hp.Choice('loss', ['dice_loss', 'tversky_loss']),
                    metrics=[tf.keras.metrics.SparseCategoricalAccuracy(name='sparse_categorical_accuracy', dtype=None),UpdatedMeanIoU(name='Mean_IOU',num_classes=args.num_classes)])
        return model

When I execute this code, I get this error:

ValueError: Unknown loss function:dice_loss

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
mandar2812commented, Jul 17, 2020

@dhaneshr I think you might have to try something similar to what is mentioned here when loading custom objects.

Keras would not know from the strings dice_loss and tversky_loss that you mean your custom implementations.

Something like this might help:

def build_model(hp):
  custom_objects = {"tversky_loss": ..., "dice_loss": ...}
  with keras.utils.custom_object_scope(custom_objects):
      ## build model code here

1reaction
dhaneshrcommented, Jul 20, 2020

Hi again Mandar,

I ended up implementing the 2nd option which seems to be working so far. Here is my code:


    acc_metric = tf.keras.metrics.SparseCategoricalAccuracy(name='xent_acc', dtype=None)
    miou_metric = UpdatedMeanIoU(name='miou',num_classes=args.num_classes)
    

    def build_model(hp):
        model = models.keras_model(output_channels=args.num_classes,dropout_rate=hp.Choice('dropout_rate',[0.10,0.25,0.5,0.7]))
        model.build(input_shape=((1,256,256,3)))
        loss_hyp_param = hp.Choice('loss', ['dice_loss', 'tversky_loss']) 
        beta = hp.Choice('beta', [0.1,0.25,0.5,0.75])
        if loss_hyp_param == "dice_loss":
            loss_fn = dice_loss(num_classes=args.num_classes,name='dice_loss')
        else:
            loss_fn = tversky_loss(beta=beta,num_classes = args.num_classes,name='tversky_loss')

        model.compile(optimizer=hp.Choice('optimizer', ['Nadam','Adam']),
                    loss = loss_fn,
                    metrics=[miou_metric,acc_metric])
        return model


    hp = HyperParameters()

    tuner = RandomSearch(
        build_model,
        objective = kerastuner.Objective('val_miou', direction="max"),
        max_trials=60,
        executions_per_trial = 1,
        overwrite=True,
        directory='test_dir')
   
    tuner.search_space_summary()

    tuner.search(train_dataset,
                epochs=25,
                validation_data=val_dataset)
    tuner.results_summary()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple Enclosed Custom loss function not working
I have worked with custom functions in the past and just today this shocked me. Why isn't it working? how can I make...
Read more >
Custom loss function not working properly - TensorFlow Forum
I am training a convolutional neural network which predicts one label. I want to ignore loss resulted from some predictions during training.
Read more >
How To Build Custom Loss Functions In Keras For Any Use ...
This means that the custom loss function you designed, is not suitable for the dataset, and the business problem you are trying to...
Read more >
How to create a custom loss function in Keras | by Dhiraj K
Learn how to define and implement a custom loss function for training a machine learning model in ... Don't worry about missing out...
Read more >
Custom Loss Function in TensorFlow | by Marco Sanguineti
The choice of an objective function is not arbitrary and can sometimes ... We can find this loss function pre-implemented (tf.keras.losses.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found