cd /news/machine-learning/show-hn-usul-express-neural-networks… · home › topics › machine-learning › article
[ARTICLE · art-166964] src=github.com ↗ pub= topic=machine-learning verified=true sentiment=↑ positive

Show HN: Usul – Express neural networks as raw math in C++23

A developer released Usul-cpp, an open-source C++23 library that lets data scientists express neural networks as raw math instead of relying on heavy frameworks such as TensorFlow, with built-in tensor support, auto-grad and back-propagation. The library is built on top of xtensor and is described as lightweight and memory safe; a sample XOR training loop with a learning rate of 0.1 over 5000 epochs converges to a mean loss of 0.001222. The project is hosted on GitHub at crushr3sist/Usul-cpp, and the author is soliciting feature ideas and contributions while asking that pull requests not be "vibecoded.

read2 min views2 publishedNov 10, 2026

SO, Usul-cpp is a library I've written in C++, it is based around allowing data scientists to create their models without heavy libraries such as tensorflow, and just express their models using maths, and Usul will handle the rest. There is builtin tensor support, auto-grad, back-prop and its quite fast.

I wrote it because, it seemed less mystifying to read maths instead of a random class that supposedly does something. I wrote it from scratch in cpp (23) due to the fact that I wanted to emphasise on precision, however it's subtly built on top of xtensor.

check it out in action #include using namespace std;

  int main() {
    double LR = 0.1;
    int EPOCH = 5000;
    // lets write the forward pass
    Tensor X = {
        {0.0, 0.0}, // this is what we're feeding it
        {0.0, 1.0},
        {1.0, 0.0},
        {1.0, 1.0},
    };

    // this is what we're after
    Tensor Y_true = {
        0.0, // xor{0.0, 0.0} = 0.0
        1.0, // xor{0.0, 1.0} = 1.0
        1.0, // xor{1.0, 0.0} = 1.0
        0.0  // xor{1.0, 1.0} = 0.0
    };

    Y_true.reshape({4, 1});

    // we are now going to create a single layer, double neuron network
    // each layer will have a bias and weights tensor.
    auto W1 = generate_random_matrix({2, 4}, -1.0, 1.0);
    auto B1 = generate_random_matrix({1, 4}, -1.0, 1.0);
    auto W2 = generate_random_matrix({4, 1}, -1.0, 1.0);
    auto B2 = generate_random_matrix({1, 1}, -1.0, 1.0);

    // this is the training loop
    for (int i = 0; i < EPOCH; i++) {
      // so lets do one forward pass
      // first layer
      auto Z1 = X % W1 + B1;
      auto A = Z1.ReLU();
      // second layer
      auto Z2 = A % W2 + B2;

      // activation
      // should be - auto S = Z2.activate(Activate::Sigmoid);
      auto S = Z2.Sigmoid();
      // Loss
      // should be - auto Loss = S.loss(Loss::MSE);

      auto loss = (S - Y_true) * (S - Y_true);
      loss = loss.mean();

      cout << "Loss: " << loss << "\n";
      cout << "guesses: " << "\n"
           << S << "\n";
      cout << "should be: " << Y_true << "\n";

      loss.backwards();

      W1.step(LR);
      B1.step(LR);
      W2.step(LR);
      B2.step(LR);
    }
  }

which will output

  Guesses: 
  Tensor({{ 0.027546},
   { 0.963592},
   { 0.964138},
   { 0.038964}}, shape=(4,1))

  should be: Tensor({{ 0.},
   { 1.},
   { 1.},
   { 0.}}, shape=(4,1))

  Loss: Tensor( 0.001222, shape=())

Just write the maths, and Usul-cpp will handle the rest, it is extremely lightweight, and the entire library is memory safe. And its called Usul, because I'm a huge dune fan. You can view the project at [https://github.com/crushr3sist/Usul-cpp](https://github.com/crushr3sist/Usul-cpp)

Any ideas for what to do next are all welcome, you can also pull and implement features that you think could improve this project. (no vibecoded prs please, this project is not vibe-coded)

Comments URL: https://news.ycombinator.com/item?id=50440232

Points: 2

── more in #machine-learning 4 stories · sorted by recency
── more on @usul-cpp 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/show-hn-usul-express…] indexed:0 read:2min 2026-11-10 · —