{"slug": "show-hn-usul-express-neural-networks-as-raw-math-in-c-23", "title": "Show HN: Usul – Express neural networks as raw math in C++23", "summary": "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.", "body_md": "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.\n\nI 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.\n\ncheck it out in action #include using namespace std;\n\n```\n  int main() {\n    double LR = 0.1;\n    int EPOCH = 5000;\n    // lets write the forward pass\n    Tensor X = {\n        {0.0, 0.0}, // this is what we're feeding it\n        {0.0, 1.0},\n        {1.0, 0.0},\n        {1.0, 1.0},\n    };\n\n    // this is what we're after\n    Tensor Y_true = {\n        0.0, // xor{0.0, 0.0} = 0.0\n        1.0, // xor{0.0, 1.0} = 1.0\n        1.0, // xor{1.0, 0.0} = 1.0\n        0.0  // xor{1.0, 1.0} = 0.0\n    };\n\n    Y_true.reshape({4, 1});\n\n    // we are now going to create a single layer, double neuron network\n    // each layer will have a bias and weights tensor.\n    auto W1 = generate_random_matrix({2, 4}, -1.0, 1.0);\n    auto B1 = generate_random_matrix({1, 4}, -1.0, 1.0);\n    auto W2 = generate_random_matrix({4, 1}, -1.0, 1.0);\n    auto B2 = generate_random_matrix({1, 1}, -1.0, 1.0);\n\n    // this is the training loop\n    for (int i = 0; i < EPOCH; i++) {\n      // so lets do one forward pass\n      // first layer\n      auto Z1 = X % W1 + B1;\n      auto A = Z1.ReLU();\n      // second layer\n      auto Z2 = A % W2 + B2;\n\n      // activation\n      // should be - auto S = Z2.activate(Activate::Sigmoid);\n      auto S = Z2.Sigmoid();\n      // Loss\n      // should be - auto Loss = S.loss(Loss::MSE);\n\n      auto loss = (S - Y_true) * (S - Y_true);\n      loss = loss.mean();\n\n      cout << \"Loss: \" << loss << \"\\n\";\n      cout << \"guesses: \" << \"\\n\"\n           << S << \"\\n\";\n      cout << \"should be: \" << Y_true << \"\\n\";\n\n      loss.backwards();\n\n      W1.step(LR);\n      B1.step(LR);\n      W2.step(LR);\n      B2.step(LR);\n    }\n  }\n```\n\nwhich will output\n\n```\n  Guesses: \n  Tensor({{ 0.027546},\n   { 0.963592},\n   { 0.964138},\n   { 0.038964}}, shape=(4,1))\n\n  should be: Tensor({{ 0.},\n   { 1.},\n   { 1.},\n   { 0.}}, shape=(4,1))\n\n  Loss: Tensor( 0.001222, shape=())\n```\n\nJust 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.\nYou can view the project at [[https://github.com/crushr3sist/Usul-cpp](https://github.com/crushr3sist/Usul-cpp)]([https://github.com/crushr3sist/Usul-cpp](https://github.com/crushr3sist/Usul-cpp))\n\nAny 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)\n\nComments URL: [https://news.ycombinator.com/item?id=50440232](https://news.ycombinator.com/item?id=50440232)\n\nPoints: 2\n\n# Comments: 1", "url": "https://wpnews.pro/news/show-hn-usul-express-neural-networks-as-raw-math-in-c-23", "canonical_source": "https://github.com/crushr3sist/Usul-cpp", "published_at": "2026-11-10 13:43:38+00:00", "updated_at": "2026-11-10 13:53:31.777345+00:00", "lang": "en", "topics": ["machine-learning", "neural-networks", "developer-tools", "ai-tools"], "entities": ["Usul-cpp", "C++23", "xtensor", "TensorFlow", "GitHub"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/show-hn-usul-express-neural-networks-as-raw-math-in-c-23", "markdown": "https://wpnews.pro/news/show-hn-usul-express-neural-networks-as-raw-math-in-c-23.md", "text": "https://wpnews.pro/news/show-hn-usul-express-neural-networks-as-raw-math-in-c-23.txt", "jsonld": "https://wpnews.pro/news/show-hn-usul-express-neural-networks-as-raw-math-in-c-23.jsonld"}}