ag_gather: Pick one entry per column

View source: R/ag_select.R

ag_gatherR Documentation

Pick one entry per column

Description

out[1, j] = x[index[j] + 1, j]: the log-probability of the action taken in sample j, for example. Differentiable in x; the gradient of column j lands on the picked row only.

Usage

ag_gather(x, index)

Arguments

x

ag_tensor or numeric matrix, [n, batch].

index

Integer-valued vector of length ncol(x), 0-based, each in 0..nrow(x)-1. A 1-based index is an error (out of range for n) or, worse, silently the neighbouring row – so the range is checked and NA / non-integers are rejected.

Details

Built as colSums(x * onehot(index)), so the graph backward covers it with no rule of its own. The one-hot is [n, batch] and is uploaded on every call: cheap for tens of classes (games), wasteful for thousands – there a dedicated op with an I32 index tensor is the right tool (TODO.md).

Value

ag_tensor [1, batch].

Examples

ag_gather(matrix(1:6 + 0, 3, 2), c(2L, 0L))   # 3 4

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