Nerve
⚡ zero dependencies · single header · runs everywhere

Build with Nerve

The modern AI stack in pure C you can read — and in JavaScript via WebAssembly. Train a network, run a Transformer, embed sentences, and learn on-device. No GPU, no Python at runtime, no cloud.

Get started Try the live demos →

Overview

Nerve is a from-scratch implementation of the modern neural-network stack, written to be read. The C library is one header you drop into any project; the same engine compiles to a 65 KB WebAssembly module for the browser and Node.

Generate
A real Transformer (RMSNorm, RoPE, GQA, SwiGLU) — runs a 1.1B LLM on a laptop CPU.
Learn from you
Personalises to your own examples on-device, in milliseconds — privately.
Understand
A MiniLM sentence encoder for semantic search and classification.
See
Recognises handwritten digits (MNIST, ~97%).
Discover
Reads an equation out of your data — a formula you can check, not weights you cannot.

Give nerve_discover.h the nine planets as they were actually measured and it returns Kepler’s third law in half a second. Try it in your browser → — a 70 KB module with no model to download, so it starts instantly and your data never leaves the tab.

Install

C — copy one header

// main.c
#define NERVE_IMPLEMENTATION
#include "nerve.h"
gcc -O2 main.c -o main -lm

No build system, no packages — just gcc and nerve.h.

JavaScript / TypeScript — npm

npm install @fkkarakurt/nerve

C — your first network

A network that learns XOR in a few lines:

#define NERVE_IMPLEMENTATION
#include "nerve.h"

int main(void) {
    float X[] = {0,0, 0,1, 1,0, 1,1};
    float y[] = {0, 1, 1, 0};

    nerve_t *net = nerve_new("2->4->1");   // Adam + Tanh + Xavier
    nerve_fit(net, X, y, 4, 5000);          // train
    for (int i = 0; i < 4; i++) {
        float out;
        nerve_predict(net, &X[i*2], &out);
        printf("%.0f xor %.0f = %.2f\n", X[i*2], X[i*2+1], out);
    }
    nerve_free(net);
}

JavaScript / TypeScript

One await loads both models (a text generator and a sentence encoder). Everything runs on the user's machine.

import Nerve from "@fkkarakurt/nerve";

const nerve = await Nerve.load();

nerve.generate("Once upon a time", { onToken: t => process.stdout.write(t) });
nerve.similarity("a puppy", "a young dog");   // ~0.5

Tutorial — teach it your own data

This is the part the cloud can't do: a model that learns your categories, on your device, in milliseconds.

const nerve = await Nerve.load();

// 1. give a few of your own examples
nerve.teach([
  { text: "schedule a meeting tomorrow", label: "calendar" },
  { text: "i want to eat a hamburger",  label: "food" },
  { text: "go for a run in the park",    label: "fitness" },
]);

// 2. classify anything — it generalises
nerve.classify("i want a cola");
// { label: "food", confidence: 0.69, scores: { calendar:.16, food:.69, fitness:.15 } }
Training happens in the browser in ~40 ms; no server, no data leaving the page.
nerve.index([
  "The capital of France is Paris.",
  "Coffee contains caffeine, a stimulant.",
  "Mount Everest is the tallest mountain.",
]);

nerve.search("what keeps me awake at night?");
// [ { text: "Coffee contains caffeine…", score: 0.29 }, … ]  — matched by meaning

Tutorial — generate text

nerve.generate("In a faraway forest", {
  steps: 120,
  temperature: 0.85,
  onToken: t => out.textContent += t,   // stream into the page
});

JS API reference

MethodReturnsDescription
Nerve.load()Promise<Nerve>Load the models.
embed(text)Float32ArrayL2-normalised meaning vector.
similarity(a,b)numberCosine similarity.
generate(prompt,opts)stringText generation (streams via onToken).
teach(examples)NerveTrain a classifier on {text,label}.
classify(text){label,confidence,scores}Classify with the trained head.
index(notes) / search(q,k)—/hitsSemantic search.

C API reference (core)

FunctionDescription
net_allocate(n, …)Create a network with given layer sizes.
net_set_optimizer / activationAdam/SGD; sigmoid/tanh/ReLU/softmax.
net_set_classification(net)softmax output + cross-entropy.
net_train_epoch(...)Shuffled training epoch.
net_compute / net_classifyInference / argmax label.
net_save / net_loadPersist a model.

Full reference and the scientific manual are in the repository.

Live demos

Everything below runs entirely in your browser — text generation, on-device learning, semantic search, similarity, and handwritten-digit recognition.

Open the playground →