├── .gitignore
├── CONTRIBUTING.md
├── ConvNetSharp.Nuget.sln
├── ConvNetSharp.sln
├── ConvNetSharp.sln.DotSettings
├── Examples
├── Classify2DDemo
│ ├── App.config
│ ├── Classify2DDemo.csproj
│ ├── Program.cs
│ └── README.md
├── FlowDemo
│ ├── Classify2DDemo.cs
│ ├── ExampleCPUDouble.cs
│ ├── ExampleCPUSingle.cs
│ ├── ExampleGPUSingle.cs
│ ├── FlowDemo.csproj
│ ├── Program.cs
│ └── packages.config
├── FluentMnistDemo
│ ├── CircularBuffer.cs
│ ├── DataSet.cs
│ ├── DataSets.cs
│ ├── FluentMnistDemo.csproj
│ ├── MnistEntry.cs
│ ├── MnistReader.cs
│ └── Program.cs
├── MinimalExample
│ ├── MinimalExample.csproj
│ └── Program.cs
├── MnistDemo.Flow.GPU
│ ├── CircularBuffer.cs
│ ├── DataSet.cs
│ ├── DataSets.cs
│ ├── MnistDemo.Flow.GPU.csproj
│ ├── MnistEntry.cs
│ ├── MnistReader.cs
│ ├── Program.cs
│ └── packages.config
├── MnistDemo.GPU
│ ├── CircularBuffer.cs
│ ├── DataSet.cs
│ ├── DataSets.cs
│ ├── MnistEntry.cs
│ ├── MnistReader.cs
│ ├── MnitsDemo.GPU.csproj
│ └── Program.cs
├── MnistDemo
│ ├── CircularBuffer.cs
│ ├── DataSet.cs
│ ├── DataSets.cs
│ ├── MnistDemo.csproj
│ ├── MnistEntry.cs
│ ├── MnistReader.cs
│ └── Program.cs
├── Regression1DDemo
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Regression1DDemo.csproj
├── RnnDemo.Flow
│ ├── Program.cs
│ ├── RnnDemo.Flow.GPU.csproj
│ ├── RnnDemo.cs
│ ├── Simple.txt
│ ├── TextData.cs
│ └── shakespeare
│ │ ├── 1kinghenryiv.txt
│ │ ├── 1kinghenryvi.txt
│ │ ├── 2kinghenryiv.txt
│ │ ├── 2kinghenryvi.txt
│ │ ├── 3kinghenryvi.txt
│ │ ├── allswellthatendswell.txt
│ │ ├── antonyandcleopatra.txt
│ │ ├── asyoulikeit.txt
│ │ ├── comedyoferrors.txt
│ │ ├── coriolanus.txt
│ │ ├── cymbeline.txt
│ │ ├── hamlet.txt
│ │ ├── juliuscaesar.txt
│ │ ├── kinghenryv.txt
│ │ ├── kinghenryviii.txt
│ │ ├── kingjohn.txt
│ │ ├── kinglear.txt
│ │ ├── kingrichardii.txt
│ │ ├── kingrichardiii.txt
│ │ ├── loverscomplaint.txt
│ │ ├── loveslabourslost.txt
│ │ ├── macbeth.txt
│ │ ├── measureforemeasure.txt
│ │ ├── merchantofvenice.txt
│ │ ├── merrywivesofwindsor.txt
│ │ ├── midsummersnightsdream.txt
│ │ ├── muchadoaboutnothing.txt
│ │ ├── othello.txt
│ │ ├── periclesprinceoftyre.txt
│ │ ├── rapeoflucrece.txt
│ │ ├── romeoandjuliet.txt
│ │ ├── sonnets.txt
│ │ ├── tamingoftheshrew.txt
│ │ ├── tempest.txt
│ │ ├── timonofathens.txt
│ │ ├── titusandronicus.txt
│ │ ├── troilusandcressida.txt
│ │ ├── twelfthnight.txt
│ │ ├── twogentlemenofverona.txt
│ │ ├── various.txt
│ │ ├── venusandadonis.txt
│ │ └── winterstale.txt
├── SaveAndLoadDemo.Flow
│ ├── Program.cs
│ └── SaveAndLoadDemo.Flow.csproj
└── SaveAndLoadDemo
│ ├── Program.cs
│ └── SaveAndLoadDemo.csproj
├── LICENSE
├── README.md
├── clean_bin_obj.bat
├── img
├── ConvNetSharpLogo.png
├── ConvNetSharpLogo.svg
├── graph.png
└── structure.png
└── src
├── ConvNetSharp.Core.Tests
├── ConvLayerTests.cs
├── ConvNetSharp.Core.Tests.csproj
├── DropoutLayerTests.cs
├── FullyConnLayerTests.cs
├── GradientCheckTools.cs
├── LeakyReluLayerTests.cs
├── PoolLayerTests.cs
├── RegressionLayerTests.cs
├── ReluLayerTests.cs
├── SerializationTests.cs
├── SigmoidLayerTests.cs
├── SoftMaxLayerTests.cs
└── TanhLayerTests.cs
├── ConvNetSharp.Core
├── ConvNetSharp.Core.Nuget.csproj
├── ConvNetSharp.Core.csproj
├── Fluent
│ ├── Extensions.cs
│ └── FluentNet.cs
├── INet.cs
├── Layers
│ ├── ConvLayer.cs
│ ├── Double
│ │ ├── ConvLayer.cs
│ │ ├── DropoutLayer.cs
│ │ ├── FullyConnLayer.cs
│ │ ├── InputLayer.cs
│ │ ├── LeakyReluLayer.cs
│ │ ├── PoolLayer.cs
│ │ ├── RegressionLayer.cs
│ │ ├── ReluLayer.cs
│ │ ├── SigmoidLayer.cs
│ │ ├── SoftmaxLayer.cs
│ │ └── TanhLayer.cs
│ ├── DropoutLayer.cs
│ ├── FullyConnLayer.cs
│ ├── IClassificationLayer.cs
│ ├── IDotProductLayer.cs
│ ├── ILastLayer.cs
│ ├── InputLayer.cs
│ ├── LastLayerBase.cs
│ ├── LayerBase.cs
│ ├── LeakyReluLayer.cs
│ ├── ParametersAndGradients.cs
│ ├── PoolLayer.cs
│ ├── RegressionLayer.cs
│ ├── ReluLayer.cs
│ ├── SigmoidLayer.cs
│ ├── Single
│ │ ├── ConvLayer.cs
│ │ ├── DropoutLayer.cs
│ │ ├── FullyConnLayer.cs
│ │ ├── InputLayer.cs
│ │ ├── LeakyReluLayer.cs
│ │ ├── PoolLayer.cs
│ │ ├── RegressionLayer.cs
│ │ ├── ReluLayer.cs
│ │ ├── SigmoidLayer.cs
│ │ ├── SoftmaxLayer.cs
│ │ └── TanhLayer.cs
│ ├── SoftMaxLayer.cs
│ └── TanhLayer.cs
├── Net.cs
├── Ops.cs
├── Serialization
│ ├── JObjectExtensions.cs
│ └── SerializationExtensions.cs
├── Training
│ ├── AdamTrainer.cs
│ ├── Double
│ │ ├── AdamTrainer.cs
│ │ └── SgdTrainer.cs
│ ├── SgdTrainer.cs
│ ├── Single
│ │ ├── AdamTrainer.cs
│ │ └── SgdTrainer.cs
│ └── TrainerBase.cs
└── img
│ └── icon.png
├── ConvNetSharp.Flow.Tests
├── ConvNetSharp.Flow.Tests.csproj
├── DoubleGpuOpTests.cs
├── DoubleOpTests.cs
├── OpTests.cs
├── OpsTests.cs
├── SerializationTests.cs
├── SingleGpuOpTests.cs
├── SingleOpTests.cs
└── VolumeMock.cs
├── ConvNetSharp.Flow
├── ConvNetSharp.Flow.Nuget.csproj
├── ConvNetSharp.Flow.csproj
├── ConvNetSharp.cs
├── Fluent
│ └── Extensions.cs
├── Graph
│ ├── DifferentiateVisitor.cs
│ └── IOpVisitor.cs
├── Layers
│ ├── ConvLayer.cs
│ ├── DropoutLayer.cs
│ ├── FullyConnLayer.cs
│ ├── ILastLayer.cs
│ ├── InputLayer.cs
│ ├── LayerBase.cs
│ ├── LeakyReluLayer.cs
│ ├── OpWrapperLayer.cs
│ ├── PoolLayer.cs
│ ├── ReluLayer.cs
│ ├── SigmoidLayer.cs
│ ├── SoftMaxLayer.cs
│ └── TanhLayer.cs
├── Net.cs
├── Ops
│ ├── Activation.cs
│ ├── ActivationGradient.cs
│ ├── Add.cs
│ ├── Assign.cs
│ ├── Concat.cs
│ ├── Const.cs
│ ├── Convolution.cs
│ ├── ConvolutionFilterGradient.cs
│ ├── ConvolutionInputGradient.cs
│ ├── Dense.cs
│ ├── Div.cs
│ ├── Dropout.cs
│ ├── DropoutGradient.cs
│ ├── Exp.cs
│ ├── Extract.cs
│ ├── INamedOp.cs
│ ├── IPersistable.cs
│ ├── LeakyRelu.cs
│ ├── LeakyReluGradient.cs
│ ├── Log.cs
│ ├── MatMult.cs
│ ├── Max.cs
│ ├── Mult.cs
│ ├── Negate.cs
│ ├── Op.cs
│ ├── OpVisitor.cs
│ ├── PlaceHolder.cs
│ ├── Pool.cs
│ ├── PoolGradient.cs
│ ├── Power.cs
│ ├── Reshape.cs
│ ├── Shape.cs
│ ├── SoftMax.cs
│ ├── SoftMaxCrossEntropy.cs
│ ├── SoftMaxGradient.cs
│ ├── Sqrt.cs
│ ├── Sum.cs
│ ├── Tile.cs
│ ├── Transpose.cs
│ └── Variable.cs
├── Scope.cs
├── Serialization
│ └── SerializationExtensions.cs
├── Session.cs
├── Training
│ ├── AdamOptimizer.cs
│ ├── GradientDescentOptimizer.cs
│ ├── SgdTrainer.cs
│ └── TrainerBase.cs
└── img
│ └── icon.png
├── ConvNetSharp.Performance.Tests
├── ConvNetSharp.Performance.Tests.csproj
└── Program.cs
├── ConvNetSharp.Utils
├── ConvNetSharp.Utils.Nuget.csproj
├── ConvNetSharp.Utils.csproj
├── GraphVisualizer
│ ├── GraphControl.xaml
│ ├── GraphControl.xaml.cs
│ ├── OpEdge.cs
│ └── ViewModel.cs
├── Image.cs
└── img
│ └── icon.png
├── ConvNetSharp.Volume.GPU.Tests
├── ConvNetSharp.Volume.GPU.Tests.csproj
├── DoubleVolumeTests.cs
├── GenericVolumeTests.cs
└── SingleVolumeTests.cs
├── ConvNetSharp.Volume.GPU
├── ActivationTypeExtension.cs
├── ConvNetSharp.Volume.GPU.Nuget.csproj
├── ConvNetSharp.Volume.GPU.csproj
├── CudaHostMemoryRegion.cs
├── DataLocation.cs
├── Double
│ ├── BuilderInstance.cs
│ ├── Kernels
│ │ ├── concat.cu
│ │ ├── div.cu
│ │ ├── exp.cu
│ │ ├── extract.cu
│ │ ├── leakyrelu.cu
│ │ ├── leakyrelu_gradient.cu
│ │ ├── log.cu
│ │ ├── power.cu
│ │ └── tile.cu
│ ├── Volume.cs
│ ├── VolumeBuilder.cs
│ └── VolumeStorage.cs
├── GpuContext.cs
├── IVolumeStorage.cs
├── KernelLoader.cs
├── Single
│ ├── BuilderInstance.cs
│ ├── Kernels
│ │ ├── concat.cu
│ │ ├── div.cu
│ │ ├── exp.cu
│ │ ├── extract.cu
│ │ ├── leakyrelu.cu
│ │ ├── leakyrelu_gradient.cu
│ │ ├── log.cu
│ │ ├── power.cu
│ │ └── tile.cu
│ ├── Volume.cs
│ ├── VolumeBuilder.cs
│ └── VolumeStorage.cs
├── TensorReduceOpExtension.cs
└── img
│ └── icon.png
├── ConvNetSharp.Volume.Tests
├── AssertNumber.cs
├── ConvNetSharp.Volume.Tests.csproj
├── DoubleVolumeTests.cs
├── GenericVolumeTests.cs
├── ShapeTests.cs
├── SingleVolumeTests.cs
└── VolumeTests.cs
├── ConvNetSharp.Volume
├── ActivationType.cs
├── ArrayExtensions.cs
├── BuilderInstance.cs
├── ConvNetSharp.Volume.csproj
├── Double
│ ├── BuilderInstance.cs
│ ├── Volume.cs
│ └── VolumeBuilder.cs
├── NcwhVolumeStorage.cs
├── RandomUtilities.cs
├── Shape.cs
├── ShapeExtension.cs
├── Single
│ ├── BuilderInstance.cs
│ ├── Volume.cs
│ └── VolumeBuilder.cs
├── TensorReduceOp.cs
├── Volume.cs
├── VolumeBuilder.cs
├── VolumeStorage.cs
└── img
│ └── icon.png
└── external
└── ManagedCuda
├── CudaBlas.XML
├── CudaBlas.dll
├── CudaDNN.XML
├── CudaDNN.dll
├── ManagedCuda.dll
├── ManagedCuda.xml
├── NVRTC.XML
└── NVRTC.dll
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to contribute
2 |
3 | 1) Pick or create some issues and assign them to yourself
4 | 2) Create a feature branch
5 | 3) Commit documented changes along with unit tests
6 | 4) Create a Pull request
7 |
8 |
9 | Contribution is welcome on any aspect of the project but the following points are the priority:
10 | * Better documentation: Fill up missing public methods documentation headers, find a way to easily export it and host it in github
11 | * Better error messages: End user should understand why an exception is raised (e.g. wrong input shape)
12 | * Make the api less verbose (e.g. volume creation is ugly: ```BuilderInstance.Volume.From(new[] { 0.3, -0.5 }, new Shape(2))```)
13 | * Optimization of ConvNetSharp.Flow computation graph (factorization, simple optimization like replace X * 1 by X, ...)
14 |
--------------------------------------------------------------------------------
/ConvNetSharp.sln.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
3 | True
4 | True
5 | True
6 | True
7 | True
8 | True
9 | True
10 | True
11 | True
--------------------------------------------------------------------------------
/Examples/Classify2DDemo/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Examples/Classify2DDemo/Classify2DDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | Classify2DDemo.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Examples/Classify2DDemo/README.md:
--------------------------------------------------------------------------------
1 | This example shows how to train a neural network to classify a two dimensions vector (x1, x2) into one of two classes (y).
2 | This is therefore supervised training (because the label is given in the training set)
3 |
4 | We want to learn `(x1, x2) -> y where y = 0 OR y = 1`
5 |
6 | ### Trainin set
7 |
8 | | x1 | x2 | y (label / class) |
9 | | ------------- | ------------- | ------------- |
10 | |-0.4326| 1.1909| 1|
11 | |3.0| 4.0|1|
12 | |0.1253| -0.0376|1|
13 | |0.2877| 0.3273|1|
14 | |-1.1465| 0.1746|1|
15 | |1.8133| 1.0139|0|
16 | |2.7258| 1.0668|0|
17 | |1.4117| 0.5593|0|
18 | |4.1832| 0.3044|0|
19 | |1.8636| 0.1677|0|
20 | |0.5| 3.2|1|
21 | |0.8| 3.2|1|
22 | |1.0| -2.2|1|
23 |
24 | ### Code
25 |
26 | #### Learning
27 | This example shows how to input a batch of data (**n** examples at the time) rather than one example at the time.
28 | This means that the network will learn from several example at the time rather than learning by looking at one example at the time. This is more efficient.
29 |
30 | We first try to teach the network `(x1, x2) -> (y1, y2)` where `(y1, y2)` is the one-hot encoded label.
31 | ```
32 | y = 1 -> (y1, y2) = (1, 0)
33 | y = 2 -> (y1, y2) = (0, 1)
34 | ```
35 |
36 | In *Classify2DUpdate*, a Volume of shape [1, 1, 2, n] is created to store input data `(x1, x2)` and another Volume of shape [1, 1, 2, n] is created to store the one-hot encoded labels where n is the batchsize.
37 |
38 | #### Inference
39 |
40 | An input volume **netx** of shape [1, 1, 2, n] is created. For each input (n inputs here), we want to guess the associated label.
41 | This is done with those lines:
42 |
43 | ```c#
44 | var result = net.Forward(netx);
45 | var c = net.GetPrediction();
46 | ```
47 | **Result** will contain the one-hot encoded predicted labels.
48 | **GetPrediction** is a convenience method that will translate from one-hot encoded labels to the labels.
49 | ```
50 | e.g.
51 | (1, 0) -> 0
52 | (0, 1) -> 1
53 | ```
54 | *note:* Learning was done using batch of **n** elements but you can do the inference on any batch size (i.e. the 'n' of inference doesnt have to be the same as the 'n' used during training)
55 |
--------------------------------------------------------------------------------
/Examples/FlowDemo/FlowDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | FlowDemo.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/Examples/FlowDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FlowDemo
4 | {
5 | internal class Program
6 | {
7 | [STAThread]
8 | private static void Main()
9 | {
10 | //ExampleCpuSingle.Example1();
11 | //ExampleCpuDouble.Example2();
12 | //ExampleGpuSingle.Example1();
13 | NetExampleSingle.Classify2DDemo();
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/Examples/FlowDemo/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Examples/FluentMnistDemo/CircularBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace FluentMnistDemo
5 | {
6 | public class CircularBuffer
7 | {
8 | private readonly T[] _buffer;
9 | private int _nextFree;
10 |
11 | public CircularBuffer(int capacity)
12 | {
13 | this.Capacity = capacity;
14 | this.Count = 0;
15 | this._buffer = new T[capacity];
16 | }
17 |
18 | public int Capacity { get; }
19 |
20 | public int Count { get; private set; }
21 |
22 | public IEnumerable Items => this._buffer;
23 |
24 | public void Add(T o)
25 | {
26 | this._buffer[this._nextFree] = o;
27 | this._nextFree = (this._nextFree + 1) % this._buffer.Length;
28 | this.Count = Math.Min(this.Count + 1, this.Capacity);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/Examples/FluentMnistDemo/FluentMnistDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | FluentMnistDemo.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Examples/FluentMnistDemo/MnistEntry.cs:
--------------------------------------------------------------------------------
1 | namespace FluentMnistDemo
2 | {
3 | public class MnistEntry
4 | {
5 | public byte[] Image { get; set; }
6 |
7 | public int Label { get; set; }
8 |
9 | public override string ToString()
10 | {
11 | return "Label: " + this.Label;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/Examples/MinimalExample/MinimalExample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | MinimalExample.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Examples/MinimalExample/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Core;
3 | using ConvNetSharp.Core.Layers.Double;
4 | using ConvNetSharp.Core.Training.Double;
5 | using ConvNetSharp.Volume;
6 | using ConvNetSharp.Volume.Double;
7 |
8 | namespace MinimalExample
9 | {
10 | internal class Program
11 | {
12 | private static void Main()
13 | {
14 | // species a 2-layer neural network with one hidden layer of 20 neurons
15 | var net = new Net();
16 |
17 | // input layer declares size of input. here: 2-D data
18 | // ConvNetJS works on 3-Dimensional volumes (width, height, depth), but if you're not dealing with images
19 | // then the first two dimensions (width, height) will always be kept at size 1
20 | net.AddLayer(new InputLayer(1, 1, 2));
21 |
22 | // declare 20 neurons
23 | net.AddLayer(new FullyConnLayer(20));
24 |
25 | // declare a ReLU (rectified linear unit non-linearity)
26 | net.AddLayer(new ReluLayer());
27 |
28 | // declare a fully connected layer that will be used by the softmax layer
29 | net.AddLayer(new FullyConnLayer(10));
30 |
31 | // declare the linear classifier on top of the previous hidden layer
32 | net.AddLayer(new SoftmaxLayer(10));
33 |
34 | // forward a random data point through the network
35 | var x = BuilderInstance.Volume.From(new[] { 0.3, -0.5 }, new Shape(2));
36 |
37 | var prob = net.Forward(x);
38 |
39 | // prob is a Volume. Volumes have a property Weights that stores the raw data, and WeightGradients that stores gradients
40 | Console.WriteLine("probability that x is class 0: " + prob.Get(0)); // prints e.g. 0.50101
41 |
42 | var trainer = new SgdTrainer(net) { LearningRate = 0.01 };
43 | 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
44 |
45 | var prob2 = net.Forward(x);
46 | Console.WriteLine("probability that x is class 0: " + prob2.Get(0));
47 | // now prints 0.50374, slightly higher than previous 0.50101: the networks
48 | // weights have been adjusted by the Trainer to give a higher probability to
49 | // the class we trained the network with (zero)
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/Examples/MnistDemo.Flow.GPU/CircularBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace MnistDemo.GPU
5 | {
6 | public class CircularBuffer
7 | {
8 | private readonly T[] _buffer;
9 | private int _nextFree;
10 |
11 | public CircularBuffer(int capacity)
12 | {
13 | this.Capacity = capacity;
14 | this.Count = 0;
15 | this._buffer = new T[capacity];
16 | }
17 |
18 | public int Capacity { get; }
19 |
20 | public int Count { get; private set; }
21 |
22 | public IEnumerable Items => this._buffer;
23 |
24 | public void Add(T o)
25 | {
26 | this._buffer[this._nextFree] = o;
27 | this._nextFree = (this._nextFree + 1) % this._buffer.Length;
28 | this.Count = Math.Min(this.Count + 1, this.Capacity);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/Examples/MnistDemo.Flow.GPU/MnistDemo.Flow.GPU.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | MnistDemo.GPU.Program
7 |
8 | Exe
9 |
10 |
11 |
12 | x64
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/Examples/MnistDemo.Flow.GPU/MnistEntry.cs:
--------------------------------------------------------------------------------
1 | namespace MnistDemo.Flow.GPU
2 | {
3 | public class MnistEntry
4 | {
5 | public byte[] Image { get; set; }
6 |
7 | public int Label { get; set; }
8 |
9 | public override string ToString()
10 | {
11 | return "Label: " + this.Label;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/Examples/MnistDemo.Flow.GPU/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Examples/MnistDemo.GPU/CircularBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace MnistDemo.GPU
5 | {
6 | public class CircularBuffer
7 | {
8 | private readonly T[] _buffer;
9 | private int _nextFree;
10 |
11 | public CircularBuffer(int capacity)
12 | {
13 | this.Capacity = capacity;
14 | this.Count = 0;
15 | this._buffer = new T[capacity];
16 | }
17 |
18 | public int Capacity { get; }
19 |
20 | public int Count { get; private set; }
21 |
22 | public IEnumerable Items => this._buffer;
23 |
24 | public void Add(T o)
25 | {
26 | this._buffer[this._nextFree] = o;
27 | this._nextFree = (this._nextFree + 1) % this._buffer.Length;
28 | this.Count = Math.Min(this.Count + 1, this.Capacity);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/Examples/MnistDemo.GPU/MnistEntry.cs:
--------------------------------------------------------------------------------
1 | namespace MnistDemo.GPU
2 | {
3 | public class MnistEntry
4 | {
5 | public byte[] Image { get; set; }
6 |
7 | public int Label { get; set; }
8 |
9 | public override string ToString()
10 | {
11 | return "Label: " + this.Label;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/Examples/MnistDemo.GPU/MnitsDemo.GPU.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | MnistDemo.GPU.Program
7 |
8 | Exe
9 |
10 |
11 |
12 | x64
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Examples/MnistDemo/CircularBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace MnistDemo
5 | {
6 | public class CircularBuffer
7 | {
8 | private readonly T[] _buffer;
9 | private int _nextFree;
10 |
11 | public CircularBuffer(int capacity)
12 | {
13 | this.Capacity = capacity;
14 | this.Count = 0;
15 | this._buffer = new T[capacity];
16 | }
17 |
18 | public int Capacity { get; }
19 |
20 | public int Count { get; private set; }
21 |
22 | public IEnumerable Items => this._buffer;
23 |
24 | public void Add(T o)
25 | {
26 | this._buffer[this._nextFree] = o;
27 | this._nextFree = (this._nextFree + 1) % this._buffer.Length;
28 | this.Count = Math.Min(this.Count + 1, this.Capacity);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/Examples/MnistDemo/MnistDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | MnistDemo.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Examples/MnistDemo/MnistEntry.cs:
--------------------------------------------------------------------------------
1 | namespace MnistDemo
2 | {
3 | public class MnistEntry
4 | {
5 | public byte[] Image { get; set; }
6 |
7 | public int Label { get; set; }
8 |
9 | public override string ToString()
10 | {
11 | return "Label: " + this.Label;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/Examples/Regression1DDemo/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Examples/Regression1DDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Core;
3 | using ConvNetSharp.Core.Layers.Double;
4 | using ConvNetSharp.Core.Training;
5 | using ConvNetSharp.Core.Training.Double;
6 | using ConvNetSharp.Volume;
7 | using ConvNetSharp.Volume.Double;
8 |
9 | namespace Regression1DDemo
10 | {
11 | internal class Program
12 | {
13 | private static void Main()
14 | {
15 | Regression1DDemo();
16 | }
17 |
18 | private static void Regression1DDemo()
19 | {
20 | var net = new Net();
21 | net.AddLayer(new InputLayer(1, 1, 1));
22 | net.AddLayer(new FullyConnLayer(20));
23 | net.AddLayer(new ReluLayer());
24 | net.AddLayer(new FullyConnLayer(20));
25 | net.AddLayer(new SigmoidLayer());
26 | net.AddLayer(new FullyConnLayer(1));
27 | net.AddLayer(new RegressionLayer());
28 |
29 | var trainer = new SgdTrainer(net) { LearningRate = 0.01, Momentum = 0.0, BatchSize = 1, L2Decay = 0.001 };
30 |
31 | // Function we want to learn
32 | double[] x = { 0.0, 0.5, 1.0 };
33 | double[] y = { 0.0, 0.1, 0.2 };
34 | var n = x.Length;
35 |
36 | // Training
37 | do
38 | {
39 | RegressionUpdate(n, x, trainer, y);
40 | } while (!Console.KeyAvailable);
41 |
42 | // Testing
43 | var netx = BuilderInstance.Volume.SameAs(new Shape(1, 1, 1));
44 | for (var ix = 0; ix < n; ix++)
45 | {
46 | netx.Set(0, 0, 0, x[ix]);
47 | var result = net.Forward(netx);
48 | }
49 | }
50 |
51 | private static void RegressionUpdate(int n, double[] x, TrainerBase trainer, double[] y)
52 | {
53 | var netx = BuilderInstance.Volume.SameAs(new Shape(1, 1, 1, n));
54 | var nety = BuilderInstance.Volume.SameAs(new Shape(1, 1, 1, n));
55 | var avloss = 0.0;
56 |
57 | for (var ix = 0; ix < n; ix++)
58 | {
59 | netx.Set(0, 0, 0, ix, x[ix]);
60 | nety.Set(0, 0, 0, ix, y[ix]);
61 | }
62 |
63 | for (var iters = 0; iters < 50; iters++)
64 | {
65 | trainer.Train(netx, nety);
66 | avloss += trainer.Loss;
67 | }
68 |
69 | avloss /= n * 50.0;
70 | Console.WriteLine("Loss:" + avloss);
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/Examples/Regression1DDemo/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Regression1DDemo")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Regression1DDemo")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ebb6ceb5-1390-405c-b0f6-1e9b24306079")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Examples/RnnDemo.Flow/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 |
4 | namespace RnnDemo.Flow.GPU
5 | {
6 | internal class Program
7 | {
8 | ///
9 | /// This demo is has some issues:
10 | /// - Network never manages to get every word right (not matter the size of hidden state)
11 | /// - When starting a new epoch, loss increases. This is probably due to discontinuity in the state.
12 | /// - Text generation is not impressive at all. Some real words are sometimes generated but it's mostly garbage.
13 | ///
14 | /// TODO: use LSTM
15 | ///
16 | private static void Main()
17 | {
18 | // Load Shakespeare data
19 | //var book = new StringBuilder();
20 | //foreach (var file in Directory.EnumerateFiles(@"./shakespeare/", "*.txt"))
21 | //{
22 | // using (var sr = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read)))
23 | // {
24 | // book.Append(sr.ReadToEnd());
25 | // }
26 | //}
27 |
28 | //var textData = new TextData(book.ToString());
29 | var textData = new TextData(File.ReadAllText("Simple.txt"));
30 |
31 | // Start learning
32 | var rnnDemo = new RnnDemo(textData, 3, 100, 300, 0.01f);
33 |
34 | while (true)
35 | {
36 | Console.Clear();
37 | Console.WriteLine(Environment.NewLine + "---- Rnn demo ----");
38 | Console.WriteLine("0) Learning");
39 | Console.WriteLine("1) Text generation");
40 | Console.WriteLine("2) Exit");
41 |
42 | var c = Console.ReadKey();
43 | switch (c.KeyChar)
44 | {
45 | case '2':
46 | return;
47 | case '0':
48 | rnnDemo.Learn();
49 | break;
50 | case '1':
51 | rnnDemo.GenerateText();
52 | break;
53 | }
54 | }
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/Examples/RnnDemo.Flow/RnnDemo.Flow.GPU.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | RnnDemo.Flow.GPU.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | x64
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | PreserveNewest
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/Examples/RnnDemo.Flow/Simple.txt:
--------------------------------------------------------------------------------
1 | Poncho is a police dog. He is from Madrid. He is eight years old. He is a very good dog. He performs at a show for police dogs. People especially love that he can perform CPR. His partner falls to the ground. He runs to him and tries to help him. He starts jumping on his chest. After doing this for a while, the officer gets up.
--------------------------------------------------------------------------------
/Examples/RnnDemo.Flow/TextData.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 |
4 | namespace RnnDemo.Flow.GPU
5 | {
6 | internal class TextData
7 | {
8 | public TextData(string rawData)
9 | {
10 | this.RawData = rawData;
11 | this.Vocabulary = rawData.Select(c => c).Distinct().OrderBy(c => c).ToList();
12 | }
13 |
14 | public string RawData { get; }
15 |
16 | public List Vocabulary { get; }
17 | }
18 | }
--------------------------------------------------------------------------------
/Examples/SaveAndLoadDemo.Flow/SaveAndLoadDemo.Flow.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | SaveAndLoadDemo.Flow.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Examples/SaveAndLoadDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Core;
3 | using ConvNetSharp.Core.Layers.Double;
4 | using ConvNetSharp.Core.Serialization;
5 | using ConvNetSharp.Core.Training.Double;
6 | using ConvNetSharp.Volume;
7 | using ConvNetSharp.Volume.Double;
8 |
9 | namespace SaveAndLoadDemo
10 | {
11 | internal class Program
12 | {
13 | ///
14 | /// This sample shows how to serialize and deserialize a ConvNetSharp.Core network
15 | /// 1) Network creation
16 | /// 2) Dummy Training (only use a single data point)
17 | /// 3) Serialization
18 | /// 4) Deserialization
19 | ///
20 | private static void Main()
21 | {
22 | // 1) Network creation
23 | var net = new Net();
24 |
25 | net.AddLayer(new InputLayer(1, 1, 2));
26 | net.AddLayer(new FullyConnLayer(20));
27 | net.AddLayer(new ReluLayer());
28 | net.AddLayer(new FullyConnLayer(10));
29 | net.AddLayer(new SoftmaxLayer(10));
30 |
31 | // 2) Dummy Training (only use a single data point)
32 | var x = BuilderInstance.Volume.From(new[] {0.3, -0.5}, new Shape(2));
33 | var y = 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(10));
34 |
35 | var count = 0;
36 | var trainer = new SgdTrainer(net) {LearningRate = 0.01};
37 | do
38 | {
39 | trainer.Train(x, y); // train the network, specifying that x is class zero
40 | Console.WriteLine($"Loss: {trainer.Loss}");
41 | count++;
42 | } while (trainer.Loss > 1e-2);
43 |
44 | Console.WriteLine($"{count}");
45 |
46 | // Forward pass with original network
47 | var prob1 = net.Forward(x);
48 | Console.WriteLine("probability that x is class 0: " + prob1.Get(0));
49 |
50 | // 3) Serialization
51 | var json = net.ToJson();
52 |
53 | // 4) Deserialization
54 | var deserialized = SerializationExtensions.FromJson(json);
55 |
56 | // Forward pass with deserialized network
57 | var prob2 = deserialized.Forward(x);
58 | Console.WriteLine("probability that x is class 0: " + prob2.Get(0)); // This should give exactly the same result as previous network evaluation
59 |
60 | Console.ReadLine();
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/Examples/SaveAndLoadDemo/SaveAndLoadDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | SaveAndLoadDemo.Program
7 |
8 | Exe
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright (c) 2018 Cédric Bovar
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/clean_bin_obj.bat:
--------------------------------------------------------------------------------
1 | rd /s /q .\src\ConvNetSharp.Core\bin
2 | rd /s /q .\src\ConvNetSharp.Core.Tests\bin
3 | rd /s /q .\src\ConvNetSharp.Volume\bin
4 | rd /s /q .\src\ConvNetSharp.Volume.GPU\bin
5 | rd /s /q .\src\ConvNetSharp.Volume.GPU.Tests\bin
6 | rd /s /q .\src\ConvNetSharp.Volume.Tests\bin
7 | rd /s /q .\src\ConvNetSharp.Flow\bin
8 | rd /s /q .\src\ConvNetSharp.Flow.Tests\bin
9 | rd /s /q .\src\ConvNetSharp.Performance.Tests\bin
10 | rd /s /q .\Examples\Classify2DDemo\bin
11 | rd /s /q .\Examples\ConvNetSharp.Example\bin
12 | rd /s /q .\Examples\FluentMnistDemo\bin
13 | rd /s /q .\Examples\MinimalExample\bin
14 | rd /s /q .\Examples\MnistDemo\bin
15 | rd /s /q .\Examples\MnistDemo.GPU\bin
16 | rd /s /q .\src\ConvNetSharp.Utils\bin
17 | rd /s /q .\Examples\MnistDemo.Flow.GPU\bin
18 | rd /s /q .\Examples\FlowDemo\bin
19 | rd /s /q .\Examples\RnnDemo.Flow\bin
20 | rd /s /q .\Examples\SaveAndLoadDemo\bin
21 | rd /s /q .\Examples\SaveAndLoadDemo.Flow\bin
22 |
23 | rd /s /q .\src\ConvNetSharp.Core\obj
24 | rd /s /q .\src\ConvNetSharp.Core.Tests\obj
25 | rd /s /q .\src\ConvNetSharp.Volume\obj
26 | rd /s /q .\src\ConvNetSharp.Volume.GPU\obj
27 | rd /s /q .\src\ConvNetSharp.Volume.GPU.Tests\obj
28 | rd /s /q .\src\ConvNetSharp.Volume.Tests\obj
29 | rd /s /q .\src\ConvNetSharp.Flow\obj
30 | rd /s /q .\src\ConvNetSharp.Flow.Tests\obj
31 | rd /s /q .\src\ConvNetSharp.Performance.Tests\obj
32 | rd /s /q .\Examples\Classify2DDemo\obj
33 | rd /s /q .\Examples\ConvNetSharp.Example\obj
34 | rd /s /q .\Examples\FluentMnistDemo\obj
35 | rd /s /q .\Examples\MinimalExample\obj
36 | rd /s /q .\Examples\MnistDemo\obj
37 | rd /s /q .\Examples\MnistDemo.GPU\obj
38 | rd /s /q .\src\ConvNetSharp.Utils\obj
39 | rd /s /q .\Examples\MnistDemo.Flow.GPU\obj
40 | rd /s /q .\Examples\FlowDemo\obj
41 | rd /s /q .\Examples\RnnDemo.Flow\obj
42 | rd /s /q .\Examples\SaveAndLoadDemo\obj
43 | rd /s /q .\Examples\SaveAndLoadDemo.Flow\obj
--------------------------------------------------------------------------------
/img/ConvNetSharpLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cbovar/ConvNetSharp/ccaf6f6959c12026c4e1a0e396a7711654c92551/img/ConvNetSharpLogo.png
--------------------------------------------------------------------------------
/img/graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cbovar/ConvNetSharp/ccaf6f6959c12026c4e1a0e396a7711654c92551/img/graph.png
--------------------------------------------------------------------------------
/img/structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cbovar/ConvNetSharp/ccaf6f6959c12026c4e1a0e396a7711654c92551/img/structure.png
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core.Tests/ConvNetSharp.Core.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | all
14 | runtime; build; native; contentfiles; analyzers; buildtransitive
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core.Tests/DropoutLayerTests.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using ConvNetSharp.Core.Layers;
3 | using ConvNetSharp.Volume;
4 | using ConvNetSharp.Volume.Double;
5 | using NUnit.Framework;
6 |
7 | namespace ConvNetSharp.Core.Tests
8 | {
9 | [TestFixture]
10 | public class DropoutLayerTests
11 | {
12 | [Test]
13 | public void Learning()
14 | {
15 | var n = 1000000;
16 | var dropProbability = 0.2;
17 | var layer = new DropoutLayer(dropProbability);
18 | layer.Init(1, 1, n);
19 |
20 | var input = BuilderInstance.Volume.From(new double[n].Populate(1.0), new Shape(1, 1, n, 1));
21 | var result = layer.DoForward(input, true);
22 |
23 | var val = result.ToArray().First(o => o != 0.0);
24 | var scalingFactor = 1.0 / (1.0 - dropProbability);
25 | Assert.AreEqual(scalingFactor, val); // Make sure output is scaled during learning
26 |
27 | var average = result.ToArray().Average();
28 | var measuredProba = average * dropProbability;
29 | Assert.AreEqual(dropProbability, measuredProba, 0.001); // Make sure dropout really happened
30 | }
31 |
32 | [Test]
33 | public void NotLearning()
34 | {
35 | var n = 1000000;
36 | var dropProbability = 0.2;
37 | var layer = new DropoutLayer(dropProbability);
38 | layer.Init(1, 1, n);
39 |
40 | var input = BuilderInstance.Volume.From(new double[n].Populate(1.0), new Shape(1, 1, n, 1));
41 | var result = layer.DoForward(input);
42 |
43 | var average = result.ToArray().Average();
44 | Assert.AreEqual(1.0, average); // Let everything go through
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core.Tests/LeakyReluLayerTests.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using ConvNetSharp.Core.Layers.Double;
3 | using ConvNetSharp.Volume;
4 | using ConvNetSharp.Volume.Double;
5 | using NUnit.Framework;
6 |
7 | namespace ConvNetSharp.Core.Tests
8 | {
9 | [TestFixture]
10 | public class LeakyReluLayerTests
11 | {
12 | [Test]
13 | public void ComputeTwiceGradientShouldYieldTheSameResult()
14 | {
15 | const int inputWidth = 20;
16 | const int inputHeight = 20;
17 | const int inputDepth = 2;
18 |
19 | var layer = new LeakyReluLayer(0.01);
20 | layer.Init(inputWidth, inputHeight, inputDepth);
21 |
22 | // Forward pass
23 | var input = BuilderInstance.Volume.Random(new Shape(inputWidth, inputHeight, inputDepth));
24 | var output = layer.DoForward(input, true);
25 |
26 | // Set output gradients to 1
27 | var outputGradient = BuilderInstance.Volume.From(new double[output.Shape.TotalLength].Populate(1.0), output.Shape);
28 |
29 | // Backward pass to retrieve gradients
30 | layer.Backward(outputGradient);
31 | var step1 = ((Volume.Double.Volume)layer.InputActivationGradients.Clone()).ToArray();
32 |
33 | layer.Backward(outputGradient);
34 | var step2 = ((Volume.Double.Volume)layer.InputActivationGradients.Clone()).ToArray();
35 |
36 | Assert.IsTrue(step1.SequenceEqual(step2));
37 | }
38 |
39 | [Test]
40 | public void GradientWrtInputCheck()
41 | {
42 | const int inputWidth = 20;
43 | const int inputHeight = 20;
44 | const int inputDepth = 2;
45 |
46 | const int batchSize = 3;
47 |
48 | // Create layer
49 | var layer = new LeakyReluLayer(0.01);
50 |
51 | GradientCheckTools.GradientCheck(layer, inputWidth, inputHeight, inputDepth, batchSize, 1e-6);
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core.Tests/RegressionLayerTests.cs:
--------------------------------------------------------------------------------
1 | using ConvNetSharp.Core.Layers.Double;
2 | using NUnit.Framework;
3 |
4 | namespace ConvNetSharp.Core.Tests
5 | {
6 | [TestFixture]
7 | public class RegressionLayerTests
8 | {
9 | [Test]
10 | public void Instantiation()
11 | {
12 | const int inputWidth = 20;
13 | const int inputHeight = 20;
14 | const int inputDepth = 2;
15 |
16 | var layer = new ReluLayer();
17 | layer.Init(inputWidth, inputHeight, inputDepth);
18 |
19 | Assert.AreEqual(20, layer.OutputWidth);
20 | Assert.AreEqual(20, layer.OutputHeight);
21 | Assert.AreEqual(2, layer.OutputDepth);
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core.Tests/ReluLayerTests.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using ConvNetSharp.Core.Layers.Double;
3 | using ConvNetSharp.Volume;
4 | using ConvNetSharp.Volume.Double;
5 | using NUnit.Framework;
6 |
7 | namespace ConvNetSharp.Core.Tests
8 | {
9 | [TestFixture]
10 | public class ReluLayerTests
11 | {
12 | [Test]
13 | public void ComputeTwiceGradientShouldYieldTheSameResult()
14 | {
15 | const int inputWidth = 20;
16 | const int inputHeight = 20;
17 | const int inputDepth = 2;
18 |
19 | var layer = new ReluLayer();
20 | layer.Init(inputWidth, inputHeight, inputDepth);
21 |
22 | // Forward pass
23 | var input = BuilderInstance.Volume.Random(new Shape(inputWidth, inputHeight, inputDepth));
24 | var output = layer.DoForward(input, true);
25 |
26 | // Set output gradients to 1
27 | var outputGradient = BuilderInstance.Volume.From(new double[output.Shape.TotalLength].Populate(1.0), output.Shape);
28 |
29 | // Backward pass to retrieve gradients
30 | layer.Backward(outputGradient);
31 | var step1 = ((Volume.Double.Volume)layer.InputActivationGradients.Clone()).ToArray();
32 |
33 | layer.Backward(outputGradient);
34 | var step2 = ((Volume.Double.Volume)layer.InputActivationGradients.Clone()).ToArray();
35 |
36 | Assert.IsTrue(step1.SequenceEqual(step2));
37 | }
38 |
39 | [Test]
40 | public void GradientWrtInputCheck()
41 | {
42 | const int inputWidth = 20;
43 | const int inputHeight = 20;
44 | const int inputDepth = 2;
45 |
46 | const int batchSize = 3;
47 |
48 | // Create layer
49 | var layer = new ReluLayer();
50 |
51 | GradientCheckTools.GradientCheck(layer, inputWidth, inputHeight, inputDepth, batchSize, 1e-6);
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core.Tests/SoftMaxLayerTests.cs:
--------------------------------------------------------------------------------
1 | using ConvNetSharp.Core.Layers.Double;
2 | using ConvNetSharp.Volume;
3 | using ConvNetSharp.Volume.Double;
4 | using NUnit.Framework;
5 |
6 | namespace ConvNetSharp.Core.Tests
7 | {
8 | [TestFixture]
9 | public class SoftmaxLayerTests
10 | {
11 | private readonly SoftmaxLayer layer;
12 | private readonly Volume input;
13 |
14 | public VolumeBuilder Volume => BuilderInstance.Volume;
15 |
16 | public SoftmaxLayerTests()
17 | {
18 | this.layer = new SoftmaxLayer(4);
19 | this.layer.Init(1, 1, 4);
20 |
21 | this.input = this.Volume.From(new[]
22 | {
23 | 0.1, 0.1, 0.1, 0.1,
24 | 1000, 2000, 3000, 4000,
25 | 0, 0, 0, 0
26 | }, new Shape(1, 1, 4, 3));
27 | }
28 |
29 | [Test]
30 | public void OutputIsNormalized()
31 | {
32 | var output = this.layer.DoForward(this.input, true);
33 | Assert.AreEqual(1, output.Shape.Dimensions[0]);
34 | Assert.AreEqual(1, output.Shape.Dimensions[1]);
35 | Assert.AreEqual(4, output.Shape.Dimensions[2]);
36 | Assert.AreEqual(3, output.Shape.Dimensions[3]);
37 |
38 | var values = output.ToArray();
39 | Assert.AreEqual(0.25, values[0]);
40 | Assert.AreEqual(0.25, values[1]);
41 | Assert.AreEqual(0.25, values[2]);
42 | Assert.AreEqual(0.25, values[3]);
43 |
44 | Assert.AreEqual(0, values[4]);
45 | Assert.AreEqual(0, values[5]);
46 | Assert.AreEqual(0, values[6]);
47 | Assert.AreEqual(1, values[7]);
48 |
49 | Assert.AreEqual(0.25, values[8]);
50 | Assert.AreEqual(0.25, values[9]);
51 | Assert.AreEqual(0.25, values[10]);
52 | Assert.AreEqual(0.25, values[11]);
53 | }
54 |
55 | [Test]
56 | public void StorageIsReusedIfPossible()
57 | {
58 | var output1 = this.layer.DoForward(this.input, true);
59 | var output2 = this.layer.DoForward(this.input, true);
60 | Assert.AreSame(output1, output2, "Storage is reused if possible.");
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/ConvNetSharp.Core.Nuget.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 |
6 |
7 |
8 | 0.4.14
9 | 0.4.14.0
10 | Cedric Bovar
11 | Deep learning in C#
12 | Copyright (c) 2020
13 |
14 |
15 |
16 | Cognitio.ConvNetSharp.Core
17 | 0.4.14
18 | false
19 | Deep Learning Convolution AI
20 | https://github.com/cbovar/ConvNetSharp/blob/master/LICENSE
21 | https://github.com/cbovar/ConvNetSharp
22 | ConvNetSharp.Core
23 | ConvNetSharp.Core
24 | https://github.com/cbovar/ConvNetSharp/releases/tag/v0.4.14
25 | True
26 | 0.4.14.0
27 | icon.png
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/ConvNetSharp.Core.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/INet.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Core.Layers;
4 | using ConvNetSharp.Volume;
5 |
6 | namespace ConvNetSharp.Core
7 | {
8 | public interface INet where T : struct, IEquatable, IFormattable
9 | {
10 | T Backward(Volume y);
11 |
12 | Volume Forward(Volume input, bool isTraining = false);
13 |
14 | T GetCostLoss(Volume input, Volume y);
15 |
16 | List> GetParametersAndGradients();
17 |
18 | int[] GetPrediction();
19 | }
20 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/ConvLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class ConvLayer : ConvLayer
6 | {
7 | public ConvLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public ConvLayer(int width, int height, int filterCount) : base(width, height, filterCount)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/DropoutLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace ConvNetSharp.Core.Layers.Double
5 | {
6 | public class DropoutLayer : DropoutLayer
7 | {
8 | public DropoutLayer(double dropoutProbability) : base(dropoutProbability)
9 | {
10 | }
11 |
12 | public DropoutLayer(Dictionary data) : base(data)
13 | {
14 | this.DropProbability = Convert.ToDouble(data["DropProbability"]);
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/FullyConnLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class FullyConnLayer : FullyConnLayer
6 | {
7 | public FullyConnLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public FullyConnLayer(int neuronCount) : base(neuronCount)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/InputLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class InputLayer : InputLayer
6 | {
7 | public InputLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public InputLayer(int inputWidth, int inputHeight, int inputDepth) : base(inputWidth, inputHeight, inputDepth)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/LeakyReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class LeakyReluLayer : LeakyReluLayer
6 | {
7 | public LeakyReluLayer(double alpha) : base(alpha)
8 | {
9 | }
10 |
11 | public LeakyReluLayer(Dictionary data) : base(data)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/PoolLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class PoolLayer : PoolLayer
6 | {
7 | public PoolLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public PoolLayer(int width, int height) : base(width, height)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/RegressionLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class RegressionLayer : RegressionLayer
6 | {
7 | public RegressionLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public RegressionLayer()
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/ReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class ReluLayer : ReluLayer
6 | {
7 | public ReluLayer()
8 | {
9 | }
10 |
11 | public ReluLayer(Dictionary data) : base(data)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/SigmoidLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class SigmoidLayer : SigmoidLayer
6 | {
7 | public SigmoidLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public SigmoidLayer()
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/SoftmaxLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class SoftmaxLayer : SoftmaxLayer
6 | {
7 | public SoftmaxLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public SoftmaxLayer(int classCount) : base(classCount)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Double/TanhLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Double
4 | {
5 | public class TanhLayer : TanhLayer
6 | {
7 | public TanhLayer()
8 | {
9 | }
10 |
11 | public TanhLayer(Dictionary data) : base(data)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/DropoutLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | public class DropoutLayer : LayerBase where T : struct, IEquatable, IFormattable
8 | {
9 | public DropoutLayer(Dictionary data) : base(data)
10 | {
11 | this.DropProbability = (T)Convert.ChangeType(data["DropProbability"], typeof(T));
12 | }
13 |
14 | public DropoutLayer(T dropProbability)
15 | {
16 | this.DropProbability = dropProbability;
17 | }
18 |
19 | public T DropProbability { get; set; }
20 |
21 | public override void Backward(Volume outputGradient)
22 | {
23 | this.OutputActivationGradients = outputGradient;
24 |
25 | this.InputActivationGradients.Clear();
26 |
27 | this.OutputActivation.DropoutGradient(this.InputActivation, this.OutputActivationGradients, this.DropProbability, this.InputActivationGradients);
28 | }
29 |
30 | protected override Volume Forward(Volume input, bool isTraining = false)
31 | {
32 | input.Dropout(isTraining ? this.DropProbability : Ops.Zero, this.OutputActivation);
33 | return this.OutputActivation;
34 | }
35 |
36 | public override Dictionary GetData()
37 | {
38 | var dico = base.GetData();
39 | dico["DropProbability"] = this.DropProbability;
40 | return dico;
41 | }
42 |
43 | public override void Init(int inputWidth, int inputHeight, int inputDepth)
44 | {
45 | base.Init(inputWidth, inputHeight, inputDepth);
46 |
47 | this.OutputDepth = inputDepth;
48 | this.OutputWidth = inputWidth;
49 | this.OutputHeight = inputHeight;
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/IClassificationLayer.cs:
--------------------------------------------------------------------------------
1 | namespace ConvNetSharp.Core.Layers
2 | {
3 | public interface IClassificationLayer
4 | {
5 | int ClassCount { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/IDotProductLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ConvNetSharp.Core.Layers
4 | {
5 | public interface IDotProductLayer where T : struct, IEquatable, IFormattable
6 | {
7 | T BiasPref { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/ILastLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Volume;
3 |
4 | namespace ConvNetSharp.Core.Layers
5 | {
6 | public interface ILastLayer where T : struct, IEquatable, IFormattable
7 | {
8 | void Backward(Volume y, out T loss);
9 | }
10 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/InputLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | public class InputLayer : LayerBase where T : struct, IEquatable, IFormattable
8 | {
9 | public InputLayer(Dictionary data) : base(data)
10 | {
11 | this.OutputWidth = this.InputWidth;
12 | this.OutputHeight = this.InputHeight;
13 | this.OutputDepth = this.InputDepth;
14 | }
15 |
16 | public InputLayer(int inputWidth, int inputHeight, int inputDepth)
17 | {
18 | Init(inputWidth, inputHeight, inputDepth);
19 |
20 | this.OutputWidth = inputWidth;
21 | this.OutputHeight = inputHeight;
22 | this.OutputDepth = inputDepth;
23 | }
24 |
25 | public override void Backward(Volume outputGradient)
26 | {
27 | }
28 |
29 | protected override Volume Forward(Volume input, bool isTraining = false)
30 | {
31 | this.OutputActivation = input;
32 | return this.OutputActivation;
33 | }
34 |
35 | public override Volume Forward(bool isTraining)
36 | {
37 | return this.OutputActivation;
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/LastLayerBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | public abstract class LastLayerBase : LayerBase, ILastLayer where T : struct, IEquatable, IFormattable
8 | {
9 | protected LastLayerBase()
10 | {
11 | }
12 |
13 | protected LastLayerBase(Dictionary data) : base(data)
14 | {
15 | }
16 |
17 | public abstract void Backward(Volume y, out T loss);
18 | }
19 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/LeakyReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | ///
8 | /// Implements LeakyReLU nonlinearity elementwise
9 | /// x -> x > 0, x, otherwise alpha * x
10 | ///
11 | public class LeakyReluLayer : LayerBase where T : struct, IEquatable, IFormattable
12 | {
13 | public LeakyReluLayer(T alpha)
14 | {
15 | this.Alpha = alpha;
16 | }
17 |
18 | public LeakyReluLayer(Dictionary data) : base(data)
19 | {
20 | this.Alpha = (T)Convert.ChangeType(data["Alpha"], typeof(T));
21 | }
22 |
23 | public T Alpha { get; set; }
24 |
25 | public override Dictionary GetData()
26 | {
27 | var dico = base.GetData();
28 |
29 | dico["Alpha"] = this.Alpha;
30 |
31 | return dico;
32 | }
33 |
34 | public override void Backward(Volume outputGradient)
35 | {
36 | this.OutputActivationGradients = outputGradient;
37 | this.OutputActivation.LeakyReluGradient(this.OutputActivationGradients, this.InputActivationGradients, this.Alpha);
38 | }
39 |
40 | protected override Volume Forward(Volume input, bool isTraining = false)
41 | {
42 | input.LeakyRelu(this.Alpha, this.OutputActivation);
43 | return this.OutputActivation;
44 | }
45 |
46 | public override void Init(int inputWidth, int inputHeight, int inputDepth)
47 | {
48 | base.Init(inputWidth, inputHeight, inputDepth);
49 |
50 | this.OutputDepth = inputDepth;
51 | this.OutputWidth = inputWidth;
52 | this.OutputHeight = inputHeight;
53 | }
54 | }
55 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/ParametersAndGradients.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Volume;
3 |
4 | namespace ConvNetSharp.Core.Layers
5 | {
6 | public class ParametersAndGradients where T : struct, IEquatable, IFormattable
7 | {
8 | public Volume Volume { get; set; }
9 |
10 | public Volume Gradient { get; set; }
11 | }
12 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/RegressionLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | ///
8 | /// implements an L2 regression cost layer,
9 | /// so penalizes \sum_i(||x_i - y_i||^2), where x is its input
10 | /// and y is the user-provided array of "correct" values.
11 | /// Input should have a shape of [1, 1, 1, n] where n is the batch size
12 | ///
13 | public class RegressionLayer : LastLayerBase where T : struct, IEquatable, IFormattable
14 | {
15 | private Volume _result;
16 | private Volume _sum;
17 |
18 | public RegressionLayer()
19 | {
20 | }
21 |
22 | public RegressionLayer(Dictionary data) : base(data)
23 | {
24 | }
25 |
26 | public override void Init(int inputWidth, int inputHeight, int inputDepth)
27 | {
28 | base.Init(inputWidth, inputHeight, inputDepth);
29 |
30 | this.OutputWidth = inputWidth;
31 | this.OutputHeight = inputHeight;
32 | this.OutputDepth = inputDepth;
33 | }
34 |
35 | public override void Backward(Volume outputGradient)
36 | {
37 | throw new NotImplementedException();
38 | }
39 |
40 | public override void Backward(Volume y, out T loss)
41 | {
42 | var reshape = y.ReShape(new Shape(1, 1, -1, Shape.Keep));
43 | var dy = this.InputActivationGradients.ReShape(this.OutputActivation.Shape.Dimensions);
44 | reshape.SubtractFrom(this.OutputActivation, dy);
45 |
46 | if (this._result == null)
47 | {
48 | this._result = BuilderInstance.Volume.SameAs(this.OutputActivation.Shape);
49 | this._sum = BuilderInstance.Volume.SameAs(new Shape(1));
50 | }
51 |
52 | this._sum.Clear();
53 | dy.Multiply(dy, this._result); // dy * dy
54 | var half = (T)Convert.ChangeType(0.5, typeof(T));
55 | this._result.Multiply(half, this._result); // dy * dy * 0.5
56 | this._result.Sum(this._sum); // sum over all batch
57 | var batchSize = y.Shape.Dimensions[3];
58 | loss = Ops.Divide(this._sum.Get(0), Ops.Cast(batchSize)); // average
59 | }
60 |
61 | protected override Volume Forward(Volume input, bool isTraining = false)
62 | {
63 | return input;
64 | }
65 | }
66 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/ReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | ///
8 | /// Implements ReLU nonlinearity elementwise
9 | /// x -> max(0, x)
10 | /// the output is in [0, inf)
11 | ///
12 | public class ReluLayer : LayerBase where T : struct, IEquatable, IFormattable
13 | {
14 | public ReluLayer()
15 | {
16 | }
17 |
18 | public ReluLayer(Dictionary data) : base(data)
19 | {
20 | }
21 |
22 | public override void Backward(Volume outputGradient)
23 | {
24 | this.OutputActivationGradients = outputGradient;
25 |
26 | this.OutputActivation.ReluGradient(this.InputActivation,
27 | this.OutputActivationGradients,
28 | this.InputActivationGradients);
29 | }
30 |
31 | protected override Volume Forward(Volume input, bool isTraining = false)
32 | {
33 | input.Relu(this.OutputActivation);
34 | return this.OutputActivation;
35 | }
36 |
37 | public override void Init(int inputWidth, int inputHeight, int inputDepth)
38 | {
39 | base.Init(inputWidth, inputHeight, inputDepth);
40 |
41 | this.OutputDepth = inputDepth;
42 | this.OutputWidth = inputWidth;
43 | this.OutputHeight = inputHeight;
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/SigmoidLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | public class SigmoidLayer : LayerBase where T : struct, IEquatable, IFormattable
8 | {
9 | public SigmoidLayer(Dictionary data) : base(data)
10 | {
11 | }
12 |
13 | public SigmoidLayer()
14 | {
15 | }
16 |
17 | public override void Backward(Volume outputGradient)
18 | {
19 | this.OutputActivationGradients = outputGradient;
20 | this.OutputActivation.SigmoidGradient(this.InputActivation, this.OutputActivationGradients, this.InputActivationGradients);
21 | }
22 |
23 | protected override Volume Forward(Volume input, bool isTraining = false)
24 | {
25 | input.Sigmoid(this.OutputActivation);
26 | return this.OutputActivation;
27 | }
28 |
29 | public override void Init(int inputWidth, int inputHeight, int inputDepth)
30 | {
31 | base.Init(inputWidth, inputHeight, inputDepth);
32 |
33 | this.OutputDepth = inputDepth;
34 | this.OutputWidth = inputWidth;
35 | this.OutputHeight = inputHeight;
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/ConvLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class ConvLayer : ConvLayer
6 | {
7 | public ConvLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public ConvLayer(int width, int height, int filterCount) : base(width, height, filterCount)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/DropoutLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace ConvNetSharp.Core.Layers.Single
5 | {
6 | public class DropoutLayer : DropoutLayer
7 | {
8 | public DropoutLayer(float dropoutProbability) : base(dropoutProbability)
9 | {
10 | }
11 |
12 | public DropoutLayer(Dictionary data) : base(data)
13 | {
14 | this.DropProbability = Convert.ToSingle(data["DropProbability"]);
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/FullyConnLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class FullyConnLayer : FullyConnLayer
6 | {
7 | public FullyConnLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public FullyConnLayer(int neuronCount) : base(neuronCount)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/InputLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class InputLayer : InputLayer
6 | {
7 | public InputLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public InputLayer(int inputWidth, int inputHeight, int inputDepth) : base(inputWidth, inputHeight, inputDepth)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/LeakyReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class LeakyReluLayer : LeakyReluLayer
6 | {
7 | public LeakyReluLayer(float alpha) : base(alpha)
8 | {
9 | }
10 |
11 | public LeakyReluLayer(Dictionary data) : base(data)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/PoolLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class PoolLayer : PoolLayer
6 | {
7 | public PoolLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public PoolLayer(int width, int height) : base(width, height)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/RegressionLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class RegressionLayer : RegressionLayer
6 | {
7 | public RegressionLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public RegressionLayer()
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/ReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class ReluLayer : ReluLayer
6 | {
7 | public ReluLayer()
8 | {
9 |
10 | }
11 |
12 | public ReluLayer(Dictionary data) : base(data)
13 | {
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/SigmoidLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class SigmoidLayer : SigmoidLayer
6 | {
7 | public SigmoidLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public SigmoidLayer()
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/SoftmaxLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class SoftmaxLayer : SoftmaxLayer
6 | {
7 | public SoftmaxLayer(Dictionary data) : base(data)
8 | {
9 | }
10 |
11 | public SoftmaxLayer(int classCount) : base(classCount)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/Single/TanhLayer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ConvNetSharp.Core.Layers.Single
4 | {
5 | public class TanhLayer : TanhLayer
6 | {
7 | public TanhLayer()
8 | {
9 | }
10 |
11 | public TanhLayer(Dictionary data) : base(data)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Layers/TanhLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Layers
6 | {
7 | public class TanhLayer : LayerBase where T : struct, IEquatable, IFormattable
8 | {
9 | public TanhLayer(Dictionary data) : base(data)
10 | {
11 | }
12 |
13 | public TanhLayer()
14 | {
15 | }
16 |
17 | public override void Backward(Volume outputGradient)
18 | {
19 | this.OutputActivationGradients = outputGradient;
20 | this.OutputActivation.TanhGradient(this.InputActivation, this.OutputActivationGradients, this.InputActivationGradients);
21 | }
22 |
23 | protected override Volume Forward(Volume input, bool isTraining = false)
24 | {
25 | input.Tanh(this.OutputActivation);
26 | return this.OutputActivation;
27 | }
28 |
29 | public override void Init(int inputWidth, int inputHeight, int inputDepth)
30 | {
31 | base.Init(inputWidth, inputHeight, inputDepth);
32 |
33 | this.OutputDepth = inputDepth;
34 | this.OutputWidth = inputWidth;
35 | this.OutputHeight = inputHeight;
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Serialization/JObjectExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using Newtonsoft.Json.Linq;
4 |
5 | namespace ConvNetSharp.Core.Serialization
6 | {
7 | public static class JObjectExtensions
8 | {
9 | ///
10 | /// JObject to nested dictionaries
11 | ///
12 | ///
13 | ///
14 | public static Dictionary ToDictionary(this JObject jobject)
15 | {
16 | var dico = new Dictionary();
17 |
18 | foreach (var o in jobject)
19 | {
20 | var oValue = o.Value;
21 |
22 | if (o.Value is JArray jArray)
23 | {
24 | var first = jArray[0]; // use first element to guess if we are dealing with a list of dico or an array
25 | var isValueArray = first is JValue;
26 |
27 | if (isValueArray)
28 | {
29 | var array = jArray.Values().Select(x => ((JValue) x).Value).ToArray();
30 | dico[o.Key] = array;
31 | }
32 | else
33 | {
34 | var list = new List>();
35 | foreach (var token in jArray)
36 | {
37 | var elt = ((JObject) token).ToDictionary();
38 | list.Add(elt);
39 | }
40 |
41 | dico[o.Key] = list;
42 | }
43 | }
44 | else
45 | {
46 | dico[o.Key] = ((JValue) oValue).Value;
47 | }
48 | }
49 |
50 | return dico;
51 | }
52 | }
53 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Serialization/SerializationExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using ConvNetSharp.Core.Fluent;
4 | using Newtonsoft.Json;
5 | using Newtonsoft.Json.Linq;
6 |
7 | namespace ConvNetSharp.Core.Serialization
8 | {
9 | public static class SerializationExtensions
10 | {
11 | public static Net FromJson(string json) where T : struct, IEquatable, IFormattable
12 | {
13 | var data = JsonConvert.DeserializeObject(json);
14 | var dico = data.ToDictionary();
15 | var net = Net.FromData(dico);
16 | return net;
17 | }
18 |
19 | public static T[] ToArrayOfT(this object obj)
20 | {
21 | if (obj is T[] arrayofT)
22 | {
23 | return arrayofT;
24 | }
25 |
26 | if (obj is JArray jarray)
27 | {
28 | return jarray.ToObject();
29 | }
30 |
31 | return ((object[]) obj).Select(o => (T) Convert.ChangeType(o, typeof(T), null)).ToArray();
32 | }
33 |
34 | public static string ToJson(this Net net) where T : struct, IEquatable, IFormattable
35 | {
36 | var data = net.GetData();
37 | var json = JsonConvert.SerializeObject(data);
38 | return json;
39 | }
40 |
41 | public static string ToJson(this FluentNet net) where T : struct, IEquatable, IFormattable
42 | {
43 | var data = net.GetData();
44 | var json = JsonConvert.SerializeObject(data);
45 | return json;
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Training/Double/AdamTrainer.cs:
--------------------------------------------------------------------------------
1 | namespace ConvNetSharp.Core.Training.Double
2 | {
3 | public class AdamTrainer : AdamTrainer
4 | {
5 | public AdamTrainer(INet net) : base(net)
6 | {
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Training/Double/SgdTrainer.cs:
--------------------------------------------------------------------------------
1 | namespace ConvNetSharp.Core.Training.Double
2 | {
3 | public class SgdTrainer : SgdTrainer
4 | {
5 | public SgdTrainer(INet net) : base(net)
6 | {
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Training/Single/AdamTrainer.cs:
--------------------------------------------------------------------------------
1 | namespace ConvNetSharp.Core.Training.Single
2 | {
3 | public class AdamTrainer : AdamTrainer
4 | {
5 | public AdamTrainer(INet net) : base(net)
6 | {
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Training/Single/SgdTrainer.cs:
--------------------------------------------------------------------------------
1 | namespace ConvNetSharp.Core.Training.Single
2 | {
3 | public class SgdTrainer : SgdTrainer
4 | {
5 | public SgdTrainer(INet net) : base(net)
6 | {
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/Training/TrainerBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Core.Training
6 | {
7 | public abstract class TrainerBase where T : struct, IEquatable, IFormattable
8 | {
9 | protected TrainerBase(INet net)
10 | {
11 | this.Net = net;
12 | }
13 |
14 | public INet Net { get; }
15 |
16 | public double BackwardTimeMs { get; protected set; }
17 |
18 | public double ForwardTimeMs { get; protected set; }
19 |
20 | public double UpdateWeightsTimeMs { get; private set; }
21 |
22 | public virtual T Loss { get; protected set; }
23 |
24 | public int BatchSize { get; set; } = 1;
25 |
26 | protected virtual void Backward(Volume y)
27 | {
28 | var chrono = Stopwatch.StartNew();
29 |
30 | var batchSize = y.Shape.Dimensions[3];
31 | this.Loss = Ops.Divide(this.Net.Backward(y), Ops.Cast(batchSize));
32 | this.BackwardTimeMs = chrono.Elapsed.TotalMilliseconds / batchSize;
33 | }
34 |
35 | private void Forward(Volume x)
36 | {
37 | var chrono = Stopwatch.StartNew();
38 | var batchSize = x.Shape.Dimensions[3];
39 | this.Net.Forward(x, true); // also set the flag that lets the net know we're just training
40 | this.ForwardTimeMs = chrono.Elapsed.TotalMilliseconds / batchSize;
41 | }
42 |
43 | public virtual void Train(Volume x, Volume y)
44 | {
45 | this.Forward(x);
46 |
47 | this.Backward(y);
48 |
49 | var batchSize = x.Shape.Dimensions[3];
50 | var chrono = Stopwatch.StartNew();
51 | this.TrainImplem();
52 | this.UpdateWeightsTimeMs = chrono.Elapsed.TotalMilliseconds / batchSize;
53 | }
54 |
55 | protected abstract void TrainImplem();
56 | }
57 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Core/img/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cbovar/ConvNetSharp/ccaf6f6959c12026c4e1a0e396a7711654c92551/src/ConvNetSharp.Core/img/icon.png
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow.Tests/ConvNetSharp.Flow.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | all
15 | runtime; build; native; contentfiles; analyzers; buildtransitive
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow.Tests/DoubleGpuOpTests.cs:
--------------------------------------------------------------------------------
1 | using ConvNetSharp.Flow.Ops;
2 | using ConvNetSharp.Volume;
3 | using ConvNetSharp.Volume.GPU.Double;
4 | using NUnit.Framework;
5 |
6 | namespace ConvNetSharp.Flow.Tests
7 | {
8 | [TestFixture]
9 | public class DoubleGpuOpTests : OpTests
10 | {
11 | public DoubleGpuOpTests()
12 | {
13 | Op.Count = 1;
14 | BuilderInstance.Volume = new VolumeBuilder();
15 | }
16 |
17 | protected override Volume NewVolume(double[] values, Shape shape)
18 | {
19 | return new Volume.GPU.Double.Volume(values, shape);
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow.Tests/DoubleOpTests.cs:
--------------------------------------------------------------------------------
1 | using ConvNetSharp.Flow.Ops;
2 | using ConvNetSharp.Volume;
3 | using ConvNetSharp.Volume.Double;
4 | using NUnit.Framework;
5 |
6 | namespace ConvNetSharp.Flow.Tests
7 | {
8 | [TestFixture]
9 | public class DoubleOpTests : OpTests
10 | {
11 | public DoubleOpTests()
12 | {
13 | Op.Count = 1;
14 | BuilderInstance.Volume = new VolumeBuilder();
15 | }
16 |
17 | protected override Volume NewVolume(double[] values, Shape shape)
18 | {
19 | return BuilderInstance.Volume.From(values, shape);
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow.Tests/SingleGpuOpTests.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using ConvNetSharp.Flow.Ops;
3 | using ConvNetSharp.Volume;
4 | using ConvNetSharp.Volume.GPU.Single;
5 | using NUnit.Framework;
6 |
7 | namespace ConvNetSharp.Flow.Tests
8 | {
9 | [TestFixture]
10 | public class SingleGpuOpTests : OpTests
11 | {
12 | public SingleGpuOpTests()
13 | {
14 | Op.Count = 1;
15 | BuilderInstance.Volume = new VolumeBuilder();
16 | }
17 |
18 | protected override Volume NewVolume(double[] values, Shape shape)
19 | {
20 | var converted = values.Select(i => (float)i).ToArray();
21 | return BuilderInstance.Volume.From(converted, shape);
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow.Tests/SingleOpTests.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using ConvNetSharp.Flow.Ops;
3 | using ConvNetSharp.Volume;
4 | using ConvNetSharp.Volume.Single;
5 | using NUnit.Framework;
6 |
7 | namespace ConvNetSharp.Flow.Tests
8 | {
9 | [TestFixture]
10 | public class SingleOpTests : OpTests
11 | {
12 | public SingleOpTests()
13 | {
14 | Op.Count = 1;
15 | BuilderInstance.Volume = new VolumeBuilder();
16 | }
17 |
18 | protected override Volume NewVolume(double[] values, Shape shape)
19 | {
20 | var converted = values.Select(i => (float)i).ToArray();
21 | return BuilderInstance.Volume.From(converted, shape);
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/ConvNetSharp.Flow.Nuget.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 |
6 |
7 |
8 | 0.4.14
9 | 0.4.14.0
10 | Cedric Bovar
11 | Deep learning in C#
12 | Copyright (c) 2020
13 |
14 |
15 |
16 | Cognitio.ConvNetSharp.Flow
17 | 0.4.14
18 | false
19 | Deep Learning Convolution AI
20 | https://github.com/cbovar/ConvNetSharp/blob/master/LICENSE
21 | https://github.com/cbovar/ConvNetSharp
22 | ConvNetSharp.Flow
23 | ConvNetSharp.Flow
24 | https://github.com/cbovar/ConvNetSharp/releases/tag/v0.4.14
25 | True
26 | ConvNetSharp.Flow
27 | 0.4.14.0
28 | icon.png
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/ConvNetSharp.Flow.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Graph/DifferentiateVisitor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 |
4 | namespace ConvNetSharp.Flow.Graph
5 | {
6 | internal class DifferentiateVisitor : IOpVisitor where T : struct, IEquatable, IFormattable
7 | {
8 | public void Visit(Op op)
9 | {
10 | if (op.Derivate != null)
11 | {
12 | op.Differentiate();
13 | }
14 |
15 | foreach (var parent in op.Parents)
16 | {
17 | var diff = new DifferentiateVisitor();
18 | diff.Visit(parent);
19 | }
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Graph/IOpVisitor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 |
4 | namespace ConvNetSharp.Flow.Graph
5 | {
6 | public interface IOpVisitor where T : struct, IEquatable, IFormattable
7 | {
8 | void Visit(Op op);
9 | }
10 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/ConvLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Flow.Layers
6 | {
7 | public class ConvLayer : LayerBase where T : struct, IEquatable, IFormattable
8 | {
9 | private readonly int _filterCount;
10 | private readonly int _height;
11 | private readonly int _width;
12 | private Variable _bias;
13 | private T _biasPref;
14 | private Convolution _conv;
15 | private bool _initialized;
16 | private int _pad;
17 | private int _stride = 1;
18 |
19 | public ConvLayer(int width, int height, int filterCount)
20 | {
21 | this._width = width;
22 | this._height = height;
23 | this._filterCount = filterCount;
24 | }
25 |
26 | public T BiasPref
27 | {
28 | get => this._biasPref;
29 | set
30 | {
31 | this._biasPref = value;
32 |
33 | if (this._initialized)
34 | {
35 | this._bias.Result = new T[this._filterCount].Populate(this.BiasPref);
36 | }
37 | }
38 | }
39 |
40 | public int Stride
41 | {
42 | get => this._stride;
43 | set
44 | {
45 | this._stride = value;
46 | if (this._initialized)
47 | {
48 | this._conv.Stride = value;
49 | }
50 | }
51 | }
52 |
53 | public int Pad
54 | {
55 | get => this._pad;
56 | set
57 | {
58 | this._pad = value;
59 | if (this._initialized)
60 | {
61 | this._conv.Pad = value;
62 | }
63 | }
64 | }
65 |
66 | public Op Filter => this._conv.Filter;
67 |
68 | public override void AcceptParent(LayerBase parent)
69 | {
70 | base.AcceptParent(parent);
71 |
72 | var cns = parent.Op.Graph;
73 |
74 | using (cns.Scope($"ConvLayer_{this.Id}"))
75 | {
76 | var content = new T[this._filterCount].Populate(this.BiasPref);
77 | this._bias = cns.Variable(BuilderInstance.Volume.From(content, new Shape(1, 1, this._filterCount, 1)), "Bias", true);
78 | this._conv = cns.Conv(parent.Op, this._width, this._height, this._filterCount, this.Stride, this.Pad);
79 | this.Op = this._conv + this._bias;
80 | }
81 |
82 | this._initialized = true;
83 | }
84 | }
85 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/DropoutLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ConvNetSharp.Flow.Layers
4 | {
5 | public class DropoutLayer : LayerBase where T : struct, IEquatable, IFormattable
6 | {
7 | private readonly T _dropoutProbability;
8 |
9 | public DropoutLayer(T dropoutProbability)
10 | {
11 | this._dropoutProbability = dropoutProbability;
12 | }
13 |
14 | public override void AcceptParent(LayerBase parent)
15 | {
16 | base.AcceptParent(parent);
17 | this.Op = parent.Op.Graph.Dropout(parent.Op, this._dropoutProbability);
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/FullyConnLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 | using ConvNetSharp.Volume;
4 |
5 | namespace ConvNetSharp.Flow.Layers
6 | {
7 | public class FullyConnLayer : LayerBase where T : struct, IEquatable, IFormattable
8 | {
9 | private readonly int _neuronCount;
10 | private Variable _bias;
11 | private T _biasPref;
12 | private Dense _conv;
13 | private bool _initialized;
14 |
15 | public FullyConnLayer(int neuronCount)
16 | {
17 | this._neuronCount = neuronCount;
18 | }
19 |
20 | public T BiasPref
21 | {
22 | get => this._biasPref;
23 | set
24 | {
25 | this._biasPref = value;
26 |
27 | if (this._initialized)
28 | {
29 | this._bias.Result = new T[this._neuronCount].Populate(this.BiasPref);
30 | }
31 | }
32 | }
33 |
34 | public Op Filter => this._conv.Filter;
35 |
36 | public override void AcceptParent(LayerBase parent)
37 | {
38 | base.AcceptParent(parent);
39 |
40 | var cns = parent.Op.Graph;
41 |
42 | using (cns.Scope($"FullConnLayer_{this.Id}"))
43 | {
44 | this._bias = cns.Variable(BuilderInstance.Volume.SameAs(new Shape(1, 1, this._neuronCount, 1)), "Bias", true);
45 | this._conv = cns.Dense(cns.Reshape(parent.Op, new Shape(1, 1, -1, Shape.Keep)), this._neuronCount);
46 | this.Op = this._conv + this._bias;
47 | }
48 |
49 | this._initialized = true;
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/ILastLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 |
4 | namespace ConvNetSharp.Flow.Layers
5 | {
6 | public interface ILastLayer where T : struct, IEquatable, IFormattable
7 | {
8 | Op Cost { get; }
9 | }
10 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/InputLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ConvNetSharp.Flow.Layers
4 | {
5 | public class InputLayer : LayerBase where T : struct, IEquatable, IFormattable
6 | {
7 | public InputLayer()
8 | {
9 | var cns = new ConvNetSharp();
10 | this.Op = cns.PlaceHolder("input");
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/LayerBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 |
4 | namespace ConvNetSharp.Flow.Layers
5 | {
6 | public abstract class LayerBase where T : struct, IEquatable, IFormattable
7 | {
8 | public Op Op { get; set; }
9 |
10 | public int Id { get; private set; }
11 |
12 | public virtual void AcceptParent(LayerBase parent)
13 | {
14 | this.Id = parent.Id + 1;
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/LeakyReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ConvNetSharp.Flow.Layers
4 | {
5 | public class LeakyReluLayer : LayerBase where T : struct, IEquatable, IFormattable
6 | {
7 | public LeakyReluLayer(T alpha)
8 | {
9 | this.Alpha = alpha;
10 | }
11 |
12 | public T Alpha { get; set; }
13 |
14 | public override void AcceptParent(LayerBase parent)
15 | {
16 | base.AcceptParent(parent);
17 | this.Op = parent.Op.Graph.LeakyRelu(parent.Op, this.Alpha);
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/OpWrapperLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 |
4 | namespace ConvNetSharp.Flow.Layers
5 | {
6 | public class OpWrapperLayer : LayerBase, ILastLayer where T : struct, IEquatable, IFormattable
7 | {
8 | public OpWrapperLayer(Op root, Op costOp)
9 | {
10 | this.Op = root;
11 | this.Cost = costOp;
12 | }
13 |
14 | public Op Cost { get; }
15 | }
16 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/PoolLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ConvNetSharp.Flow.Ops;
3 |
4 | namespace ConvNetSharp.Flow.Layers
5 | {
6 | public class PoolLayer : LayerBase where T : struct, IEquatable, IFormattable
7 | {
8 | private readonly int _height;
9 | private readonly int _width;
10 | private bool _initialized;
11 | private int _pad;
12 | private Pool _pool;
13 | private int _stride = 1;
14 |
15 | public PoolLayer(int width, int height)
16 | {
17 | this._width = width;
18 | this._height = height;
19 | }
20 |
21 | public int Stride
22 | {
23 | get => this._stride;
24 | set
25 | {
26 | this._stride = value;
27 | if (this._initialized)
28 | {
29 | this._pool.HorizontalStride = value;
30 | this._pool.VerticalStride = value;
31 | }
32 | }
33 | }
34 |
35 | public int Pad
36 | {
37 | get => this._pad;
38 | set
39 | {
40 | this._pad = value;
41 | if (this._initialized)
42 | {
43 | this._pool.HorizontalPad = value;
44 | this._pool.VerticalPad = value;
45 | }
46 | }
47 | }
48 |
49 | public override void AcceptParent(LayerBase parent)
50 | {
51 | base.AcceptParent(parent);
52 | this._pool = parent.Op.Graph.Pool(parent.Op, this._width, this._height, this.Pad, this.Pad, this.Stride, this.Stride);
53 | this.Op = this._pool;
54 |
55 | this._initialized = true;
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/ReluLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ConvNetSharp.Flow.Layers
4 | {
5 | public class ReluLayer : LayerBase where T : struct, IEquatable, IFormattable
6 | {
7 | public override void AcceptParent(LayerBase parent)
8 | {
9 | base.AcceptParent(parent);
10 | this.Op = parent.Op.Graph.Relu(parent.Op);
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/ConvNetSharp.Flow/Layers/SigmoidLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ConvNetSharp.Flow.Layers
4 | {
5 | public class SigmoidLayer