{"id":28788065,"url":"https://github.com/habrador/microgradcsharp","last_synced_at":"2026-03-10T14:32:32.579Z","repository":{"id":299564756,"uuid":"1001329948","full_name":"Habrador/MicrogradCSharp","owner":"Habrador","description":"The easiest way to make Neural Networks in C# within Unity. The open-source AI project implements a tiny scalar-valued automatic differentiation (autograd) engine and a Neural Network library. You can use it outside of Unity as well. ","archived":false,"fork":false,"pushed_at":"2025-06-17T06:27:13.000Z","size":137,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-17T07:29:57.160Z","etag":null,"topics":["ai","artificial-intelligence","autograd","csharp","karpathy","micrograd","neural-networks","open-source","unity","unity3d"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Habrador.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,"zenodo":null}},"created_at":"2025-06-13T07:41:01.000Z","updated_at":"2025-06-17T06:27:16.000Z","dependencies_parsed_at":"2025-06-17T07:30:19.423Z","dependency_job_id":null,"html_url":"https://github.com/Habrador/MicrogradCSharp","commit_stats":null,"previous_names":["habrador/microgradcsharp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Habrador/MicrogradCSharp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Habrador%2FMicrogradCSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Habrador%2FMicrogradCSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Habrador%2FMicrogradCSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Habrador%2FMicrogradCSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Habrador","download_url":"https://codeload.github.com/Habrador/MicrogradCSharp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Habrador%2FMicrogradCSharp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260439480,"owners_count":23009294,"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","artificial-intelligence","autograd","csharp","karpathy","micrograd","neural-networks","open-source","unity","unity3d"],"created_at":"2025-06-17T21:01:06.962Z","updated_at":"2026-03-10T14:32:32.558Z","avatar_url":"https://github.com/Habrador.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MicrogradCSharp\n\nMicrogradCSharp is an open-source Artificial Intelligence project. It implements a scalar-valued automatic differentiation (autograd) engine, so you don't have to implement backpropagation on your own, and a Neural Network library for C# within the Unity game engine. There's nothing Unity specific in the library so you can use it for other C# projects as well.  \n\nThis library provides a lightweight, efficient, and simple way to build and train Neural Networks directly in Unity. Whether you're prototyping a new game AI or experimenting with Neural Networks, MicrogradCSharp offers a straightforward and intuitive code library to get you started. You can do regression, you can do classification - and you can even do reinforcement learning. \n\n\u003e [!CAUTION]\n\u003e We all saw in Terminator what can happen if you experiment too much with Artifical Intelligence, please be careful.  \n\n\n## Implementations\n\nActivation functions:\n\n* Tanh\n* Relu\n* Sigmoid\n* Softmax\n\nLoss functions:\n\n* Negative Log-Likelihood (NLL)\n* Mean Squared Error (MSE)\n\nOptimizers:\n\n* SGD with momentum\n* Adam\n\nNeural Networks:\n\n* Regression:\n\t* XOR gate\n\t* Some example Andrej Karpathy used\n\n* Classification:\n\t* Predict the next character in the alphabet given a character\n\t* Predict the class of a point organized like a twister\n\n\u003cimg src=\"/_media/twister-classification.jpg\" width=\"200\"\u003e\n\nHelp functions:\n\n* Batch manager that makes it easy to work with batches when training Neural Networks\n\n\n## How to create a Neural Network?\n\nYou can create it layer-by-layer:\n\n```csharp\n//Create the NN\nMLP nn = new();\n\n//Add layers\n//2 inputs, 3 neurons in the middle layer with tanh activation function, 1 output with no activation function\nnn.AddLayer(nn.Linear(2, 3));\nnn.AddLayer(nn.Tanh());\nnn.AddLayer(nn.Linear(3, 1));\n```\n\n\n...or in one go:\n\n```csharp\n//Create the NN\nMLP nn = new();\n\n//Add layers\n//2 inputs, 3 neurons in the middle layer with tanh activation function, 1 output with no activation function\nnn.AddLayers(nn.Linear(2, 3), nn.Tanh(), nn.Linear(3, 1));\n```\n\n\n## Example usage of Neural Network library\n\nA common \"Hello World\" example when making Neural Networks is the [XOR gate](https://en.wikipedia.org/wiki/XOR_gate). You want to create a Neural Network that understands the following:\n\n| Input 1  | Input  2 | Output   |\n| ---------| -------- | -------- |\n| 0        | 0        | 0        |\n| 0        | 1        | 1        |\n| 1        | 0        | 1        |\n| 1        | 1        | 0        |\n\nThe minimal Neural Network to learn this example has 2 inputs, 3 neurons in the middle layer, and 1 output. It also has 2 biases connected to the middle layer and the ouput layer. A bias always has input 1 and the bias's weight can be trained like the other weights in the network. It looks like this: \n\n\u003cimg src=\"/_media/neural-network-2-3-1.jpg\" width=\"400\"\u003e\n\nThe code for training and test such a Neural Network can be coded in as few lines as:\n\n```csharp\nMicroMath.Random.Seed(0);\n\nValue[][] inputData = Value.Convert(new [] { new[] { 0f, 0f }, new[] { 0f, 1f }, new[] { 1f, 0f }, new[] { 1f, 1f } });\nValue[] outputData = Value.Convert(new[] { 0f, 1f, 1f, 0f });\n\nMLP nn = new();\n\n//2 inputs, 3 neurons in the middle layer with tanh activation function, 1 output with no activation function\nnn.AddLayers(nn.Linear(2, 3), nn.Tanh(), nn.Linear(3, 1)); \n\n//Optimizer that will do gradient descent for us\nAdam optimizer = nn.Adam_Optimizer(nn.GetParameters(), learningRate: 0.1f);\n\n//Train\nfor (int i = 0; i \u003c= 100; i++)\n{\n    Value loss = new(0f);\n\n    for (int j = 0; j \u003c inputData.Length; j++) \n    {\n        loss += Value.Pow(nn.Activate(inputData[j])[0] - outputData[j], 2f); //MSE loss function without the M\n    }\n\n    Debug.Log($\"Iteration: {i}, Network error: {loss.data}\");\n\n    optimizer.ZeroGrad(); //Reset gradients\n\n\tloss.Backward(); //The notorious backpropagation\n\n\toptimizer.Step(); //Update weights and biases\n}\n\n//Test\nfor (int j = 0; j \u003c inputData.Length; j++)\n{\n    Debug.Log(\"Wanted: \" + outputData[j].data + \", Actual: \" + nn.Activate(inputData[j])[0].data);\n}\n```\n\nWhen I ran the Neural Network I got the following results:\n\n| Input 1  | Input  2 | Wanted   | Actual   |\n| ---------| -------- | -------- | -------- |\n| 0        | 0        | 0        | 0,008705 |\n| 0        | 1        | 1        | 0,994957 |\n| 1        | 0        | 1        | 0,993833 |\n| 1        | 1        | 0        | 0,006619 |\n\nThe outputs are very close to the 0 and 1 we wanted - the output will never be exactly 0 or 1. \n\n\n## Example usage of Value class\n\nThe idea of scalar-valued automatic differentiation (autograd) engine is to make it easy to find derivatives. If you do some math using the Value class you can find derivatives by typing .Backward(); which is useful when you start experimenting with Neural Networks and encounter Backpropagation.  \n\n```csharp\nValue a = new(-4.0f);\n\nValue b = new(2.0f);\n\nValue c = a + b;\n\nValue d = a * b + Value.Pow(b, 3f);\n\nc += c + 1f;\n\nc += 1f + c + (-a);\n\nd += d * 2f + (b + a).Relu();\n\nd += 3f * d + (b - a).Relu();\n\nValue e = c - d;\n\nValue f = Value.Pow(e, 2f);\n\nValue g = f / 2.0f;\n\ng += 10.0f / f;\n\nDebug.Log(\"Expected: 24.7041, Actual: \" + g.data);\n\ng.Backward();\n\n//dg/da\nDebug.Log(\"Expected: 138.8338, Actual: \" + a.grad);\n\n//dg/db\nDebug.Log(\"Expected: 645.5773, Actual: \" + b.grad);\n```\n\n\n## Learn more\n\nThis project was inspired by by Andrej Karpathy's Micrograd for Python GitHub project [micrograd](https://github.com/karpathy/micrograd) and YouTube video [The spelled-out intro to neural networks and backpropagation: building micrograd](https://www.youtube.com/watch?v=VMj-3S1tku0). They are great sources if you want to learn more what's going on behind the scenes. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabrador%2Fmicrogradcsharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhabrador%2Fmicrogradcsharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabrador%2Fmicrogradcsharp/lists"}