|
|
@@ -10,18 +10,20 @@ Sys.setenv(TF_CPP_MIN_LOG_LEVEL = "3") |
|
|
|
#' @param add_reduction TODO: |
|
|
|
#' @param hidden_units TODO: |
|
|
|
#' @param activation TODO: |
|
|
|
#' @param output_activation TODO: |
|
|
|
#' @param dropout TODO: |
|
|
|
#' @param loss TODO: |
|
|
|
#' @param optimizer TODO: |
|
|
|
#' @param metrics TODO: |
|
|
|
#' @param trainable_reduction TODO: |
|
|
|
#' |
|
|
|
#' @import tensorflow |
|
|
|
#' @import reticulate tensorflow |
|
|
|
#' @keywords internal |
|
|
|
build.MLP <- function(input_shapes, d, name, add_reduction, |
|
|
|
output_shape = 1L, |
|
|
|
hidden_units = 512L, |
|
|
|
activation = 'relu', |
|
|
|
output_activation = NULL, |
|
|
|
dropout = 0.4, |
|
|
|
loss = 'MSE', |
|
|
|
optimizer = 'RMSProp', |
|
|
@@ -63,9 +65,25 @@ build.MLP <- function(input_shapes, d, name, add_reduction, |
|
|
|
if (dropout > 0) |
|
|
|
out <- K$layers$Dropout(rate = dropout, name = paste0('dropout', i))(out) |
|
|
|
} |
|
|
|
out <- K$layers$Dense(units = output_shape, name = 'output')(out) |
|
|
|
out <- K$layers$Dense(units = output_shape, activation = output_activation, |
|
|
|
name = 'output')(out) |
|
|
|
|
|
|
|
mlp <- K$models$Model(inputs = inputs, outputs = out, name = name) |
|
|
|
|
|
|
|
if (!is.null(metrics)) { |
|
|
|
metrics <- as.list(metrics) |
|
|
|
for (i in seq_along(metrics)) { |
|
|
|
metric <- metrics[[i]] |
|
|
|
if (all(c("nnsdr.metric", name) %in% class(metric))) { |
|
|
|
metric_fn <- reticulate::py_func(metric(mlp)) |
|
|
|
reticulate::py_set_attr(metric_fn, "__name__", attr(metric, "name")) |
|
|
|
metrics[[i]] <- metric_fn |
|
|
|
} else if ("nnsdr.metric" %in% class(metric)) { |
|
|
|
metrics[[i]] <- NULL # Drop |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
mlp$compile(loss = loss, optimizer = optimizer, metrics = metrics) |
|
|
|
|
|
|
|
mlp |
|
|
@@ -95,18 +113,27 @@ nnsdr <- setRefClass('nnsdr', |
|
|
|
if (is.null(.self$history.opg)) |
|
|
|
return(NULL) |
|
|
|
|
|
|
|
history <- data.frame( |
|
|
|
.self$history.opg, |
|
|
|
model = factor('OPG'), |
|
|
|
epoch = seq_len(nrow(.self$history.opg)) |
|
|
|
) |
|
|
|
|
|
|
|
if (!is.null(.self$history.ref)) |
|
|
|
history <- rbind(history, data.frame( |
|
|
|
if (is.null(.self$history.ref)) { |
|
|
|
history <- data.frame( |
|
|
|
.self$history.opg, |
|
|
|
model = factor('OPG') |
|
|
|
) |
|
|
|
} else { |
|
|
|
hist.opg <- data.frame( |
|
|
|
.self$history.opg, |
|
|
|
model = factor('OPG') |
|
|
|
) |
|
|
|
hist.ref <- data.frame( |
|
|
|
.self$history.ref, |
|
|
|
model = factor('Refinement'), |
|
|
|
epoch = seq_len(nrow(.self$history.ref)) |
|
|
|
)) |
|
|
|
model = factor('Refinement') |
|
|
|
) |
|
|
|
# Augment mutualy exclusive columns |
|
|
|
hist.opg[setdiff(names(hist.ref), names(hist.opg))] <- NA |
|
|
|
hist.ref[setdiff(names(hist.opg), names(hist.ref))] <- NA |
|
|
|
# Combine/Bind |
|
|
|
history <- rbind(hist.opg, hist.ref) |
|
|
|
} |
|
|
|
history$epoch <- seq_len(nrow(history)) |
|
|
|
|
|
|
|
history |
|
|
|
} |