nillanet module
- class nillanet.model.NN(input, output, architecture, activation, derivative1, classifier, derivative2, loss, derivative3, learning_rate, dtype=cupy.float32)[source]
Bases:
object
Minimal feedforward neural network using CuPy.
This class implements batched SGD with configurable activation, classifier, and loss functions. Inputs/targets are kept on device to avoid host↔device copies.
- Parameters:
input (cupy.ndarray | numpy.ndarray) – Training inputs of shape (n_samples, n_features). If NumPy, it will be moved to device.
output (cupy.ndarray | numpy.ndarray) – Training targets of shape (n_samples, n_outputs). If NumPy, it will be moved to device.
architecture (list[int]) – Units per layer, including output layer.
activation (Callable[[cupy.ndarray], cupy.ndarray]) – Hidden-layer activation function.
derivative1 (Callable[[cupy.ndarray], cupy.ndarray]) – Derivative of
activation
evaluated at pre-activations.classifier (Callable[[cupy.ndarray], cupy.ndarray]) – Output-layer transfer function (e.g., identity, sigmoid, softmax).
derivative2 (Callable[[cupy.ndarray], cupy.ndarray]) – Derivative of
classifier
evaluated at pre-activations.loss (Callable[..., cupy.ndarray]) – Loss function that accepts named arguments (e.g.,
yhat
,y
) and returns per-sample losses or their average.derivative3 (Callable[..., cupy.ndarray]) – Derivative of
loss
with respect to predictions (same signature asloss
).learning_rate (float) – SGD step size.
dtype (cupy.dtype, optional) – Floating point dtype for parameters and data. Defaults to
cupy.float32
.
- X
Training inputs with a prepended bias column, shape (n_samples, n_features + 1).
- Type:
- Y
Training targets on device.
- Type:
- W
Layer weight matrices;
W[i]
has shape (in_features_i, out_features_i).- Type:
- batch(x, y)[source]
Run a single forward/backward/update step.
- Parameters:
x (cupy.ndarray | numpy.ndarray) – Inputs, shape (B, D) or (D,).
y (cupy.ndarray | numpy.ndarray) – Targets, shape (B, K) or (K,).
Notes
Ensures inputs/targets reside on device and are at least 2D.
- predict(input)[source]
Run a forward pass to produce predictions.
- Parameters:
input (cupy.ndarray | numpy.ndarray) – Inputs of shape (n_samples, n_features). If NumPy, it will be moved to device.
- Returns:
Model outputs of shape (n_samples, n_outputs).
- Return type:
- train(epochs=1, batch=0)[source]
Train the model using simple SGD.
- Parameters:
epochs – Number of SGD steps to run.
batch – One of: -
1
: sample a single example per step (pure SGD) -0
: use all samples per step (full batch) ->1
and< len(Y)
: use that mini-batch size per step
- Raises:
SystemExit – If
batch
is invalid.
- class nillanet.distributions.Distributions[source]
Bases:
object
Random training distributions for test modules.
- arithmetic_distribution(depth, mode)[source]
predict arithmetic result from distributions of two input values
- Parameters:
- Returns:
tuple of (generated matrix, expected output)
- Raises:
SystemExit – If the provided mode is not “summation” or “one_hot”.
- linear_distribution(depth)[source]
linear regression that predicts y from x for x-values on a random line with slope and intercept
- Parameters:
depth (int) – The number of x-values to generate.
- Returns:
tuple of (generated vector of x-values, vector of expected y-values)
- summation(rows, cols, mode='one_hot')[source]
distributions of binary vectors for testing binary cross entropy (one-hot mode only)
- Parameters:
rows (int) – The number of rows for the generated binary matrix.
cols (int) – The number of columns for the generated binary matrix.
mode (str) – The mode of operation. Accepts either “summation” or “one_hot”. - “summation”: Produces a scalar count of the number of ones in each x vector. - “one_hot”: Produces a one-hot encoded representation of the count of ones in each x vector. Defaults to “one_hot”.
- Returns:
tuple of (generated binary matrix, expected output)
- Raises:
SystemExit if the provided mode is not "summation" or "one_hot". –