Closed wbzyl closed 1 year ago
The following should work. In your custom step()
function you must assign the predicted valeus to ctx$pred
so they can be found by luz callback that computes the metrics.
net <- nn_module(
initialize = function(d_in, d_hidden, d_out) {
self$net <- nn_sequential(
nn_linear(d_in, d_hidden),
nn_relu(),
nn_linear(d_hidden, d_out)
)
},
forward = function(x) {
self$net(x)
},
step = function() {
if (ctx$training) {
closure <- function() {
ctx$pred <- ctx$model(ctx$input)
loss <- ctx$loss_fn(ctx$pred, ctx$target)
loss$backward()
loss
}
ctx$loss <- ctx$opt$step(closure)
} else {
ctx$pred <- ctx$model(ctx$input)
ctx$loss <- ctx$loss_fn(ctx$pred, ctx$target)
}
}
)
Now that the predicted values are found by a luz callback, the code works.
Thanks.
The code below produces errors, reproduced below, when the line with
metrics
is uncommented.The errors:
This is the code that produces errors.