{"id":19648118,"url":"https://github.com/dimalmfao/dimanet","last_synced_at":"2025-04-28T15:32:22.806Z","repository":{"id":271457368,"uuid":"742949424","full_name":"dimalmfao/dimanet","owner":"dimalmfao","description":"DimaNet is just a bad integration of a neural network library written in pure, ANSI C (and a little bit of C++).","archived":false,"fork":false,"pushed_at":"2025-01-24T12:02:55.000Z","size":1592,"stargazers_count":4,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T09:25:00.156Z","etag":null,"topics":["ai","c","contributions-welcome","library","open-source"],"latest_commit_sha":null,"homepage":"https://src.eco/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dimalmfao.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":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-13T21:05:35.000Z","updated_at":"2025-01-24T12:02:58.000Z","dependencies_parsed_at":"2025-01-07T22:30:16.346Z","dependency_job_id":null,"html_url":"https://github.com/dimalmfao/dimanet","commit_stats":null,"previous_names":["dimalmfao/dimanet"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimalmfao%2Fdimanet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimalmfao%2Fdimanet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimalmfao%2Fdimanet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimalmfao%2Fdimanet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dimalmfao","download_url":"https://codeload.github.com/dimalmfao/dimanet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251338821,"owners_count":21573614,"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":["ai","c","contributions-welcome","library","open-source"],"created_at":"2024-11-11T14:47:11.585Z","updated_at":"2025-04-28T15:32:22.791Z","avatar_url":"https://github.com/dimalmfao.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# DimaNet \u003cimg alt=\"DimaNet Logo didn't load :(\" width=\"390\" height=\"270\" src=\"misc/logo.png\" align=\"right\" /\u003e\n\n![LICENSE](https://img.shields.io/github/license/dimalmfao/dimanet)\n![GitHub Release](https://img.shields.io/github/v/release/dimalmfao/dimanet)\n![GitHub last commit](https://img.shields.io/github/last-commit/dimalmfao/dimanet)\n\n\nDimaNet, just a bad integration of AI serves as a compact, yet powerful library API designed for the implementation and training of feedforward artificial neural networks (ANN) in the classic *ANSI* C programming language. Striving for ~~simplicity~~, speed, ~~reliability~~, and customization, DimaNet offers a lean set of essential functions, ~~ensuring a clutter-free and efficient user experience~~.\n\nⓘ | This library is primarily developed and maintained by hobbyists and is intended for non-commercial or educational use. It is not recommended for deployment within official organizational settings or professional environments. If so, we welcome collaboration and contributions to elevate its suitability for professional use and organizational deployment.\n:---: | :---\n\nⓘ | To use this library, extract the main two files, dimanet.h and dimanet.c. The rest is unnecessary for building your own projects.\n:---: | :---\n\n#### Notice\n\n\u003e *If you have issues with the permission, or the make file, please report it at the ***Issues*** tab. (make sure to see if the project successfully built on the top of this readme file before reporting anything. if not, it might probably be an issue with your system or the make command it self)*\n\u003e *Also, search errors up instead of commiting issues concerning the commands, etc...*\n\n## Deployments\n\nHere are the list of the workflow statuses of github actions deployments.\n\n|  Name | Status |\n| -------------- | ------------------------------------------------------- |\n| Debug   | ![MAKE DEBUG](https://img.shields.io/github/actions/workflow/status/dimalmfao/dimanet/makedebug.yml) |\n| Gen    | ![MAKE GEN](https://img.shields.io/github/actions/workflow/status/dimalmfao/dimanet/makegen.yml) |\n| Strings | ![MAKE STRINGS](https://img.shields.io/github/actions/workflow/status/dimalmfao/dimanet/makestrings.yml) |\n| Valgrind | ![MAKE GEN](https://img.shields.io/github/actions/workflow/status/dimalmfao/dimanet/makevalgrind.yml) |\n\n\u003e *Note that the main deployment that is required for the project to run is `debug`. Others can be ignored.*\n\n## Automated Installation (Arch Only)\nIf you're using [Arch Linux](https://archlinux.org/), you can easily install the [dimanet library](https://aur.archlinux.org/packages/dimanet) through the [AUR](https://aur.archlinux.org/). Use the [yay](https://aur.archlinux.org/packages/yay) AUR helper by running the following command:\n```\n$ yay -S dimanet\n```\n\u003e *Note that this will only build and install the libraries on your system.*\n\n## Cloning\n\nAlternativly, to clone DimaNet, run:\n```\n$ git clone https://github.com/dimalmfao/dimanet --recursive --depth=1\n```\n`--recursive` to clone dependencies.\n`--depth=1` to only clone the latest commit. (time saver)\n\n## Building\n\nTo build, just run:\n```bash\n$ make\n```\n## Adding DimaNet\nTo use DimaNet, you have to include: `dimanet.c` and `dimanet.h` as **DimaNet is locally contained**. Include the following files with \n```c\n#include \u003cdimanet.c\u003e // Main DimaNet\n#include \u003cdimanet.h\u003e // Header\n```\n\n## Dependencies\n\nTo use the Makefile to run tests and debug scripts, DimaNet requires:\n\n- `make`\n- `gcc`\n- `valgrind`\n\n## Examples\n\nIn the [`examples`](./examples/) folder, there are various examples you can experiment with. Including:\n\n- [`example_1.c`](./examples/example_1.c) - Trains an ANN on the XOR function using backpropagation.\n- [`example_2.c`](./examples/example_2.c) - Trains an ANN on the XOR function using random search.\n- [`example_3.c`](./examples/example_3.c) - Loads and runs an ANN from a file.\n- [`example_4.c`](./examples/example_4.c) - Trains an ANN on the [IRIS data-set](https://archive.ics.uci.edu/ml/datasets/Iris) using backpropagation.\n- [`example_5.c`](./examples/example_5.c) - Visualization of Neural Network Approximation.\n\n`persist.txt` is basically last saved training.\n\u003e *It would be really appreciated to contribute and to add example files :)*\n\n## How it works\n\nArtificial Neural Networks (ANNs) are computing systems inspired by the biological neural networks that constitute animal brains. They are composed of interconnected nodes, or “neurons”, which mimic the neurons in a biological brain. Each connection between neurons can transmit a signal from one to another. The receiving neuron processes the signal and signals downstream neurons connected to it.\n\nSee this visual representation:\n\n\u003cimg src=\"https://raw.githubusercontent.com/dimalmfao/dimanet/master/misc/visualann.png\"\u003e\n\nIn a three-layer neural network, there are three layers of neurons: the input layer, the hidden layer, and the output layer. The input layer receives input patterns and passes them to the neurons in the hidden layer. Each neuron in the hidden layer applies a set of weights to the inputs, sums them, applies an activation function, and passes the result to the neurons in the output layer. The weights represent the strength or intensity of the input for the features being processed.\n\nThe output layer receives the signals from the hidden layer, applies another set of weights, and sums them. The final output is then computed using another activation function. The weights in the network are adjusted based on the error of the final output through a process called backpropagation. In backpropagation, the error is calculated by comparing the predicted output with the expected output, and this error is propagated back through the network, adjusting the weights for each neuron. This process is repeated many times, and the network learns to make more accurate predictions. The three-layer neural network is a fundamental architecture in deep learning, and understanding it is key to understanding more complex neural networks.\n\nWe then train it on a set of labeled data using backpropagation and ask it to predict on a test data point:\n\n```c\n#include \"dimanet.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. */\ndimanet *ann = dimanet_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        dimanet_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 = dimanet_run(ann, test_data_input[0]);\nprintf(\"Output for the first test data point is: %f, %f\\n\", prediction[0], prediction[1]);\n\ndimanet_free(ann);\n```\n\nThis example is to show API usage, it is not showing good machine learning techniques. In a real application you would likely want to learn on the test data in a random order. You would also want to monitor the learning to prevent over-fitting.\n\n### 1. **Activation Functions**\nThe code references several activation functions, which are crucial in neural networks. Here are the mathematical representations:\n\n- **Sigmoid Activation Function:**\n  $sigma(a)%20=%20\\frac{1}{1%20+%20e^{-a}}$\n\n  This function is used to introduce non-linearity into the network.\n\n- **Threshold Activation Function:**\n  ![formula](http://latex.codecogs.com/svg.latex?f(a)%20=%20\\begin{cases}%201%20\u0026%20\\text{if%20}%20a%20\\geq%200%20\\\\%200%20\u0026%20\\text{if%20}%20a%20\u003c%200%20\\end{cases})\n  \n  This is a binary step function that outputs 1 if the input is greater than or equal to zero, otherwise 0.\n\n- **Linear Activation Function:**\n  ![formula](http://latex.codecogs.com/svg.latex?f(a)%20=%20a)\n  \n  This function simply returns the input as the output, providing no transformation.\n\n---\n\n### 2. **Feedforward Calculation**\nThe feedforward process calculates the output of the neural network given an input. For a single neuron, the output \\( y \\) is calculated as:\n![formula](http://latex.codecogs.com/svg.latex?y%20=%20f\\left(\\sum_{i=1}^{n}%20w_i%20x_i%20+%20b\\right))\n\nwhere:\n- \\( f \\) is the activation function,\n- \\( w_i \\) are the weights,\n- \\( x_i \\) are the inputs,\n- \\( b \\) is the bias term.\n\n---\n\n### 3. **Backpropagation**\nBackpropagation is used to update the weights of the network during training. The weight update rule is:\n![formula](http://latex.codecogs.com/svg.latex?w_i%20%3D%20w_i%20-%20%5Ceta%20%5Cfrac%7B%5Cpartial%20E%7D%7B%5Cpartial%20w_i%7D)\n\nwhere:\n- \\( \\eta \\) is the learning rate,\n- \\( \\frac{\\partial E}{\\partial w_i} \\) is the gradient of the error with respect to the weight \\( w_i \\).\n\n---\n\n### 4. **Random Weight Initialization**\nThe weights are initialized randomly, typically using a uniform distribution between 0 and 1:\n![formula](http://latex.codecogs.com/svg.latex?w_i%20=%20\\text{random}(0,%201))\n\nThis helps in breaking symmetry and ensuring that different neurons learn different features.\n\n---\n\n### 5. **Delta Calculation**\nThe delta for each neuron is calculated as:\n![formula](http://latex.codecogs.com/svg.latex?\\delta_j%20=%20f'(a_j)%20\\sum_{k}%20\\delta_k%20w_{jk})\n\nwhere:\n- \\( \\delta_j \\) is the delta for neuron \\( j \\),\n- \\( f'(a_j) \\) is the derivative of the activation function,\n- \\( \\delta_k \\) is the delta for the next layer's neuron \\( k \\),\n- \\( w_{jk} \\) is the weight connecting neuron \\( j \\) to neuron \\( k \\).\n\n---\n\n### 6. **Error Calculation**\nThe error \\( E \\) for a single training example is often calculated using Mean Squared Error (MSE):\n![formula](http://latex.codecogs.com/svg.latex?E%20=%20\\frac{1}{2}%20\\sum_{k}%20(y_k%20-%20t_k)^2)\n\nwhere:\n- \\( y_k \\) is the predicted output,\n- \\( t_k \\) is the target output.\n\n---\n\n### 7. **Weight Update Rule**\nThe weights are updated using the gradient descent rule:\n![formula](http://latex.codecogs.com/svg.latex?w_{ij}%20=%20w_{ij}%20-%20\\eta%20\\delta_j%20x_i)\n\nwhere:\n- \\( \\eta \\) is the learning rate,\n- \\( \\delta_j \\) is the delta for neuron \\( j \\),\n- \\( x_i \\) is the input to the weight \\( w_{ij} \\).\n\n---\n\n### 8. **Total Weights Calculation**\nThe total number of weights in the network is calculated as:\n![formula](http://latex.codecogs.com/svg.latex?\\text{total\\_weights}%20=%20(\\text{inputs}%20\\times%20\\text{hidden})%20+%20(\\text{hidden}%20\\times%20\\text{hidden})%20\\times%20(\\text{hidden\\_layers}%20-%201)%20+%20(\\text{hidden}%20\\times%20\\text{outputs}))\n\nThis formula accounts for the weights between the input layer and the first hidden layer, between hidden layers, and between the last hidden layer and the output layer.\n\n---\n\n### 9. **Total Neurons Calculation**\nThe total number of neurons in the network is calculated as:\n![formula](http://latex.codecogs.com/svg.latex?\\text{total\\_neurons}%20=%20\\text{inputs}%20+%20(\\text{hidden}%20\\times%20\\text{hidden\\_layers})%20+%20\\text{outputs})\n\nThis includes the input neurons, hidden neurons, and output neurons.\n\n---\n\n### 10. **Random Number Generation**\nThe code uses a uniform random number generator to initialize weights:\n![formula](http://latex.codecogs.com/svg.latex?\\text{dimanet\\_RANDOM()}%20=%20\\frac{\\text{rand()}}{\\text{RAND\\_MAX}})\nThis generates a random number between 0 and 1.\n\n## Usage\n\n### Creating and Freeing ANNs\n```C\ndimanet *dimanet_init(int inputs, int hidden_layers, int hidden, int outputs);\ndimanet *dimanet_copy(dimanet const *ann);\nvoid dimanet_free(dimanet *ann);\n```\n\nCreating a new ANN is done with the `dimanet_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 `dimanet` struct pointer.\n\nCalling `dimanet_copy()` will create a deep-copy of an existing `dimanet` struct.\n\nCall `dimanet_free()` when you're finished with an ANN returned by `dimanet_init()`.\n\n\n### Training ANNs\n```C\nvoid dimanet_train(dimanet const *ann,\n                   double const *inputs,\n                   double const *desired_outputs,\n                   double learning_rate);\n```\n\n`dimanet_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.\n\nA primary design goal of dimanet 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 `dimanet` 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.\n\n### Saving and Loading ANNs\n\n```C\ndimanet *dimanet_read(FILE *in);\nvoid dimanet_write(dimanet const *ann, FILE *out);\n```\n \nDimaNet provides the `dimanet_read()` and `dimanet_write()` functions for loading or saving an ANN in a text-based format.\n\n### Evaluating\n\n```C\ndouble const *dimanet_run(dimanet const *ann,\n                          double const *inputs);\n```\n\nCall `dimanet_run()` on a trained ANN to run a feed-forward pass on a given set of inputs. `dimanet_run()`\nwill provide a pointer to the array of predicted outputs (of `ann-\u003eoutputs` length).\n\n## Hints\n\n- Every functions start with `dimanet_`.\n- Feel free to modify the code to your liking.\n\n\u003e *Add more Hints here by pull requesting!*\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).\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 dimanet.\n\n- [comp.ai.neural-nets\nFAQ](http://www.faqs.org/faqs/ai-faq/neural-nets/part1/)\n- [TINN](https://github.com/glouw/tinn)\n- [FANNlib](http://leenissen.dk/fann/wp/)\n- [Lightweight Neural\nNetwork](http://lwneuralnet.sourceforge.net/)\n- [DimaNet](https://github.com/dimalmfao/dimanet/)\n\n\u003e *Add more Extra Resources here by pull requesting!*\n\n## Strings\n\nSimple static string library\nSee more at https://github.com/dimalmfao/stringslib/\n\n``` bash\necho \"made by unsrced with love, too official to be true.\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimalmfao%2Fdimanet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimalmfao%2Fdimanet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimalmfao%2Fdimanet/lists"}