Skip to content

Commit f45d5f3

Browse files
committed
Adding keras_evaluate function
1 parent 335428c commit f45d5f3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export(create_keras_sequential_spec)
55
export(generic_functional_fit)
66
export(generic_sequential_fit)
77
export(inp_spec)
8+
export(keras_evaluate)
89
export(keras_losses)
910
export(keras_metrics)
1011
export(keras_optimizers)

R/keras_tools.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#' Evaluate a Kerasnip Model
2+
#'
3+
#' This function provides an `kera_evaluate()` method for `model_fit` objects
4+
#' created by `kerasnip`. It preprocesses the data into the format expected by
5+
#' Keras and then calls `keras3::evaluate()` on the underlying model.
6+
#'
7+
#' @param object A `model_fit` object produced by a `kerasnip` specification.
8+
#' @param x A data frame or matrix of predictors.
9+
#' @param y A vector or data frame of outcomes.
10+
#' @param ... Additional arguments passed on to `keras3::evaluate()`.
11+
#'
12+
#' @return A `list` with evaluation results
13+
#'
14+
#' @export
15+
keras_evaluate <- function(object, x, y = NULL, ...) {
16+
# 1. Preprocess predictor data (x)
17+
x_processed <- process_x(x)
18+
x_proc <- x_processed$x_proc
19+
20+
# 2. Preprocess outcome data (y)
21+
y_proc <- NULL
22+
if (!is.null(y)) {
23+
y_processed <- process_y(
24+
y,
25+
is_classification = !is.null(object$fit$lvl),
26+
class_levels = object$fit$lvl
27+
)
28+
y_proc <- y_processed$y_proc
29+
}
30+
31+
# 3. Call the underlying Keras evaluate method
32+
keras_model <- object$fit$fit
33+
keras3::evaluate(keras_model, x = x_proc, y = y_proc, ...)
34+
}

man/keras_evaluate.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)