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

load_model_tf with custom loss require the dict() function #1167

Closed
Cdk29 opened this issue Jan 31, 2021 · 0 comments
Closed

load_model_tf with custom loss require the dict() function #1167

Cdk29 opened this issue Jan 31, 2021 · 0 comments

Comments

@Cdk29
Copy link
Contributor

Cdk29 commented Jan 31, 2021

I put this here if someone else run into the trouble, it is more problem of documentation.

I created a model with a custom loss function :

smoothie <- function(y_true,y_pred){
    tf$losses$categorical_crossentropy(y_true, y_pred, label_smoothing=0.3)
}

Which make me think that the categorical_crossentropy function of the package keras could be updated too, to reflect the one of the python version of keras, which can use label_smoothing.

Running the following would create an error :

model <- load_model_tf("../input/efficientnetb7-label-smoothing-imagenet/EfficientB7_image_net/")

ValueError: ('Unknown loss function', ':smoothie')

And this is when things began to be puzzling. Looking at keras-team/keras#5916 in the main keras, it appeared to me that I must use custom_objects. But the documentation is totally clueless about how and using standard R data-structure did not worked (I tried a list without success). Finally looking at the solution provided on github, and the code of the function of the github repository, I remembered that there is some conversion to the dictionary format via reticulate, and indeed there is a function to do it. Using :

model <- load_model_tf("../input/efficientnetb7-label-smoothing-imagenet/EfficientB7_image_net/", custom_objects = dict(smoothie = "smoothie"))

Worked perfectly.

I would suggest an update to the documentation or a way to pass a list as an argument, which the first logical attempt of a R user.

This was referenced Jan 31, 2021
@Cdk29 Cdk29 closed this as completed Feb 2, 2021
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

No branches or pull requests

1 participant