train: Functions for Training of Generative Neural Networks

Description Usage Arguments Value Author(s) See Also Examples

View source: R/train.R

Description

Functions for training generative neural networks.

Usage

1
2
3
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).

verbose

see fit.keras.engine.training.Model().

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.

Author(s)

Marius Hofert

See Also

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

Examples

 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
## 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")

gnn documentation built on March 13, 2020, 5:07 p.m.

Related to train in gnn...