KingCobra's standard library includes std.nn, a native, dependency-free deep-learning library. There is no Python, no GPU and no downloaded weights: it's C code inside the interpreter, and you call it from KingCobra. I've written a guide to it, "Neural Networks in KingCobra: The Complete Guide to std.nn", and this post is the short version.
Read the guide (PDF, 168 pages):
https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf
A whole training program in a dozen lines
import std.nn;
fn main() {
nn.seed(42);
let X = [[0, 0], [0, 1], [1, 0], [1, 1]];
let Y = [[0], [1], [1], [0]];
let net = nn.mlp([2, 8, 1], "activation", "tanh", "out_activation", "sigmoid");
let h = nn.fit(net, "bce", "adam", X, Y, "epochs", 400, "batch_size", 4, "lr", 0.05, "verbose", 0);
print("epochs run:", len(h.loss), " final loss below 0.05:", h.loss[399] < 0.05);
print("predicted classes:", nn.predict_class(net, X));
print(nn.summary(net, [2]));
}
epochs run: 400 final loss below 0.05: true
predicted classes: [0, 1, 1, 0]
Model: sequential (4 layers)
# Layer Output shape Params
0 dense 2->8 [8] 24
1 tanh [8] 0
2 dense 8->1 [1] 9
3 sigmoid [1] 0
Total params: 33 (trainable: 33, frozen: 0, buffers: 0)
Plain nested arrays go straight in, nn.fit runs the whole loop, and nn.seed makes the run repeatable.
What the guide covers
-
Part I: the toolkit. Tensors, broadcasting, reductions,
einsum, statistics, linear algebra (LU, Cholesky, QR, Jacobi eigen and SVD, matrix exponential) and data preparation (one-hot, scaling, splitting, padding, time-series windows). - Part II: the mathematics. From a single neuron to back-propagation, the softmax and cross-entropy gradient, initialization, vanishing and exploding gradients, and checking gradients numerically.
-
Part III: building and training.
-
Layers: dense,
conv1d/conv2d, pooling,rnn/lstm/gruwith full BPTT, attention,pos_encoding, batch and layer norm, embedding, residual blocks andtransformer_block. -
Losses:
mse,mae,huber,bce,ce,kl,hingeand more. -
Optimizers:
sgd,adam,adamw,rmsprop,adagrad, with gradient clipping and seven learning-rate schedules. -
Training:
fit, early stopping and metrics.
-
Layers: dense,
- Part IV: practice. Which architecture for which data, model saving and loading (a CRC-checked file format), freezing layers for transfer learning, reinforcement learning with DQN and Double DQN on a grid world, performance, common mistakes and a verification checklist.
- Appendices. A cheat sheet, reference tables, a formula sheet, a glossary and a function index.
Each layer, activation, loss and optimizer is derived from first principles, with a small example worked by hand and then checked against the real interpreter. There are more than forty worked problems, with full solutions in Chapter 16.
Design in four ideas
-
One small interface. Every layer implements
forward,backwardandsave, so a convolution, an LSTM and a ReLU look identical to the training engine, and anything that fits together shape-wise can be trained. -
Verified gradients. The guide describes
nn.gradcheckand a repository test that checks about sixty architectures against numerical gradients. -
Batch-first shapes.
[N, features]for tables,[N, C, H, W]for images and[N, T, features]for sequences. -
Native handles. Tensors live in C memory, not in KingCobra arrays, so you release them explicitly with
nn.free, in line with KingCobra's no-garbage-collector design.
Honest limits
std.nn is deliberately small and exact:
- Speed. It uses float64 on one CPU thread, with no GPU. The guide reports about 6,000 samples per second for a 784-256-128-10 MLP, about 3,500 for a small CNN, about 1,300 for an LSTM and about 1,000 for a transformer block. Moderate models train in seconds to minutes, and large image or language models are out of reach.
- No pretrained models, so you train, save and load your own.
- Not every architecture. There's no cross-attention and no bidirectional RNN wrapper.
- Not an RL framework. You write the environment and replay buffer yourself in plain KingCobra, and the guide shows how.
- Not a full linear-algebra suite. There are no complex numbers or FFT.
- Resuming. Optimizer state isn't saved, so resuming training restarts Adam's moments.
Every example was run
Every program in the guide was run on KingCobra 1.0.0, and the output under each program was captured automatically while the book was typeset, so the text and the numbers can't drift apart. Random-dependent results use a fixed nn.seed, so you get the same numbers.
Get started
- Guide (PDF): https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf
- Repo: https://github.com/DivyanshuSinha136/KingCobra
- New to the language? Start with the language tutorial, then read Chapter 1 of the guide.
If you train something with std.nn, or find a place where the guide is unclear or a result differs from yours, please open an issue. A star on the repo helps other people find it.
Top comments (0)