{"id":13830932,"url":"https://github.com/jeremyong/cpp_nn_in_a_weekend","last_synced_at":"2026-02-26T20:51:47.918Z","repository":{"id":137383637,"uuid":"280570965","full_name":"jeremyong/cpp_nn_in_a_weekend","owner":"jeremyong","description":"Article and source code reference to construct a C++ neural network in a weekend without any dependencies","archived":false,"fork":false,"pushed_at":"2023-06-16T17:25:38.000Z","size":13447,"stargazers_count":148,"open_issues_count":2,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-20T12:46:04.818Z","etag":null,"topics":["cpp","neural-network"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeremyong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-07-18T03:08:48.000Z","updated_at":"2024-11-03T13:48:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"75e74d17-5041-43cf-b276-3e7e4df1d39a","html_url":"https://github.com/jeremyong/cpp_nn_in_a_weekend","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jeremyong/cpp_nn_in_a_weekend","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyong%2Fcpp_nn_in_a_weekend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyong%2Fcpp_nn_in_a_weekend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyong%2Fcpp_nn_in_a_weekend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyong%2Fcpp_nn_in_a_weekend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeremyong","download_url":"https://codeload.github.com/jeremyong/cpp_nn_in_a_weekend/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyong%2Fcpp_nn_in_a_weekend/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264467915,"owners_count":23612999,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","neural-network"],"created_at":"2024-08-04T10:01:12.879Z","updated_at":"2026-02-26T20:51:47.885Z","avatar_url":"https://github.com/jeremyong.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# C++ Neural Network in a Weekend\n\nThis repository is the companion code to the article \"Neural Network in a Weekend.\" Readers are welcome to clone the repository and use the code herein as a reference if following along the article. Pull requests and issues filed for errors and bugs in both code and/or documentation are welcome and appreciated. However, pull requests that introduce new features are unlikely to be considered, as the ultimate goal of this code is to be tractable for a newer practitioner getting started with deep learning architectures.\n\n[Article pdf link](https://github.com/jeremyong/cpp_nn_in_a_weekend/raw/master/doc/DOC.pdf)\n\n## Compilation and Usage\n\n    mkdir build\n    cd build\n    # substitute Ninja for your preferred generator\n    cmake .. -G Ninja\n    ninja\n    # trains the network and writes the learned parameters to disk\n    ./src/nn train ../data/train\n    # evaluate the model loss and accuracy based on the trained parameters\n    ./src/nn evaluate ../data/test ./ff.params\n\nNote that the actual location of the `nn` executable may depend on your build system and build type. For performance reasons, it recommended to run the training itself with an optimized build, reverting to a development/debug build only when debugging is needed.\n\n## Conventions\n\n1.  Member variables have a single underscore suffix (e.g. `member_variable_`)\n2.  The `F.T.R.` acroynym stands for \"For the reader\" and precedes suggestions for experimentation, improvements, or alternative implementations\n3.  Throughout, you may see the type aliases `num_t` and `rne_t`. These aliases refer to `float` and `std::mt199837` respectively and are defined in `Model.hpp` to easily experiment with alternative precisions and random number engines. The reader may wish to make these parameters changeable by other means.\n\n## General Code Structure\n\nThe neural network is modeled as a computational graph. The graph itself is the `Model` defined in `Model.hpp`. Nodes in the computational graph override the `Node` base class and must implement various methods to explain how data flows through the node (forwards and backwards).\n\nThe fully-connected feedforward node in this example is implemented as `FFNode` in `FFNode.hpp`. The cross-entropy loss node is implemented in `CELossNode.hpp`. Together, these two nodes are all that is needed to train our example on the MNIST dataset.\n\n## Data\n\nFor your convenience, the MNIST data used to train and test the network is provided uncompressed in the `data/` subdirectory. The data is structured like so:\n\n### Images\n\nImage data can be parsed using code provided in the `MNIST.hpp` header, but the data is described here as well. Multi-byte integers are stored with the MSB first, meaning that on a little-endian architecture, the bytes must be flipped. Image pixel data is stored in row-major order and packed contiguously one after another.\n\n     Bytes\n    [00-03] 0x00000803 (Magic Number: 2051)\n    [04-07] image count\n    [08-11] rows\n    [12-15] columns\n    [16]    pixel[0, 0]\n    [17]    pixel[0, 1]\n    ...\n\n### Labels\n\nLabel data is parsed according to the following byte layout:\n\n     Bytes\n    [00-03] 0x00000801 (Magic Number: 2049)\n    [04-07] label count\n    [8]     label 1\n    [9]     label 2\n    ...\n\nThe parser provided by the `MNIST` input node validates the magic numbers to ensure the machine endianness is as expected, and also validates that the image data and label data sizes match.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremyong%2Fcpp_nn_in_a_weekend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeremyong%2Fcpp_nn_in_a_weekend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremyong%2Fcpp_nn_in_a_weekend/lists"}