v2026.11.2
All Bundles
Bundle

Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

DenseNetwork

A multi-layer dense (fully connected) network trained by minibatch Adam on mean squared error.

NeuralNetwork above has one hidden layer of sigmoid units and answers Bool; this is the general case: any number of layers of any size, a hidden activation and an output activation (LINEAR, RELU, SIGMOID, TANH), real-valued outputs. Each layer computes act(A W + b). Weights start Glorot-uniform from the seed, biases at 0. Every epoch visits the rows in a seeded shuffle, in minibatches (default 32; the last may be shorter). The loss is the mean over rows and outputs of the squared error, as Keras's and PyTorch's MSE. Adam follows PyTorch: lr 0.001, beta1 0.9, beta2 0.999, eps 1e-8, w -= (lr / (1 - beta1^t)) m / (sqrt(v) / sqrt(1 - beta2^t) + eps).

Deterministic: the same seed and data train the same weights.

Example

net := DenseNetwork->New([4, 8, 4], DenseNetwork->Activation->RELU, DenseNetwork->Activation->LINEAR, 42);
loss := net->Fit(X, Y, 100);
Y_hat := net->Predict(X);

Operations

Fit #

Trains on X -> Y.

method : public : Fit(X:Float[,], Y:Float[,], epochs:Int) ~ Float

Parameters

NameTypeDescription
XFloatrows x input units
YFloatrows x output units
epochsIntpasses over the data

Return

TypeDescription
Floatthe last epoch's mean squared error, or -1.0 on bad input (GetError says why)

GetBiases #

A layer's biases (1 x outputs); the live matrix, not a copy.

method : public : GetBiases(layer:Int) ~ Float[,]

Parameters

NameTypeDescription
layerInt0 for the first weight layer

Return

TypeDescription
Floatbiases, or Nil for a bad layer

GetError #

Why the last call failed.

method : public : GetError() ~ String

Return

TypeDescription
Stringthe reason, or an empty string

GetLayerSizes #

Units per layer, input first.

method : public : GetLayerSizes() ~ Int[]

Return

TypeDescription
Intsizes, or Nil when construction failed

GetWeights #

A layer's weights (inputs x outputs); the live matrix, not a copy.

method : public : GetWeights(layer:Int) ~ Float[,]

Parameters

NameTypeDescription
layerInt0 for the first weight layer

Return

TypeDescription
Floatweights, or Nil for a bad layer

Loss #

Mean squared error over rows and outputs.

method : public : Loss(X:Float[,], Y:Float[,]) ~ Float

Parameters

NameTypeDescription
XFloatrows x input units
YFloatrows x output units

Return

TypeDescription
Floatthe error, or -1.0 on bad input

New # constructor

Constructor.

New(sizes:Int[], hidden:DenseNetwork->Activation, output:DenseNetwork->Activation, seed:Int)

Parameters

NameTypeDescription
sizesIntunits per layer, input first and output last (at least two entries, each >= 1)
hiddenDenseNetwork->Activationactivation of every hidden layer
outputDenseNetwork->Activationactivation of the output layer
seedIntseeds the weights and the minibatch shuffles

Predict #

Network outputs for each row.

method : public : Predict(X:Float[,]) ~ Float[,]

Parameters

NameTypeDescription
XFloatrows x input units

Return

TypeDescription
Floatrows x output units, or Nil on bad input

SetAdam #

Sets Adam's moment decay rates and epsilon (defaults 0.9, 0.999, 1e-8).

method : public : SetAdam(beta1:Float, beta2:Float, epsilon:Float) ~ Bool

Parameters

NameTypeDescription
beta1Floatfirst-moment decay in [0, 1)
beta2Floatsecond-moment decay in [0, 1)
epsilonFloatdenominator guard (> 0)

Return

TypeDescription
Boolfalse when out of range

SetBatchSize #

Sets the minibatch size (default 32).

method : public : SetBatchSize(size:Int) ~ Bool

Parameters

NameTypeDescription
sizeIntrows per batch (>= 1)

Return

TypeDescription
Boolfalse when out of range

SetBiases #

Replaces a layer's biases (copied).

method : public : SetBiases(layer:Int, biases:Float[,]) ~ Bool

Parameters

NameTypeDescription
layerInt0 for the first weight layer
biasesFloat1 x outputs

Return

TypeDescription
Boolfalse when the layer or the shape is wrong

SetLearningRate #

Sets the Adam learning rate (default 0.001).

method : public : SetLearningRate(rate:Float) ~ Bool

Parameters

NameTypeDescription
rateFloatlearning rate (> 0)

Return

TypeDescription
Boolfalse when out of range

SetWeights #

Replaces a layer's weights (copied), e.g. to start from known values.

method : public : SetWeights(layer:Int, weights:Float[,]) ~ Bool

Parameters

NameTypeDescription
layerInt0 for the first weight layer
weightsFloatinputs x outputs

Return

TypeDescription
Boolfalse when the layer or the shape is wrong

TrainBatch #

One Adam step on one minibatch.

method : public : TrainBatch(Xb:Float[,], Yb:Float[,]) ~ Float

Parameters

NameTypeDescription
XbFloatbatch inputs
YbFloatbatch targets

Return

TypeDescription
Floatthe batch's summed squared error