Network
The Network class and all of its methods.
Network owns a C network struct and frees it when the Python object is garbage collected. Every operation is a method on it.
from pymlkit import NetworkNetwork()
Creates an empty network that accepts input_size numbers per sample. Add layers with add_dense before training.
Attributes: input_size and output_size. The latter is the neuron count of the most recently added layer, and starts at 1.
add_dense
Appends a fully connected layer. Layers are applied in the order they are added.
| Parameter | Type | Description |
|---|---|---|
neuron_count | int | Number of neurons, which is the width of this layer's output. |
activation | Activations | Non-linearity applied to the layer. Defaults to Linear. |
set_loss
Selects the loss minimised by fit and backward. See Losses.
fit
Trains with mini-batch stochastic gradient descent. Returns nothing.
| Parameter | Type | Description |
|---|---|---|
train_x | list[float] | All samples concatenated. Length must be a multiple of input_size. |
train_y | list[float] | All targets concatenated. Length must equal samples × output_size. |
lr | float | Learning rate. |
epochs | int | Full passes over the data. |
batch | int | Samples per gradient step. |
log_step | int | Loss is printed when epoch % log_step == 0. Must be at least 1. |
Raises ValueError when the data lengths do not match the network shape. See Training for details.
forward
Runs one sample through the network and returns a new list with output_size floats. Raises ValueError if len(tensor) != input_size.
model.forward([0.1, 0.2]) # [0.93]backward
Backpropagates one sample, accumulating gradients. Call forward on the same sample first. Raises ValueError if len(label) != output_size. See Custom loops.
export
Writes all weights and biases to a binary file.
load
Reads weights from a file written by export. Returns False and prints a warning if the file does not exist, otherwise True. See Saving and loading.
