View source: R/downscaleTrain.keras.R
downscaleTrain.keras | R Documentation |
Train a deep model with keras in the climate4R framework.
downscaleTrain.keras(
obj,
model,
compile.args = list(object = model),
fit.args = list(object = model),
clear.session = FALSE
)
obj |
The object as returned by |
model |
A keras sequential or functional model. |
compile.args |
List of arguments passed to |
fit.args |
List of arguments passed to |
clear.session |
A logical value. Indicates whether we want to destroy the current tensorflow graph and clear the model from memory.
In particular, refers to whether we want to use the function |
This function relies on keras, which is a high-level neural networks API capable of running on top of tensorflow, CNTK or theano. There are official keras tutorials regarding how to build deep learning models. We suggest the user, especially the beginners, to consult these tutorials before using downscaleTrain.keras.
The infered keras model.
J. Bano-Medina
downscalePredict.keras for predicting with a keras model prepareNewData.keras for predictor preparation with new (test) data prepareData.keras for predictor preparation of training data downscaleR.keras Wiki
Other downscaling.functions:
downscalePredict.keras()
,
relevanceMaps()
# Loading data
require(climate4R.datasets)
require(transformeR)
data("VALUE_Iberia_tas")
y <- VALUE_Iberia_tas
data("NCEP_Iberia_hus850", "NCEP_Iberia_psl", "NCEP_Iberia_ta850")
x <- makeMultiGrid(NCEP_Iberia_hus850, NCEP_Iberia_psl, NCEP_Iberia_ta850)
# We standardize the predictors using transformeR function scaleGrid
x <- scaleGrid(x,type = "standardize")
# Preparing the predictors
data <- prepareData.keras(x = x, y = y,
first.connection = "conv",
last.connection = "dense",
channels = "last")
# Defining the keras model....
# We define 3 hidden layers that consists on
# 2 convolutional steps followed by a dense connection.
input_shape <- dim(data$x.global)[-1]
output_shape <- dim(data$y$Data)[2]
inputs <- layer_input(shape = input_shape)
hidden <- inputs %>%
layer_conv_2d(filters = 25, kernel_size = c(3,3), activation = 'relu') %>%
layer_conv_2d(filters = 10, kernel_size = c(3,3), activation = 'relu') %>%
layer_flatten() %>%
layer_dense(units = 10, activation = "relu")
outputs <- layer_dense(hidden,units = output_shape)
model <- keras_model(inputs = inputs, outputs = outputs)
# We can print model in console to observe its configuration
model
# Training the deep learning model
model <- downscaleTrain.keras(data,
model = model,
compile.args = list("loss" = "mse",
"optimizer" = optimizer_adam(lr = 0.01)),
fit.args = list("epochs" = 30, "batch_size" = 100))
# Training a deep learning model
# (saving the model using callbacks according to an early-stopping criteria)
downscaleTrain.keras(data,
model = model,
compile.args = list("loss" = "mse",
"optimizer" = optimizer_adam(lr = 0.01)),
fit.args = list("epochs" = 50, "batch_size" = 100,
"validation_split" = 0.1,
"callbacks" = list(callback_early_stopping(patience = 10),
callback_model_checkpoint(filepath=paste0(getwd(),"/model.h5"),
monitor='val_loss', save_best_only=TRUE))),
clear.session = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.