sparse_matmul#
- class braintrace.sparse_matmul(x, weight, *, sparse_mat, bias=None, weight_fn=None, bias_fn=None)[source]#
ETP-aware sparse matrix multiplication.
Computes \(y = x \mathbin{@} \mathrm{sparse}(f(w)) \; (+ g(b))\), where only the non-zero entries (
weight) of the fixed sparse pattern are trainable and participate in eligibility-trace computation. Auto-dispatches batched/unbatched based onx.ndim.- Parameters:
x (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Input array.weight (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Sparse-matrix data, i.e. the non-zero values, shape(nnz,).sparse_mat (
DataRepresentation) – Sparse-matrix structure (e.g. abrainevent.CSR). Must be abrainevent.DataRepresentation, which implements the ETP online-learning protocol:with_data(substitute new data into the structure),yw_to_w_transposed(apply the transposed sparse pattern to a trace) andyw_to_w(its non-transposed counterpart). Passing any other object raisesTypeError.bias (
Array|ndarray|bool|number|bool|int|float|complex|Quantity|None) – Bias vector. DefaultNone.weight_fn (
Callable[[Array|ndarray|bool|number|bool|int|float|complex|Quantity],Array|ndarray|bool|number|bool|int|float|complex|Quantity] |None) – Elementwise transform applied to the non-zeroweightdata before the matmul.Nonemeans identity (no transform). When provided, the transform is applied inside the primitive so thatxy_to_dwauto-composes the derivative viajax.vjp, returning the gradient w.r.t. the raw data. The transform operates on the unitless mantissa; physical units are split off before and recombined after.bias_fn (
Callable[[Array|ndarray|bool|number|bool|int|float|complex|Quantity],Array|ndarray|bool|number|bool|int|float|complex|Quantity] |None) – Elementwise transform applied tobiasbefore it is added to the output.Nonemeans identity. The derivative is composed by the samejax.vjpcall asweight_fn. The transform operates on the unitless mantissa; physical units are split off before and recombined after.
- Returns:
Output array.
- Return type:
Array|ndarray|bool|number|bool|int|float|complex|Quantity- Raises:
TypeError – If
sparse_matis not abrainevent.DataRepresentation.