Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
save training/evaluation metric during training. close #511
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiang Kou committed Nov 16, 2015
1 parent f3ece7e commit 4850e7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions R-package/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export(mx.symbol.sqrt)
export(mx.symbol.square)
export(mxnet.export)
export(outputs)
export(mx.metric.logger)
import(Rcpp)
import(methods)
importFrom(DiagrammeR,combine_edges)
Expand Down
16 changes: 15 additions & 1 deletion R-package/R/callback.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#' @export mx.metric.logger
mx.metric.logger <- setRefClass("mx.metric.logger", fields = list(train = "numeric", eval="numeric"))

#' Log training metric each period
#' @export
mx.callback.log.train.metric <- function(period) {
mx.callback.log.train.metric <- function(period, logger = NULL) {
function(iteration, nbatch, env) {
if (nbatch %% period == 0 && !is.null(env$metric)) {
result <- env$metric$get(env$train.metric)
cat(paste0("Batch [", nbatch, "] Train-", result$name, "=", result$value, "\n"))
if (!is.null(logger)) {
if (class(logger) != "mx.metric.logger") {
stop("Invalid mx.metric.logger.")
}
logger$train <- c(logger$train, result$value)
if (!is.null(env$eval.metric)) {
result <- env$metric$get(env$eval.metric)
cat(paste0("Batch [", nbatch, "] Validation-", result$name, "=", result$value, "\n"))
logger$eval <- c(logger$eval, result$value)
}
}
}
}
}
Expand Down

0 comments on commit 4850e7b

Please sign in to comment.