zl_graph_cache() wraps zl_graph() for loops whose input shapes vary,
such as a decoder whose count of new positions changes. Its graphs are
built by zl_forward() the first time it sees a set of input shapes,
and reused after. An input given as an R integer vector or array becomes
an integer input (see zl_int()); any other numeric input is f32.
Usage
zl_graph_cache(build, p, loss = NULL, outputs = NULL)
# S3 method for class 'zl_graph_cache'
print(x, ...)Arguments
- build
A function of one argument, the named list of inputs, returning a tensor or a named list of tensors.
- p
The parameter store whose parameters
build()uses.- loss
The name of a scalar output to train on (see the training functions);
NULLfor none.- outputs
The names of the outputs to keep, of those
build()returns;NULLfor all.- x
A graph cache.
- ...
Unused.
Value
A graph cache, of class zl_graph_cache, for zl_forward().
length() gives the number of graphs it holds.
Details
When the store's and the cached graphs' memory together would pass
getOption("zulm.max_bytes"), the graphs used least recently are freed
first; the graph just built is kept.
Examples
p <- zl_params()
zl_param(p, "w", c(3, 2))
gc <- zl_graph_cache(function(x) zl_crossprod(zl_param_tensor(p, "w"), x$input), p)
dim(zl_forward(gc, list(input = matrix(1, 3, 5)))$output)
#> [1] 2 5
dim(zl_forward(gc, list(input = matrix(1, 3, 7)))$output)
#> [1] 2 7
length(gc)
#> [1] 2