View source: R/computeRainfall.R
computeRainfall | R Documentation |
Compute the amount of precipitation given the parameters of a Gamma distribution
as returned by downscalePredict.keras
on a trained model
(see downscaleTrain.keras
)
that optimized the loss function bernouilliGammaLoss
.
computeRainfall(log_alpha, log_beta, simulate = FALSE, bias = NULL)
log_alpha |
A grid. The log of the shape parameter ("log_alpha") as returned by downscalePredict.keras when we use the bernouilliGamma loss function. |
log_beta |
A grid. The log of the scale parameter ("log_beta") as returned by downscalePredict.keras when we use the bernouilliGamma loss function. |
simulate |
A logical value. If TRUE then the output is an stochastic sample for the given distribution parameters (p,alpha and beta). |
bias |
An integer. Added to the quantity of rain after its computation, either deterministically or stochastically. It basically displaces the distribution. Default is NULL. |
A climate4R grid with the deterministic or stochastic temporal serie
depending on whether the simulate
parameter is FALSE or TRUE.
J. Bano-Medina
bernouilliGammaLoss a custom loss function for Bernouilli-Gamma distributions gaussianLoss a custom loss function for gaussian distributions downscaleTrain.keras for training a downscaling deep model with keras downscalePredict.keras for predicting with a keras model prepareNewData.keras for predictor preparation with new (test) data
require(climate4R.datasets)
require(transformeR)
data("NCEP_Iberia_hus850", "NCEP_Iberia_psl", "NCEP_Iberia_ta850")
x <- makeMultiGrid(NCEP_Iberia_hus850, NCEP_Iberia_psl, NCEP_Iberia_ta850)
data("VALUE_Iberia_pr")
y <- VALUE_Iberia_pr
inputs <- layer_input(shape = c(getShape(x,"lat"),getShape(x,"lon"),getShape(x,"var")))
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")
outputs1 <- layer_dense(hidden,units = getShape(y,"loc"), activation = "sigmoid")
outputs2 <- layer_dense(hidden,units = getShape(y,"loc"))
outputs3 <- layer_dense(hidden,units = getShape(y,"loc"))
outputs <- layer_concatenate(list(outputs1,outputs2,outputs3))
model <- keras_model(inputs = inputs, outputs = outputs)
y <- gridArithmetics(y,0.99,operator = "-") %>% binaryGrid("GT",0,partial = TRUE)
pred <- downscaleCV.keras(x, y, model,
sampling.strategy = "kfold.chronological", folds = 4,
scaleGrid.args = list(type = "standardize"),
prepareData.keras.args = list(first.connection = "conv",
last.connection = "dense",
channels = "last"),
compile.args = list(loss = bernouilliGammaLoss(last.connection = "dense"),
optimizer = optimizer_adam()),
fit.args = list(batch_size = 100, epochs = 10, validation_split = 0.1),
loss = "bernouilliGammaLoss",
binarySerie = TRUE, condition = "GE", threshold = 1)
# Deterministic
pred_amo <- computeRainfall(log_alpha = subsetGrid(pred,var = "log_alpha"),
log_beta = subsetGrid(pred,var = "log_beta"),
bias = 0.99)
# Stochastic
pred_amo <- computeRainfall(log_alpha = subsetGrid(pred,var = "log_alpha"),
log_beta = subsetGrid(pred,var = "log_beta"),
simulate = TRUE,
bias = 0.99)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.