Qu

gru_cell

Builtin index · Statistics & ML

result = gru_cell(x_t, h_prev, weights)

Input: x_t, h_prev, weights · Output: result

What it is

One GRU timestep: x_t (length-input_size input vector), h_prev (length-hidden_size previous hidden state), weights (a Record with the nine gate fields gru_init produces).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = gru_cell(x_t, h_prev, weights)
x_t vector or Recordnot documented yet
h_prev vector or Recordnot documented yet
weights vector or Recordnot documented yet
result vector
Returns the length-hidden_size new hidden state vector h directly, not a record — a GRU has no separate cell state to carry alongside it.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

raw = gru_init(2, 3, seed = 1)
w = {wz=param(raw.wz), uz=param(raw.uz), bz=param(raw.bz),
     wr=param(raw.wr), ur=param(raw.ur), br=param(raw.br),
     wh=param(raw.wh), uh=param(raw.uh), bh=param(raw.bh)}
Xseq = [0.5, -0.3, 0.8; 0.2, 0.4, -0.1]   # (input_size, seq_len)

hs = gru_forward(Xseq, [0.0, 0.0, 0.0], w)
h_last = hs[length(hs) - 1]
print("gru_forward last hidden state: {round(h_last, 4)}")

loss = sum(h_last .^ 2)
g = grad(loss, wrt = w.wz)
print("dL/dwz: {round(g, 4)}")
tape_reset()

h1 = gru_cell([1.0], [1.0], {wz=0.5, uz=0.5, bz=0.0, wr=0.3, ur=0.3, br=0.0, wh=0.2, uh=0.2, bh=0.0})
print("gru_cell one step: {round(h1, 4)}")
output
gru_forward last hidden state: [-0.542; -0.0036; -0.0815]
dL/dwz: [0.2321, -0.0486; -0.0006, 0.0001; -0.0079, 0.0034]
gru_cell one step: [0.5012]

from statistics-ml

How it works

gru_cell(x_t, h_prev, weights) — one GRU timestep. See gru_cell_compute's own doc comment for the exact equations and shape conventions; this arm is just argument plumbing.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.