Model Checkpoints
Save a built sequential network and restore an independent model
with its architecture and exact parameter bits. The default format is .knn.
Save and Load
import klyn.io
import klyn.math.nn
import klyn.math.nn.layers
model = Sequential<Float>([
Input<Float>((28, 28, 1)),
Conv2D<Float>(16, 3, padding="same", activation="relu"),
MaxPooling2D<Float>(2),
Flatten<Float>(),
Dense<Float>(10, activation="softmax")
])
model.compile(optimizer="adam")
model.save(FilePath("MnistCNN.knn"))
restored = Sequential<Float>.load(FilePath("MnistCNN.knn"))
restored.to(Device.cpu)
restored.summary()
The model must be built before saving. An Input declaration builds it
immediately; otherwise fit, evaluate or predict
builds it from input dimensions. Path and String overloads are available. A filename
without an extension also selects KNN; other extensions are rejected. The parent
directory must already exist.
Supported stages are Input, Dense, Conv2D, Conv2DTranspose, MaxPooling2D, AveragePooling2D, Flatten, Dropout, ReLU, Sigmoid, Tanh and Softmax. Unknown stages and custom subclasses are rejected, never silently replaced by a base class.
Exact Precision and Device Selection
Float16, Float and Double are stored as IEEE binary16, binary32 and binary64,
respectively. Loading requires the same static model precision. Weights are binary,
not decimal JSON numbers, so signed zero and floating-point bit patterns survive.
GPU tensors are synchronized before saving. Device selection is not persisted;
the restored model uses Device.auto until you select another policy.
halfModel.save("half.knn")
halfCopy = Sequential<Float16>.load("half.knn")
# Sequential<Float>.load("half.knn") raises ValueException.
Resume Training or Export for Inference
A compiled model saves its SGD or Adam configuration, sparse categorical cross-entropy or mean squared error loss, Adam moments and bias-correction powers, Dropout random state and dynamic loss scaling. Float16 Adam master weights and moments remain FP32 in the checkpoint.
Regression checkpoints restore the MSE objective and floating-target training contract, including Adam continuation. Existing classification KNN 1.0 checkpoints remain readable. Feature-normalization statistics are not model layers and are not saved automatically; retain them with your dataset preparation.
Resume is supported between calls to fit, not midway through an epoch.
Datasets, training history, batch cursors and shuffle permutations are not saved.
A new call to fit uses its supplied seed to initialize shuffling again.
Do not train, predict or mutate parameters concurrently with saving or loading.
model.save("training.knn")
continued = Sequential<Float>.load("training.knn")
# continued.fit(xTrain, yTrain, epochs=5)
model.save("inference.knn", includeOptimizer=false)
inference = Sequential<Float>.load("inference.knn")
# Compile inference before training it.
Load Only the Weights
model.loadWeights(FilePath("best.knn"))
The existing model must be built with the same architecture, precision and layer configuration. Initializer seeds and Dropout random state are not compatibility requirements. Parameter objects retain their identity; only their bytes change. Optimizer accumulators and loss scaling reset, while optimizer hyperparameters remain. This avoids stale Adam master weights overwriting newly loaded FP16 data.
Loading is staged: incompatible metadata or a corrupt tensor leaves the existing
parameters unchanged. Prefer Sequential<T>.load when you want to
resume saved optimizer and random states as well as weights.
Validation and File Safety
KNN 1.0 has a fixed header, a bounded UTF-8 JSON manifest and 64-byte-aligned little-endian tensor blocks. Shapes, dtypes, offsets, lengths, unique identities, padding and SHA-256 checksums are validated before publishing the model.
model = Sequential<Float>.load("model.knn", maxBytes=512ul * 1024ul * 1024ul)
The default tensor budget is 2 GiB and the manifest limit is 16 MiB. The budget limits the sum of checkpoint tensor bytes, not total process memory; loading weights into an existing model also retains the old model while staging the replacement. SHA-256 detects corruption, not authenticity. Use trusted files or a separate authenticated distribution channel.
Saving writes private temporaries in the destination directory and replaces the
previous file atomically after a complete write. No checkpoint code is executed,
no arbitrary class is instantiated and no referenced external path is followed.
Archives named .keras, .pt or .pth are not
accepted by this API; renaming them to .knn does not convert them.
Next Step
Continue with LLM Calls.