gnn (version 0.0-2)

training: Functions for Training of Generative Neural Networks

Description

Functions for training generative neural networks.

Usage

train(gnn, data, batch.size, nepoch, verbose = 3, ...)
train_once(gnn, data, batch.size, nepoch,
           file, name = rm_ext(basename(file)), package = NULL, ...)

Arguments

gnn

GNN object as created by GMMN_model() or VAE_model().

data

\((n,d)\)-matrix containing the \(n\) \(d\)-dimensional observations of the training data.

batch.size

number of samples used per stochastic gradient step.

nepoch

number of epochs (one epoch equals one pass through the complete training dataset while updating the GNN's parameters through stochastic gradient steps).

file

character string (with or without ending .rda) specifying the file to save the trained GNN to.

name

name under which the trained GNN is saved in file.

package

name of the package from which to read the trained GNN; if NULL (the default) the current working directory is used.

additional arguments passed to the underlying train() for train_once() and fit() (which is keras:::fit.keras.engine.training.Model()) for train().

Value

train():

The trained GNN object.

train_once():

If object name exists in file, train_once() reads it, converts it to a callable GNN object via to_callable() and returns it. Otherwise, train_once() calls train() to train the GNN, converts it to a savable GNN object via to_savable(), saves it and returns the trained GNN.

See Also

GMMN_model(), VAE_model(), to_savable(), to_callable().

Examples

Run this code
# NOT RUN {
## Training data
d <- 2
P <- matrix(0.9, nrow = d, ncol = d)
diag(P) <- 1
A <- t(chol(P))
set.seed(271)
ntrn <- 60000
Z <- matrix(rnorm(ntrn * d), ncol = d)
X <- t(A %*% t(Z)) # d-dimensional equicorrelated normal
U <- apply(abs(X), 2, rank) / (ntrn + 1) # pseudo-observations of |X|
plot(U[1:2000,], xlab = expression(U[1]), ylab = expression(U[2]))

## Define the model and 'train' it
dim <- c(d, 300, d) # dimensions of the input, hidden and output layers
GMMN.mod <- GMMN_model(dim)
GMMN.trained <- train(GMMN.mod, data = U, batch.size = 500, nepoch = 2)
## Note: Obviously, in a real-world application, batch.size and nepoch
##       should be (much) larger (e.g., batch.size = 5000, nepoch = 300).

## Evaluate (roughly picks up the shape even with our bad choices of
## batch.size and nepoch)
set.seed(271)
N.prior <- matrix(rnorm(2000 * d), ncol = d)
V <- predict(GMMN.trained[["model"]], x = N.prior)
plot(V, xlab = expression(V[1]), ylab = expression(V[2]))

## Convert the trained neural network to one that can be saved
## and save it (here: to some temporary file)
GMMN.savable <- to_savable(GMMN.trained)
file <- tempfile("trained_GMMN", fileext = ".rda")
save_rda(GMMN.savable, file = file, names = "GMMN")
# }

Run the code above in your browser using DataLab