{"id":13560651,"url":"https://github.com/cbovar/ConvNetSharp","last_synced_at":"2025-04-03T16:31:00.612Z","repository":{"id":32912089,"uuid":"36507290","full_name":"cbovar/ConvNetSharp","owner":"cbovar","description":"Deep Learning in C#","archived":false,"fork":false,"pushed_at":"2024-12-29T13:27:36.000Z","size":16635,"stargazers_count":468,"open_issues_count":23,"forks_count":109,"subscribers_count":49,"default_branch":"master","last_synced_at":"2025-03-04T22:37:08.695Z","etag":null,"topics":["ai","convolution","csharp","machine-learning","neural-network"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"robfig/cron","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cbovar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-29T13:53:47.000Z","updated_at":"2025-02-04T10:01:43.000Z","dependencies_parsed_at":"2023-01-14T22:45:24.051Z","dependency_job_id":null,"html_url":"https://github.com/cbovar/ConvNetSharp","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbovar%2FConvNetSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbovar%2FConvNetSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbovar%2FConvNetSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbovar%2FConvNetSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cbovar","download_url":"https://codeload.github.com/cbovar/ConvNetSharp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247036921,"owners_count":20873048,"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","convolution","csharp","machine-learning","neural-network"],"created_at":"2024-08-01T13:00:48.357Z","updated_at":"2025-04-03T16:30:58.232Z","avatar_url":"https://github.com/cbovar.png","language":"C#","readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/cbovar/ConvNetSharp/blob/master/img/ConvNetSharpLogo.png\"\u003e\u003cbr\u003e\u003cbr\u003e\n\u003c/div\u003e\n\n-----------------\n\n[![Build status](https://ci.appveyor.com/api/projects/status/lcqjebortqnn1wkg?svg=true)](https://ci.appveyor.com/project/cbovar/convnetsharp) [![ConvNetSharp NuGet package](https://img.shields.io/nuget/v/Cognitio.ConvNetSharp.Core.svg?style=flat)](https://www.nuget.org/packages/Cognitio.ConvNetSharp.Core/) [![ConvNetSharp NuGet package](https://img.shields.io/nuget/dt/Cognitio.ConvNetSharp.Core.svg?style=flat)](https://www.nuget.org/packages/Cognitio.ConvNetSharp.Core/)\n\n\n# ConvNetSharp\nStarted initially as C# port of [ConvNetJS](https://github.com/karpathy/convnetjs). You can use ConvNetSharp to train and evaluate convolutional neural networks (CNN).\n\nThank you very much to the original author of ConvNetJS (Andrej Karpathy) and to all the contributors!  \nConvNetSharp relies on [ManagedCuda](https://github.com/kunzmi/managedCuda) library to acces NVidia's CUDA\n\n### 3 ways to create neural network\n\n| Core.Layers  | Flow.Layers | Computation graph |\n| ------------- | ------------- | ------------- |\n| No computation graph  | Layers that create a computation graph behind the scene  | 'Pure flow'  |\n| Network organised by stacking layers  | Network organised by stacking layers  | 'Ops' connected to each others. Can implement more complex networks  |\n| ![Layers](https://github.com/cbovar/ConvNetSharp/blob/master/img/structure.png) |  ![Layers](https://github.com/cbovar/ConvNetSharp/blob/master/img/structure.png)  | ![Layers](https://github.com/cbovar/ConvNetSharp/blob/master/img/graph.png)  |\n| E.g. [MnistDemo](https://github.com/cbovar/ConvNetSharp/tree/master/Examples/MnistFlowGPUDemo)  |  E.g. [MnistFlowGPUDemo](https://github.com/cbovar/ConvNetSharp/tree/master/Examples/MnistDemo.Flow.GPU) or [Flow version of Classify2DDemo ](https://github.com/cbovar/ConvNetSharp/blob/master/Examples/FlowDemo/Classify2DDemo.cs)  | E.g. [ExampleCpuSingle](https://github.com/cbovar/ConvNetSharp/blob/master/Examples/FlowDemo/ExampleCPUSingle.cs)  |\n\n## Example Code\n\nHere's a minimum example of defining a **2-layer neural network** and training\nit on a single data point:\n```c#\nusing System;\nusing ConvNetSharp.Core;\nusing ConvNetSharp.Core.Layers.Double;\nusing ConvNetSharp.Core.Training.Double;\nusing ConvNetSharp.Volume;\nusing ConvNetSharp.Volume.Double;\n\nnamespace MinimalExample\n{\n    internal class Program\n    {\n        private static void Main()\n        {\n            // specifies a 2-layer neural network with one hidden layer of 20 neurons\n            var net = new Net\u003cdouble\u003e();\n\n            // input layer declares size of input. here: 2-D data\n            // ConvNetJS works on 3-Dimensional volumes (width, height, depth), but if you're not dealing with images\n            // then the first two dimensions (width, height) will always be kept at size 1\n            net.AddLayer(new InputLayer(1, 1, 2));\n\n            // declare 20 neurons\n            net.AddLayer(new FullyConnLayer(20));\n\n            // declare a ReLU (rectified linear unit non-linearity)\n            net.AddLayer(new ReluLayer());\n\n            // declare a fully connected layer that will be used by the softmax layer\n            net.AddLayer(new FullyConnLayer(10));\n\n            // declare the linear classifier on top of the previous hidden layer\n            net.AddLayer(new SoftmaxLayer(10));\n\n            // forward a random data point through the network\n            var x =  BuilderInstance.Volume.From(new[] { 0.3, -0.5 }, new Shape(2));\n\n            var prob = net.Forward(x);\n\n            // prob is a Volume. Volumes have a property Weights that stores the raw data, and WeightGradients that stores gradients\n            Console.WriteLine(\"probability that x is class 0: \" + prob.Get(0)); // prints e.g. 0.50101\n\n            var trainer = new SgdTrainer(net) { LearningRate = 0.01, L2Decay = 0.001 };\n            trainer.Train(x, BuilderInstance.Volume.From(new[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, new Shape(1, 1, 10, 1))); // train the network, specifying that x is class zero\n\n            var prob2 = net.Forward(x);\n            Console.WriteLine(\"probability that x is class 0: \" + prob2.Get(0));\n            // now prints 0.50374, slightly higher than previous 0.50101: the networks\n            // weights have been adjusted by the Trainer to give a higher probability to\n            // the class we trained the network with (zero)\n        }\n    }\n}\n```\n\n## Fluent API (see [FluentMnistDemo](https://github.com/cbovar/ConvNetSharp/tree/master/Examples/FluentMnistDemo))\n\n```c#\nvar net = FluentNet\u003cdouble\u003e.Create(24, 24, 1)\n                   .Conv(5, 5, 8).Stride(1).Pad(2)\n                   .Relu()\n                   .Pool(2, 2).Stride(2)\n                   .Conv(5, 5, 16).Stride(1).Pad(2)\n                   .Relu()\n                   .Pool(3, 3).Stride(3)\n                   .FullyConn(10)\n                   .Softmax(10)\n                   .Build();\n```\n\n## GPU\n\nTo switch to GPU mode:\n* add '`GPU`' in the namespace: `using ConvNetSharp.Volume.`**GPU**`.Single;` or `using ConvNetSharp.Volume.`**GPU**`.Double;`\n* add `BuilderInstance\u003cfloat\u003e.Volume = new ConvNetSharp.Volume.GPU.Single.VolumeBuilder();` or `BuilderInstance\u003cdouble\u003e.Volume = new ConvNetSharp.Volume.GPU.Double.VolumeBuilder();` at the beggining of your code\n\nYou must have [CUDA version 10.0](https://developer.nvidia.com/cuda-91-download-archivehttps://developer.nvidia.com/cuda-10.0-download-archive?target_os=Windows\u0026target_arch=x86_64\u0026target_version=10) and [cuDNN v7.6.4 (September 27, 2019), for CUDA 10.0](https://developer.nvidia.com/rdp/cudnn-archive) installed.\ncuDNN bin path should be referenced in the PATH environment variable.\n\nMnist GPU demo [here](https://github.com/cbovar/ConvNetSharp/tree/master/Examples/MnistDemo.GPU)\n\n## Save and Load Network\n### Serialization in ConvNetSharp.Core\n```c#\nusing ConvNetSharp.Core.Serialization;\n\n[...]\n\n// Serialize to json \nvar json = net.ToJson();\n\n// Deserialize from json\nNet deserialized = SerializationExtensions.FromJson\u003cdouble\u003e(json);\n```\n\n###  Serialization in ConvNetSharp.Flow\n```c#\nusing ConvNetSharp.Flow.Serialization;\n\n[...]\n\n// Serialize to two files: MyNetwork.graphml (graph structure) / MyNetwork.json (volume data)\nnet.Save(\"MyNetwork\");\n\n// Deserialize from files\nvar deserialized = SerializationExtensions.Load\u003cdouble\u003e(\"MyNetwork\", false)[0];  // first element is the network (second element is the cost if it was saved along)\n```\n","funding_links":[],"categories":["C# #","Uncategorized","C\\#","🗒️ Cheatsheets"],"sub_categories":["Uncategorized","📦 Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcbovar%2FConvNetSharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcbovar%2FConvNetSharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcbovar%2FConvNetSharp/lists"}