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

Custom loss #132

Merged
merged 9 commits into from
Mar 21, 2021
Merged

Custom loss #132

merged 9 commits into from
Mar 21, 2021

Conversation

turgut090
Copy link
Member

No description provided.

@turgut090 turgut090 linked an issue Mar 20, 2021 that may be closed by this pull request
@turgut090
Copy link
Member Author

turgut090 commented Mar 21, 2021

E.g.

my_loss = nn_loss(function(inputs, targets, alpha=0.8, gamma=2L, smooth=1L) {
  #comment out if your model contains a sigmoid or equivalent activation layer
  #inputs = Functional$sigmoid(inputs)       
  
  #flatten label and prediction tensors
  inputs = inputs$view(-1L)
  targets = targets$view(-1L)
  
  #first compute binary cross-entropy 
  BCE = Functional$binary_cross_entropy(inputs, targets, reduction='mean')
  BCE_EXP = torch$exp(-BCE)
  focal_loss = alpha * (1-BCE_EXP)^gamma * BCE
  focal_loss
},name = 'BCE')
learn = Learner(dls, model, loss_func=my_loss, metrics=accuracy())

based on https://www.kaggle.com/bigironsphere/loss-function-library-keras-pytorch

@turgut090 turgut090 merged commit 777726d into master Mar 21, 2021
@turgut090 turgut090 deleted the custom_loss branch October 2, 2023 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

customize a loss function (e.g., vgg16)
1 participant