Description Usage Arguments Details Value Author(s) References Examples
Constructor for a generative feedforward neural network (FNN) model,
an object of S3
class "gnn_FNN"
.
1 2 
dim 

activation 

loss.fun 
loss function specified as 
batch.norm 

dropout.rate 

n.GPU 
nonnegative 
... 
additional arguments passed to 
The S3
class "gnn_FNN"
is a subclass of the
S3
class "gnn_GNN"
which in turn is a subclass of
"gnn_Model"
.
FNN()
returns an object of S3
class "gnn_FNN"
with components
model
FNN model (a keras object inheriting from
the R6 classes "keras.engine.training.Model"
,
"keras.engine.network.Network"
,
"keras.engine.base_layer.Layer"
and "python.builtin.object"
, or a raw
object).
type
character
string indicating
the type of model.
dim
see above.
activation
see above.
batch.norm
see above.
dropout.rate
see above.
n.param
number of trainable, nontrainable and total number of parameters.
loss.type
type of loss function (character
).
n.train
number of training samples (NA_integer_
unless trained).
batch.size
batch size (NA_integer_
unless trained).
n.epoch
number of epochs (NA_integer_
unless trained).
loss
numeric(n.epoch)
containing the
loss function values per epoch.
time
object of S3 class "proc_time"
containing the training time (if trained).
prior
matrix
containing a (sub)sample
of the prior (if trained).
Marius Hofert and Avinash Prasad
Li, Y., Swersky, K. and Zemel, R. (2015). Generative moment matching networks. Proceedings of Machine Learning Research, 37 (International Conference on Maching Learning), 1718–1727. See http://proceedings.mlr.press/v37/li15.pdf (20190824)
Dziugaite, G. K., Roy, D. M. and Ghahramani, Z. (2015). Training generative neural networks via maximum mean discrepancy optimization. AUAI Press, 258–267. See http://www.auai.org/uai2015/proceedings/papers/230.pdf (20190824)
Hofert, M., Prasad, A. and Zhu, M. (2020). Quasirandom sampling for multivariate distributions via generative neural networks. Journal of Computational and Graphical Statistics, doi: 10.1080/10618600.2020.1868302.
Hofert, M., Prasad, A. and Zhu, M. (2020). Multivariate timeseries modeling with generative neural networks. See https://arxiv.org/abs/2002.10645.
Hofert, M. Prasad, A. and Zhu, M. (2020). Applications of multivariate quasirandom sampling with neural networks. See https://arxiv.org/abs/2012.08036.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89  if(TensorFlow_available()) { # rather restrictive (due to RForge, winbuilder)
library(gnn) # for being standalone
## Training data
d < 2 # bivariate case
P < matrix(0.9, nrow = d, ncol = d); diag(P) < 1 # correlation matrix
ntrn < 60000 # training data sample size
set.seed(271)
library(nvmix)
X < rNorm(ntrn, scale = P) # N(0,P) samples
X. < abs(X) # X
## Plot a subsample
m < 2000 # subsample size for plots
opar < par(pty = "s")
plot(X.[1:m,], xlab = expression(X[1]), ylab = expression(X[2])) # plot X
U < apply(X., 2, rank) / (ntrn + 1) # pseudoobservations of X
plot(U[1:m,], xlab = expression(U[1]), ylab = expression(U[2])) # visual check
## Model 1: A basic feedforward neural network (FNN) with MSE loss function
fnn < FNN(c(d, 300, d), loss.fun = "MSE") # define the FNN
fnn < fitGNN(fnn, data = U, n.epoch = 40) # train with batch optimization
plot(fnn, plot.type = "loss") # plot the loss after each epoch
## Model 2: A GMMN (FNN with MMD loss function)
gmmn < FNN(c(d, 300, d)) # define the GMMN (initialized with random weights)
## For training we need to use a minibatch optimization (batch size < nrow(U)).
## For a fair comparison (same number of gradient steps) to NN, we use 500
## samples (25% = 4 gradient steps/epoch) for 10 epochs for GMMN.
library(keras) # for callback_early_stopping()
## We monitor the loss function and stop earlier if the loss function
## over the last patiencemany epochs has changed by less than min_delta
## in absolute value. Then we keep the weights that led to the smallest
## loss seen throughout training.
gmmn < fitGNN(gmmn, data = U, batch.size = 500, n.epoch = 10,
callbacks = callback_early_stopping(monitor = "loss",
min_delta = 1e3, patience = 3,
restore_best_weights = TRUE))
plot(gmmn, plot.type = "loss") # plot the loss after each epoch
## Note:
##  Obviously, in a realworld application, batch.size and n.epoch
## should be (much) larger (e.g., batch.size = 5000, n.epoch = 300).
##  Training is not reproducible (due to keras).
## Model 3: A FNN with CvM loss function
fnnCvM < FNN(c(d, 300, d), loss.fun = "CvM")
fnnCvM < fitGNN(fnnCvM, data = U, batch.size = 500, n.epoch = 10,
callbacks = callback_early_stopping(monitor = "loss",
min_delta = 1e3, patience = 3,
restore_best_weights = TRUE))
plot(fnnCvM, plot.type = "loss") # plot the loss after each epoch
## Sample from the different models
set.seed(271)
V.fnn < rGNN(fnn, size = m)
set.seed(271)
V.gmmn < rGNN(gmmn, size = m)
set.seed(271)
V.fnnCvM < rGNN(fnnCvM, size = m)
## Joint plot of training subsample with GMMN PRNs. Clearly, the MSE
## cannot be used to learn the distribution correctly.
layout(matrix(1:4, ncol = 2, byrow = TRUE))
plot(U[1:m,], xlab = expression(U[1]), ylab = expression(U[2]), cex = 0.2)
mtext("Training subsample", side = 4, line = 0.4, adj = 0)
plot(V.fnn, xlab = expression(V[1]), ylab = expression(V[2]), cex = 0.2)
mtext("Trained NN with MSE loss", side = 4, line = 0.4, adj = 0)
plot(V.gmmn, xlab = expression(V[1]), ylab = expression(V[2]), cex = 0.2)
mtext("Trained NN with MMD loss", side = 4, line = 0.4, adj = 0)
plot(V.fnnCvM, xlab = expression(V[1]), ylab = expression(V[2]), cex = 0.2)
mtext("Trained NN with CvM loss", side = 4, line = 0.4, adj = 0)
## Joint plot of training subsample with GMMN QRNs
library(qrng) # for sobol()
V.fnn. < rGNN(fnn, size = m, method = "sobol", randomize = "Owen")
V.gmmn. < rGNN(gmmn, size = m, method = "sobol", randomize = "Owen")
V.fnnCvM. < rGNN(fnnCvM, size = m, method = "sobol", randomize = "Owen")
plot(U[1:m,], xlab = expression(U[1]), ylab = expression(U[2]), cex = 0.2)
mtext("Training subsample", side = 4, line = 0.4, adj = 0)
plot(V.fnn., xlab = expression(V[1]), ylab = expression(V[2]), cex = 0.2)
mtext("Trained NN with MSE loss", side = 4, line = 0.4, adj = 0)
plot(V.gmmn., xlab = expression(V[1]), ylab = expression(V[2]), cex = 0.2)
mtext("Trained NN with MMD loss", side = 4, line = 0.4, adj = 0)
plot(V.fnnCvM., xlab = expression(V[1]), ylab = expression(V[2]), cex = 0.2)
mtext("Trained NN with CvM loss", side = 4, line = 0.4, adj = 0)
layout(1)
par(opar)
}

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.