ag_linear: Create a dense layer with learnable parameters

View source: R/autograd.R

ag_linearR Documentation

Create a dense layer with learnable parameters

Description

Returns a closure-based layer. Because ag_param uses environment semantics, the optimizer updates W and b in-place, and forward() always uses the latest weights.

Usage

ag_linear(
  in_features,
  out_features,
  activation = NULL,
  init = "glorot_uniform",
  gain = 1
)

Arguments

in_features

Input dimension

out_features

Output dimension

activation

"relu", "sigmoid", "tanh", "softmax", or NULL

init

Weight initialisation, [out_features, in_features]:

"glorot_uniform"

(default) uniform on \pm\sqrt{6/(in+out)}; unchanged from earlier versions, same random stream for the same seed.

"orthogonal"

orthogonal rows (out <= in) or columns (out > in), uniform over orthogonal matrices (QR of a Gaussian matrix with the sign of R's diagonal folded into Q). Draws from R's RNG (rnorm), so set.seed reproduces it.

a function

function(out, in) returning the matrix.

a matrix

used as is; no random numbers are drawn.

The bias always starts at zero. init only shapes a new layer: ag_load_model overwrites every weight from the file.

gain

Multiplier for the string initialisations (e.g. sqrt(2) for tanh/ReLU layers, 0.01 for a policy head). A function or matrix init carries its own scale, so gain must stay 1 with them.

Value

List with W, b, forward(x), params()

Examples


layer <- ag_linear(4L, 8L, activation = "relu")
x     <- ag_tensor(matrix(runif(4 * 16), 4, 16))
out   <- layer$forward(x)
head  <- ag_linear(8L, 2L, init = "orthogonal", gain = 0.01)


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.