-
Notifications
You must be signed in to change notification settings - Fork 282
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
new_loss_class
- ValueError: Invalid Reduction Key when calling loss as a function
#1342
Comments
Thank you very much for the thoughtful issue! I see that there is indeed a difference in the way built-in Loss functions in the package behave from the objects returned by The difficulty is that the
Attempting to patch a supplied I see two possible interfaces to enable this: we could ask users to supply a separate pure-function method, maybe like this: new_loss_class(...,
call = function(y_true, y_pred) { self$ ... },
call_fn = function(y_true, y_pred, extra_arg1, extra_arg2) { ... }
) But this has it's downsides too (I think having Perhaps, the best approach is to create a new function in Keras, as_loss_class(function(y_true, y_pred, extra_arg, ...) { ... }, name = "loss_name") This would be analogous to the This also has it's downsides because it grows the already large namespace, and having Note that today, you can pass a custom R function to A similar situation exists with the metrics, and you'll note that for backwards compatibility we preserved the state-less pure-function mode for the metrics that started out that way, but the more recently added metrics now only return a Metric instance, and cannot be used as pure functions directly. I'm eager to hear about your use-case. Is this because you want to export a custom loss function from the |
Thank you for the clarification 😃 Yes, I'd like to use it in the keras.tft. Once I created loss functions for this project, I also prepared a couple of tests to check, if they work correctly. And the results was the discovery of the aforementioned discrepancies between the losses delivered by the keras package and the custom ones based on the new_loss_class. I think I'll stick with the new_loss_class and simply add a relevant explanation in the docs about the differences in the loss objects behaviour. |
I'm trying to define a custom loss class. Normally, when using a loss from
keras
, it can be called in two ways:keras::loss_mean_absolute_error(c(1,2,3), c(3,4,5))
keras::loss_mean_absolute_error(reduction = 'sum')(c(1,2,3), c(3,4,5))
When I define a custom loss class, the first way doesn't work correctly. I don't know, what's the cause. I've tried to skip the
initialize
function definition (example 1), but it hasn't solved the problem. I use the development version ofkeras
(2.9.0.9000).Below I attach an example. In the first case, it even raises an error when calling loss as an object, but it's not the main problem. I've used a custom
quantiles
parameter, so it can be easily overcome by adding theinitialize
method. Unfortunately, I cannot solve the problem withValueError: Invalid Reduction Key when calling loss as a function
.Created on 2022-07-16 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: