{"id":13542768,"url":"https://github.com/codeplea/genann","last_synced_at":"2025-10-17T16:24:52.612Z","repository":{"id":47735411,"uuid":"51344685","full_name":"codeplea/genann","owner":"codeplea","description":"simple neural network library in ANSI C","archived":false,"fork":false,"pushed_at":"2024-06-26T06:31:21.000Z","size":75,"stargazers_count":2110,"open_issues_count":13,"forks_count":245,"subscribers_count":87,"default_branch":"master","last_synced_at":"2025-05-10T01:51:30.369Z","etag":null,"topics":["ann","ansi","artificial-neural-networks","backpropagation","c","genetic-algorithm","hidden-layers","neural","neural-network","neural-networks","neurons","tiny"],"latest_commit_sha":null,"homepage":"https://codeplea.com/genann","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"zlib","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeplea.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-09T03:33:50.000Z","updated_at":"2025-05-09T01:30:46.000Z","dependencies_parsed_at":"2024-05-01T22:12:41.106Z","dependency_job_id":"b96e6100-b4f7-44d7-a14d-41be054ca88e","html_url":"https://github.com/codeplea/genann","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeplea%2Fgenann","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeplea%2Fgenann/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeplea%2Fgenann/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeplea%2Fgenann/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeplea","download_url":"https://codeload.github.com/codeplea/genann/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310515,"owners_count":22049469,"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":["ann","ansi","artificial-neural-networks","backpropagation","c","genetic-algorithm","hidden-layers","neural","neural-network","neural-networks","neurons","tiny"],"created_at":"2024-08-01T11:00:17.333Z","updated_at":"2025-10-17T16:24:47.581Z","avatar_url":"https://github.com/codeplea.png","language":"C","readme":"[![Build Status](https://travis-ci.org/codeplea/genann.svg?branch=master)](https://travis-ci.org/codeplea/genann)\n\n\u003cimg alt=\"Genann logo\" src=\"https://codeplea.com/public/content/genann_logo.png\" align=\"right\" /\u003e\n\n# Genann\n\nGenann is a minimal, well-tested library for training and using feedforward\nartificial neural networks (ANN) in C. Its primary focus is on being simple,\nfast, reliable, and hackable. It achieves this by providing only the necessary\nfunctions and little extra.\n\n## Features\n\n- **C99 with no dependencies**.\n- Contained in a single source code and header file.\n- Simple.\n- Fast and thread-safe.\n- Easily extendible.\n- Implements backpropagation training.\n- *Compatible with alternative training methods* (classic optimization, genetic algorithms, etc)\n- Includes examples and test suite.\n- Released under the zlib license - free for nearly any use.\n\n## Building\n\nGenann is self-contained in two files: `genann.c` and `genann.h`. To use Genann, simply add those two files to your project.\n\n## Example Code\n\nFour example programs are included with the source code.\n\n- [`example1.c`](./example1.c) - Trains an ANN on the XOR function using backpropagation.\n- [`example2.c`](./example2.c) - Trains an ANN on the XOR function using random search.\n- [`example3.c`](./example3.c) - Loads and runs an ANN from a file.\n- [`example4.c`](./example4.c) - Trains an ANN on the [IRIS data-set](https://archive.ics.uci.edu/ml/datasets/Iris) using backpropagation.\n\n## Quick Example\n\nWe create an ANN taking 2 inputs, having 1 layer of 3 hidden neurons, and\nproviding 2 outputs. It has the following structure:\n\n![NN Example Structure](./doc/e1.png)\n\nWe then train it on a set of labeled data using backpropagation and ask it to\npredict on a test data point:\n\n```C\n#include \"genann.h\"\n\n/* Not shown, loading your training and test data. */\ndouble **training_data_input, **training_data_output, **test_data_input;\n\n/* New network with 2 inputs,\n * 1 hidden layer of 3 neurons each,\n * and 2 outputs. */\ngenann *ann = genann_init(2, 1, 3, 2);\n\n/* Learn on the training set. */\nfor (i = 0; i \u003c 300; ++i) {\n    for (j = 0; j \u003c 100; ++j)\n        genann_train(ann, training_data_input[j], training_data_output[j], 0.1);\n}\n\n/* Run the network and see what it predicts. */\ndouble const *prediction = genann_run(ann, test_data_input[0]);\nprintf(\"Output for the first test data point is: %f, %f\\n\", prediction[0], prediction[1]);\n\ngenann_free(ann);\n```\n\nThis example is to show API usage, it is not showing good machine learning\ntechniques. In a real application you would likely want to learn on the test\ndata in a random order. You would also want to monitor the learning to prevent\nover-fitting.\n\n\n## Usage\n\n### Creating and Freeing ANNs\n```C\ngenann *genann_init(int inputs, int hidden_layers, int hidden, int outputs);\ngenann *genann_copy(genann const *ann);\nvoid genann_free(genann *ann);\n```\n\nCreating a new ANN is done with the `genann_init()` function. Its arguments\nare the number of inputs, the number of hidden layers, the number of neurons in\neach hidden layer, and the number of outputs. It returns a `genann` struct pointer.\n\nCalling `genann_copy()` will create a deep-copy of an existing `genann` struct.\n\nCall `genann_free()` when you're finished with an ANN returned by `genann_init()`.\n\n\n### Training ANNs\n```C\nvoid genann_train(genann const *ann, double const *inputs,\n        double const *desired_outputs, double learning_rate);\n```\n\n`genann_train()` will preform one update using standard backpropogation. It\nshould be called by passing in an array of inputs, an array of expected outputs,\nand a learning rate. See *example1.c* for an example of learning with\nbackpropogation.\n\nA primary design goal of Genann was to store all the network weights in one\ncontigious block of memory. This makes it easy and efficient to train the\nnetwork weights using direct-search numeric optimization algorthims,\nsuch as [Hill Climbing](https://en.wikipedia.org/wiki/Hill_climbing),\n[the Genetic Algorithm](https://en.wikipedia.org/wiki/Genetic_algorithm), [Simulated\nAnnealing](https://en.wikipedia.org/wiki/Simulated_annealing), etc.\nThese methods can be used by searching on the ANN's weights directly.\nEvery `genann` struct contains the members `int total_weights;` and\n`double *weight;`.  `*weight` points to an array of `total_weights`\nsize which contains all weights used by the ANN. See *example2.c* for\nan example of training using random hill climbing search.\n\n### Saving and Loading ANNs\n\n```C\ngenann *genann_read(FILE *in);\nvoid genann_write(genann const *ann, FILE *out);\n```\n \nGenann provides the `genann_read()` and `genann_write()` functions for loading or saving an ANN in a text-based format.\n\n### Evaluating\n\n```C\ndouble const *genann_run(genann const *ann, double const *inputs);\n```\n\nCall `genann_run()` on a trained ANN to run a feed-forward pass on a given set of inputs. `genann_run()`\nwill provide a pointer to the array of predicted outputs (of `ann-\u003eoutputs` length).\n\n\n## Hints\n\n- All functions start with `genann_`.\n- The code is simple. Dig in and change things.\n\n## Extra Resources\n\nThe [comp.ai.neural-nets\nFAQ](http://www.faqs.org/faqs/ai-faq/neural-nets/part1/) is an excellent\nresource for an introduction to artificial neural networks.\n\nIf you need an even smaller neural network library, check out the excellent single-hidden-layer library [tinn](https://github.com/glouw/tinn).\n\nIf you're looking for a heavier, more opinionated neural network library in C,\nI recommend the [FANN library](http://leenissen.dk/fann/wp/). Another\ngood library is Peter van Rossum's [Lightweight Neural\nNetwork](http://lwneuralnet.sourceforge.net/), which despite its name, is\nheavier and has more features than Genann.\n","funding_links":[],"categories":["Artificial Intelligence","C","Utilities ##","AI"],"sub_categories":["Vim ###"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeplea%2Fgenann","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeplea%2Fgenann","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeplea%2Fgenann/lists"}