├── .gitignore ├── KerasModeltoJSON.py ├── LICENSE ├── NNSharp.sln ├── NNSharp ├── App.config ├── BindingCore │ └── NativeFunctions_Tensors.cs ├── DataTypes │ ├── Data2D.cs │ ├── DataArray.cs │ ├── IData.cs │ └── SequentialModelData.cs ├── IO │ ├── PersistSequentialModel.cs │ └── ReaderKerasModel.cs ├── KernelDescriptors │ ├── AvgPooling1D.cs │ ├── AvgPooling2D.cs │ ├── BatchNormalization.cs │ ├── Bias2D.cs │ ├── Convolution1D.cs │ ├── Convolution2D.cs │ ├── Cropping1D.cs │ ├── Cropping2D.cs │ ├── Dense2D.cs │ ├── Dropout.cs │ ├── ELu.cs │ ├── Flatten.cs │ ├── GRU.cs │ ├── GlobalAvgPooling1D.cs │ ├── GlobalAvgPooling2D.cs │ ├── GlobalMaxPooling1D.cs │ ├── GlobalMaxPooling2D.cs │ ├── HardSigmoid.cs │ ├── IKernelDescriptor.cs │ ├── Input2D.cs │ ├── LSTM.cs │ ├── LeakyReLu.cs │ ├── MaxPooling1D.cs │ ├── MaxPooling2D.cs │ ├── MinPooling2D.cs │ ├── Permute.cs │ ├── ReLu.cs │ ├── RepeatVector.cs │ ├── Reshape2D.cs │ ├── Sigmoid.cs │ ├── SimpleRNN.cs │ ├── SoftPlus.cs │ ├── SoftSign.cs │ ├── Softmax.cs │ └── TanH.cs ├── Kernels │ ├── CPUKernels │ │ ├── AvgPool1DKernel.cs │ │ ├── AvgPool2DKernel.cs │ │ ├── BatchNormKernel.cs │ │ ├── Bias2DKernel.cs │ │ ├── Conv1DKernel.cs │ │ ├── Conv2DKernel.cs │ │ ├── Cropping1DKernel.cs │ │ ├── Cropping2DKernel.cs │ │ ├── Dense2DKernel.cs │ │ ├── DropoutKernel.cs │ │ ├── ELuKernel.cs │ │ ├── FlattenKernel.cs │ │ ├── GRUKernel.cs │ │ ├── GlobalAvgPool1DKernel.cs │ │ ├── GlobalAvgPool2DKernel.cs │ │ ├── GlobalMaxPool1DKernel.cs │ │ ├── GlobalMaxPool2DKernel.cs │ │ ├── HardSigmoidKernel.cs │ │ ├── LSTMKernel.cs │ │ ├── LeakyReLuKernel.cs │ │ ├── MaxPool1DKernel.cs │ │ ├── MaxPool2DKernel.cs │ │ ├── MinPool2DKernel.cs │ │ ├── PermuteKernel.cs │ │ ├── ReLuKernel.cs │ │ ├── RepeatVectorKernel.cs │ │ ├── Reshape2DKernel.cs │ │ ├── SigmoidKernel.cs │ │ ├── SimpleRNNKernel.cs │ │ ├── SoftPlusKernel.cs │ │ ├── SoftmaxKernel.cs │ │ ├── SoftsignKernel.cs │ │ └── TanHKernel.cs │ └── IKernel.cs ├── Models │ ├── GraphModel.cs │ ├── IModel.cs │ └── SequentialModel.cs ├── NNSharp.csproj ├── Program.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ └── Resources.resx ├── SequentialBased │ ├── SequentialExecutors │ │ ├── DeafultAbstractLayerFactory.cs │ │ ├── DefaultExecutor.cs │ │ ├── IAbstractLayerFactory.cs │ │ └── ISequentialExecutor.cs │ └── SequentialLayers │ │ ├── AvgPool1DLayer.cs │ │ ├── AvgPool1DLayerFactory.cs │ │ ├── AvgPool2DLayer.cs │ │ ├── AvgPool2DLayerFactory.cs │ │ ├── BatchNormLayer.cs │ │ ├── BatchNormLayerFactory.cs │ │ ├── Bias2DLayer.cs │ │ ├── Bias2DLayerFactory.cs │ │ ├── Conv1DLayer.cs │ │ ├── Conv1DLayerFactory.cs │ │ ├── Conv2DLayer.cs │ │ ├── Conv2DLayerFactory.cs │ │ ├── Cropping1DLayer.cs │ │ ├── Cropping1DLayerFactory.cs │ │ ├── Cropping2DLayer.cs │ │ ├── Cropping2DLayerFactory.cs │ │ ├── Dense2DLayer.cs │ │ ├── Dense2DLayerFactory.cs │ │ ├── DropoutLayer.cs │ │ ├── DropoutLayerFactory.cs │ │ ├── ELuLayer.cs │ │ ├── ELuLayerFactory.cs │ │ ├── FlattenLayer.cs │ │ ├── FlattenLayerFactory.cs │ │ ├── GRULAyer.cs │ │ ├── GRULayerFactory.cs │ │ ├── GlobalAvgPool1DLayer.cs │ │ ├── GlobalAvgPool1DLayerFactory.cs │ │ ├── GlobalAvgPool2DLayer.cs │ │ ├── GlobalAvgPool2DLayerFactory.cs │ │ ├── GlobalMaxPool1DLayer.cs │ │ ├── GlobalMaxPool1DLayerFactory.cs │ │ ├── GlobalMaxPool2DLayer.cs │ │ ├── GlobalMaxPool2DLayerFactory.cs │ │ ├── HardSigmoidLayer.cs │ │ ├── HardSigmoidLayerFactory.cs │ │ ├── ILayer.cs │ │ ├── ILayerFactory.cs │ │ ├── Input2DLayer.cs │ │ ├── Input2DLayerFactory.cs │ │ ├── LSTMLayer.cs │ │ ├── LSTMLayerFactory.cs │ │ ├── LeakyReLuLayer.cs │ │ ├── LeakyReLuLayerFactory.cs │ │ ├── MaxPool1DLayer.cs │ │ ├── MaxPool1DLayerFactory.cs │ │ ├── MaxPool2DLayer.cs │ │ ├── MaxPool2DLayerFactory.cs │ │ ├── MinPool2DLayer.cs │ │ ├── MinPool2DLayerFactory.cs │ │ ├── PermuteLayer.cs │ │ ├── PermuteLayerFactory.cs │ │ ├── ReLuLayer.cs │ │ ├── ReLuLayerFactory.cs │ │ ├── RepeatVectorLayer.cs │ │ ├── RepeatVectorLayerFactory.cs │ │ ├── Reshape2DLayer.cs │ │ ├── Reshape2DLayerFactory.cs │ │ ├── SigmoidLayer.cs │ │ ├── SigmoidLayerFactory.cs │ │ ├── SimpleRNNLayer.cs │ │ ├── SimpleRNNLayerFactory.cs │ │ ├── SoftPlusLayer.cs │ │ ├── SoftPlusLayerFactory.cs │ │ ├── SoftmaxLayer.cs │ │ ├── SoftmaxLayerFactory.cs │ │ ├── SoftsignLayer.cs │ │ ├── SoftsignLayerFactory.cs │ │ ├── TanHLayer.cs │ │ └── TanHLayerFactory.cs └── packages.config ├── PythonUtils ├── BatchNormalization.py ├── Dropout.py ├── FastTest.py ├── GRU.py ├── KerasModeltoJSON.py ├── KerasTestGenerator.py ├── LSTM.py ├── RLmodel.py ├── RepeatVector.py ├── SimpleRNN.py └── tests │ ├── test_avgpool_1D_1_input.json │ ├── test_avgpool_1D_1_model.json │ ├── test_avgpool_1D_1_output.json │ ├── test_avgpool_1D_2_input.json │ ├── test_avgpool_1D_2_model.json │ ├── test_avgpool_1D_2_output.json │ ├── test_avgpool_2D_1_input.json │ ├── test_avgpool_2D_1_model.json │ ├── test_avgpool_2D_1_output.json │ ├── test_avgpool_2D_2_input.json │ ├── test_avgpool_2D_2_model.json │ ├── test_avgpool_2D_2_output.json │ ├── test_batchnorm_input.json │ ├── test_batchnorm_model.json │ ├── test_batchnorm_output.json │ ├── test_conv_1D_1_input.json │ ├── test_conv_1D_1_model.json │ ├── test_conv_1D_1_output.json │ ├── test_conv_1D_2_input.json │ ├── test_conv_1D_2_model.json │ ├── test_conv_1D_2_output.json │ ├── test_conv_2D_1_input.json │ ├── test_conv_2D_1_model.json │ ├── test_conv_2D_1_output.json │ ├── test_conv_2D_2_input.json │ ├── test_conv_2D_2_model.json │ ├── test_conv_2D_2_output.json │ ├── test_crop_1D_input.json │ ├── test_crop_1D_model.json │ ├── test_crop_1D_output.json │ ├── test_crop_2D_input.json │ ├── test_crop_2D_model.json │ ├── test_crop_2D_output.json │ ├── test_dense_input.json │ ├── test_dense_model.json │ ├── test_dense_output.json │ ├── test_dropout_input.json │ ├── test_dropout_model.json │ ├── test_dropout_output.json │ ├── test_elu_input.json │ ├── test_elu_model.json │ ├── test_elu_output.json │ ├── test_flat_input.json │ ├── test_flat_model.json │ ├── test_flat_output.json │ ├── test_globalavgpool_1D_input.json │ ├── test_globalavgpool_1D_model.json │ ├── test_globalavgpool_1D_output.json │ ├── test_globalavgpool_2D_input.json │ ├── test_globalavgpool_2D_model.json │ ├── test_globalavgpool_2D_output.json │ ├── test_globalmaxpool_1D_input.json │ ├── test_globalmaxpool_1D_model.json │ ├── test_globalmaxpool_1D_output.json │ ├── test_globalmaxpool_2D_input.json │ ├── test_globalmaxpool_2D_model.json │ ├── test_globalmaxpool_2D_output.json │ ├── test_gru_input.json │ ├── test_gru_model.json │ ├── test_gru_output.json │ ├── test_hard_sigmoid_input.json │ ├── test_hard_sigmoid_model.json │ ├── test_hard_sigmoid_output.json │ ├── test_lstm_input.json │ ├── test_lstm_model.json │ ├── test_lstm_output.json │ ├── test_maxpool_1D_1_input.json │ ├── test_maxpool_1D_1_model.json │ ├── test_maxpool_1D_1_output.json │ ├── test_maxpool_1D_2_input.json │ ├── test_maxpool_1D_2_model.json │ ├── test_maxpool_1D_2_output.json │ ├── test_maxpool_2D_1_input.json │ ├── test_maxpool_2D_1_model.json │ ├── test_maxpool_2D_1_output.json │ ├── test_maxpool_2D_2_input.json │ ├── test_maxpool_2D_2_model.json │ ├── test_maxpool_2D_2_output.json │ ├── test_permute_input.json │ ├── test_permute_model.json │ ├── test_permute_output.json │ ├── test_relu_input.json │ ├── test_relu_model.json │ ├── test_relu_output.json │ ├── test_repeatvector_input.json │ ├── test_repeatvector_model.json │ ├── test_repeatvector_output.json │ ├── test_reshape_input.json │ ├── test_reshape_model.json │ ├── test_reshape_output.json │ ├── test_sigmoid_input.json │ ├── test_sigmoid_model.json │ ├── test_sigmoid_output.json │ ├── test_simplernn_input.json │ ├── test_simplernn_model.json │ ├── test_simplernn_output.json │ ├── test_softmax_input.json │ ├── test_softmax_model.json │ ├── test_softmax_output.json │ ├── test_softplus_input.json │ ├── test_softplus_model.json │ ├── test_softplus_output.json │ ├── test_softsign_input.json │ ├── test_softsign_model.json │ ├── test_softsign_output.json │ ├── test_tanh_input.json │ ├── test_tanh_model.json │ └── test_tanh_output.json ├── README.md ├── UnitTests ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ └── Resources.resx ├── TestAvgPool1D.cs ├── TestAvgPool2D.cs ├── TestBatchNormalization.cs ├── TestBias2D.cs ├── TestConv1D.cs ├── TestConv2D.cs ├── TestCropping1D.cs ├── TestCropping2D.cs ├── TestData2D.cs ├── TestDataArray.cs ├── TestDense2D.cs ├── TestDropout.cs ├── TestELu.cs ├── TestGLobalMaxPool1D.cs ├── TestGRU.cs ├── TestGlobalAvgPool1D.cs ├── TestGlobalAvgPool2D.cs ├── TestGlobalMaxPool2D.cs ├── TestHardSigmoid.cs ├── TestInput2D.cs ├── TestLSTM.cs ├── TestLeakyReLu.cs ├── TestMaxPool1D.cs ├── TestMaxPool2D.cs ├── TestMinPool2D.cs ├── TestPermute.cs ├── TestReLu.cs ├── TestRepeatVector.cs ├── TestReshape2D.cs ├── TestSigmoid.cs ├── TestSimpleRNN.cs ├── TestSoftPlus.cs ├── TestSoftmax.cs ├── TestSoftsign.cs ├── TestTanH.cs ├── UnitTests.csproj ├── Utils.cs └── packages.config ├── cppcore ├── CMakeLists.txt ├── README.md ├── implementation_cntk │ ├── GenCNTK.cmake │ └── cppcore.cpp ├── implementation_tf │ ├── GenTF.cmake │ └── cppcore.cpp ├── nnsharp.h └── thirdparty │ ├── FindCNTK.cmake │ └── FindTF.cmake └── docs ├── custom_theme ├── css │ └── theme.css └── img │ └── favicon.ico ├── docs ├── activations.md ├── batchnormalization.md ├── convolutions.md ├── core.md ├── data.md ├── index.md ├── install.md ├── models.md ├── pooling.md ├── recurrents.md ├── startkeras.md └── starttensorflow.md └── mkdocs.yml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NNSharp/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /NNSharp/BindingCore/NativeFunctions_Tensors.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Runtime.InteropServices; 7 | using NNSharp.Properties; 8 | 9 | namespace NNSharp.BindingCore 10 | { 11 | static public partial class NativeFunctions 12 | { 13 | const string CoreDll = @"D:\ArtificialIntelligence\MachineLearning\git\NNSharp\cppcore\generated\Debug\cppcore.dll"; 14 | 15 | [DllImport(CoreDll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 16 | public static extern IntPtr create_tensor_integer(int size); 17 | 18 | [DllImport(CoreDll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 19 | public static extern int tensor_integer_get(IntPtr tensor_in, int idx); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /NNSharp/DataTypes/DataArray.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.DataTypes 9 | { 10 | [Serializable()] 11 | public class DataArray : IData, IEnumerable 12 | { 13 | 14 | public DataArray(int length) 15 | { 16 | this.length = length; 17 | array = new double[length]; 18 | ToZeros(); 19 | } 20 | 21 | public double this[int idx] 22 | { 23 | get { return array[idx];} 24 | 25 | set { array[idx] = value; } 26 | } 27 | 28 | public void ApplyToAll(Operation operation) 29 | { 30 | for (int i = 0; i < length; ++i) 31 | array[i] = operation(array[i]); 32 | } 33 | 34 | public void ToZeros() 35 | { 36 | for (int i = 0; i < length; ++i) 37 | array[i] = 0.0; 38 | } 39 | 40 | public IEnumerator GetEnumerator() 41 | { 42 | var ret = ((IEnumerable)array).GetEnumerator(); 43 | return ret; 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | var ret = (IEnumerator)array.GetEnumerator(); 49 | return ret; 50 | } 51 | 52 | public int GetLength() 53 | { 54 | return length; 55 | } 56 | 57 | private double[] array; 58 | private int length; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /NNSharp/DataTypes/IData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.DataTypes 8 | { 9 | public delegate double Operation(double current); 10 | 11 | public interface IData 12 | { 13 | void ApplyToAll(Operation operation); 14 | 15 | void ToZeros(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /NNSharp/IO/PersistSequentialModel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Runtime.Serialization.Formatters.Binary; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace NNSharp.IO 11 | { 12 | public class PersistSequentialModel 13 | { 14 | public static void SerializeModel(SequentialModel model, string fileName) 15 | { 16 | Stream stream = File.Create(fileName); 17 | BinaryFormatter serializer = new BinaryFormatter(); 18 | serializer.Serialize(stream, model); 19 | stream.Close(); 20 | } 21 | 22 | public static SequentialModel DeserializeModel(string fileName) 23 | { 24 | SequentialModel model = null; 25 | if (File.Exists(fileName)) 26 | { 27 | Stream stream = File.OpenRead(fileName); 28 | BinaryFormatter deserializer = new BinaryFormatter(); 29 | model = (SequentialModel)deserializer.Deserialize(stream); 30 | stream.Close(); 31 | } 32 | else 33 | throw new Exception("Trying to serialize a non-existing file."); 34 | 35 | return model; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/AvgPooling1D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class AvgPooling1D : IKernelDescriptor 10 | { 11 | public AvgPooling1D(int padding, int stride, int kernelSize) 12 | { 13 | this.padding = padding; 14 | this.stride = stride; 15 | this.kernelSize = kernelSize; 16 | } 17 | 18 | public int Padding { get { return padding; } } 19 | public int Stride { get { return stride; } } 20 | public int KernelSize { get { return kernelSize; } } 21 | 22 | private int padding; 23 | private int stride; 24 | private int kernelSize; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/AvgPooling2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class AvgPooling2D : IKernelDescriptor 10 | { 11 | public AvgPooling2D(int paddingVertical, int paddingHorizontal, 12 | int strideVertical, int strideHorizontal, 13 | int kernelHeight, int kernelWidth) 14 | { 15 | this.paddingVertical = paddingVertical; 16 | this.paddingHorizontal = paddingHorizontal; 17 | this.strideVertical = strideVertical; 18 | this.strideHorizontal = strideHorizontal; 19 | 20 | this.kernelHeight = kernelHeight; 21 | this.kernelWidth = kernelWidth; 22 | } 23 | 24 | public int PaddingVertical { get { return paddingVertical; } } 25 | public int PaddingHorizontal { get { return paddingHorizontal; } } 26 | public int StrideVertical { get { return strideVertical; } } 27 | public int StrideHorizontal { get { return strideHorizontal; } } 28 | 29 | public int KernelHeight { get { return kernelHeight; } } 30 | public int KernelWidth { get { return kernelWidth; } } 31 | 32 | 33 | private int paddingVertical; 34 | private int paddingHorizontal; 35 | private int strideVertical; 36 | private int strideHorizontal; 37 | 38 | private int kernelHeight; 39 | private int kernelWidth; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/BatchNormalization.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class BatchNormalization : IKernelDescriptor 10 | { 11 | public BatchNormalization(double epsilon) 12 | { 13 | this.epsilon = epsilon; 14 | } 15 | 16 | public double Epsilon { get { return epsilon; } } 17 | 18 | private double epsilon; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Bias2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Bias2D : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Convolution1D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Convolution1D : IKernelDescriptor 10 | { 11 | public Convolution1D(int padding, int stride, int kernelSize, int kernelNum) 12 | { 13 | this.padding = padding; 14 | this.stride = stride; 15 | this.kernelSize = kernelSize; 16 | this.kernelNum = kernelNum; 17 | 18 | } 19 | 20 | public int Padding { get { return padding; } } 21 | public int Stride { get { return stride; } } 22 | 23 | public int KernelSize { get { return kernelSize; } } 24 | public int KernelNum { get { return kernelNum; } } 25 | 26 | 27 | private int padding; 28 | private int stride; 29 | private int kernelSize; 30 | private int kernelNum; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Convolution2D.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.KernelDescriptors 9 | { 10 | public class Convolution2D : IKernelDescriptor 11 | { 12 | public Convolution2D(int paddingVertical, int paddingHorizontal, 13 | int strideVertical, int strideHorizontal, 14 | int kernelHeight, int kernelWidth, int kernelNum) 15 | { 16 | this.paddingVertical = paddingVertical; 17 | this.paddingHorizontal = paddingHorizontal; 18 | this.strideVertical = strideVertical; 19 | this.strideHorizontal = strideHorizontal; 20 | 21 | this.kernelHeight = kernelHeight; 22 | this.kernelWidth = kernelWidth; 23 | this.kernelNum = kernelNum; 24 | 25 | } 26 | 27 | public int PaddingVertical { get { return paddingVertical; } } 28 | public int PaddingHorizontal { get { return paddingHorizontal; } } 29 | public int StrideVertical { get { return strideVertical; } } 30 | public int StrideHorizontal { get { return strideHorizontal; } } 31 | 32 | public int KernelHeight { get { return kernelHeight; } } 33 | public int KernelWidth { get { return kernelWidth; } } 34 | public int KernelNum { get { return kernelNum; } } 35 | 36 | 37 | private int paddingVertical; 38 | private int paddingHorizontal; 39 | private int strideVertical; 40 | private int strideHorizontal; 41 | 42 | private int kernelHeight; 43 | private int kernelWidth; 44 | private int kernelNum; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Cropping1D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Cropping1D : IKernelDescriptor 10 | { 11 | public Cropping1D(int trimBegin, int trimEnd) 12 | { 13 | this.trimBegin = trimBegin; 14 | this.trimEnd = trimEnd; 15 | } 16 | 17 | public int TrimBegin { get { return trimBegin; } } 18 | public int TrimEnd { get { return trimEnd; } } 19 | 20 | private int trimBegin; 21 | private int trimEnd; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Cropping2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Cropping2D : IKernelDescriptor 10 | { 11 | public Cropping2D(int topTrim, int bottomTrim, int leftTrim, int rightTrim) 12 | { 13 | this.topTrim = topTrim; 14 | this.bottomTrim = bottomTrim; 15 | this.leftTrim = leftTrim; 16 | this.rightTrim = rightTrim; 17 | } 18 | 19 | public int TopTrim { get { return topTrim; } } 20 | public int BottomTrim { get { return bottomTrim; } } 21 | public int LeftTrim { get { return leftTrim; } } 22 | public int RightTrim { get { return rightTrim; } } 23 | 24 | private int topTrim; 25 | private int bottomTrim; 26 | private int leftTrim; 27 | private int rightTrim; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Dense2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Dense2D : IKernelDescriptor 10 | { 11 | public Dense2D(int units) 12 | { 13 | this.units = units; 14 | } 15 | 16 | public int Units { get { return units; } } 17 | 18 | private int units; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Dropout.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.KernelDescriptors 9 | { 10 | public class Dropout : IKernelDescriptor 11 | { 12 | public Dropout(double rate, Data2D noiseShape) 13 | { 14 | this.rate = rate; 15 | this.noiseShape = noiseShape; 16 | } 17 | 18 | public double Rate { get { return rate; } } 19 | public Data2D NoiseShape { get { return noiseShape; } } 20 | 21 | private double rate; 22 | private Data2D noiseShape; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/ELu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class ELu : IKernelDescriptor 10 | { 11 | public ELu(double alpha) 12 | { 13 | this.alpha = alpha; 14 | } 15 | 16 | public double Alpha { get { return alpha; } } 17 | private double alpha; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Flatten.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Flatten : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/GRU.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.Kernels; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.KernelDescriptors 9 | { 10 | public class GRU : IKernelDescriptor 11 | { 12 | public GRU(int units, int inputDim, ActivationLambda activation, 13 | ActivationLambda recurrentActivation) 14 | { 15 | this.units = units; 16 | this.inputDim = inputDim; 17 | this.activation = activation; 18 | this.recurrentActivation = recurrentActivation; 19 | } 20 | 21 | public int Units { get { return units; } } 22 | public int InputDim { get { return inputDim; } } 23 | public ActivationLambda Activation { get { return activation; } } 24 | public ActivationLambda RecurrentActivation { get { return recurrentActivation; } } 25 | 26 | private int units; 27 | private int inputDim; 28 | private ActivationLambda activation; 29 | private ActivationLambda recurrentActivation; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/GlobalAvgPooling1D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class GlobalAvgPooling1D : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/GlobalAvgPooling2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class GlobalAvgPooling2D : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/GlobalMaxPooling1D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class GlobalMaxPooling1D : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/GlobalMaxPooling2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class GlobalMaxPooling2D : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/HardSigmoid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class HardSigmoid : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/IKernelDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public interface IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Input2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Input2D : IKernelDescriptor 10 | { 11 | public Input2D(int height, int width, int channels, int batch) 12 | { 13 | this.height = height; 14 | this.width = width; 15 | this.channels = channels; 16 | this.batch = batch; 17 | } 18 | 19 | public int Height { get { return height; } } 20 | public int Width { get { return width; } } 21 | public int Channels { get { return channels; } } 22 | public int Batch { get { return batch; } } 23 | 24 | private int height; 25 | private int width; 26 | private int channels; 27 | private int batch; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/LSTM.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.Kernels; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.KernelDescriptors 9 | { 10 | public class LSTM : IKernelDescriptor 11 | { 12 | public LSTM(int units, int inputDim, ActivationLambda activation, 13 | ActivationLambda recurrentActivation) 14 | { 15 | this.units = units; 16 | this.inputDim = inputDim; 17 | this.activation = activation; 18 | this.recurrentActivation = recurrentActivation; 19 | } 20 | 21 | public int Units { get { return units; } } 22 | public int InputDim { get { return inputDim; } } 23 | public ActivationLambda Activation { get { return activation; } } 24 | public ActivationLambda RecurrentActivation { get { return recurrentActivation; } } 25 | 26 | private int units; 27 | private int inputDim; 28 | private ActivationLambda activation; 29 | private ActivationLambda recurrentActivation; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/LeakyReLu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class LeakyReLu : IKernelDescriptor 10 | { 11 | 12 | public LeakyReLu(double alpha) 13 | { 14 | this.alpha = alpha; 15 | } 16 | 17 | public double Alpha { get { return alpha; } } 18 | private double alpha; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/MaxPooling1D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class MaxPooling1D : IKernelDescriptor 10 | { 11 | public MaxPooling1D(int padding, int stride, int kernelSize) 12 | { 13 | this.padding = padding; 14 | this.stride = stride; 15 | this.kernelSize = kernelSize; 16 | } 17 | 18 | public int Padding { get { return padding; } } 19 | public int Stride { get { return stride; } } 20 | public int KernelSize { get { return kernelSize; } } 21 | 22 | private int padding; 23 | private int stride; 24 | private int kernelSize; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/MaxPooling2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class MaxPooling2D : IKernelDescriptor 10 | { 11 | public MaxPooling2D(int paddingVertical, int paddingHorizontal, 12 | int strideVertical, int strideHorizontal, 13 | int kernelHeight, int kernelWidth) 14 | { 15 | this.paddingVertical = paddingVertical; 16 | this.paddingHorizontal = paddingHorizontal; 17 | this.strideVertical = strideVertical; 18 | this.strideHorizontal = strideHorizontal; 19 | 20 | this.kernelHeight = kernelHeight; 21 | this.kernelWidth = kernelWidth; 22 | } 23 | 24 | public int PaddingVertical { get { return paddingVertical; } } 25 | public int PaddingHorizontal { get { return paddingHorizontal; } } 26 | public int StrideVertical { get { return strideVertical; } } 27 | public int StrideHorizontal { get { return strideHorizontal; } } 28 | 29 | public int KernelHeight { get { return kernelHeight; } } 30 | public int KernelWidth { get { return kernelWidth; } } 31 | 32 | 33 | private int paddingVertical; 34 | private int paddingHorizontal; 35 | private int strideVertical; 36 | private int strideHorizontal; 37 | 38 | private int kernelHeight; 39 | private int kernelWidth; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/MinPooling2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class MinPooling2D : IKernelDescriptor 10 | { 11 | public MinPooling2D(int paddingVertical, int paddingHorizontal, 12 | int strideVertical, int strideHorizontal, 13 | int kernelHeight, int kernelWidth) 14 | { 15 | this.paddingVertical = paddingVertical; 16 | this.paddingHorizontal = paddingHorizontal; 17 | this.strideVertical = strideVertical; 18 | this.strideHorizontal = strideHorizontal; 19 | 20 | this.kernelHeight = kernelHeight; 21 | this.kernelWidth = kernelWidth; 22 | } 23 | 24 | public int PaddingVertical { get { return paddingVertical; } } 25 | public int PaddingHorizontal { get { return paddingHorizontal; } } 26 | public int StrideVertical { get { return strideVertical; } } 27 | public int StrideHorizontal { get { return strideHorizontal; } } 28 | 29 | public int KernelHeight { get { return kernelHeight; } } 30 | public int KernelWidth { get { return kernelWidth; } } 31 | 32 | 33 | private int paddingVertical; 34 | private int paddingHorizontal; 35 | private int strideVertical; 36 | private int strideHorizontal; 37 | 38 | private int kernelHeight; 39 | private int kernelWidth; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Permute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Permute : IKernelDescriptor 10 | { 11 | // Batch remains unchanged. 12 | public Permute(int dim1, int dim2, int dim3) 13 | { 14 | this.dim1 = dim1; 15 | this.dim2 = dim2; 16 | this.dim3 = dim3; 17 | } 18 | 19 | public int Dim1 { get { return dim1; } } 20 | public int Dim2 { get { return dim2; } } 21 | public int Dim3 { get { return dim3; } } 22 | 23 | private int dim1; 24 | private int dim2; 25 | private int dim3; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/ReLu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class ReLu : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/RepeatVector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class RepeatVector : IKernelDescriptor 10 | { 11 | public RepeatVector(int num) 12 | { 13 | this.num = num; 14 | } 15 | 16 | public int Num { get { return num; } } 17 | 18 | private int num; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Reshape2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Reshape2D : IKernelDescriptor 10 | { 11 | public Reshape2D(int height, int width, int channel, int batch) 12 | { 13 | // The paramters of the new shape. 14 | this.height = height; 15 | this.width = width; 16 | this.channel = channel; 17 | this.batch = batch; 18 | } 19 | 20 | public int Height { get { return height; } } 21 | public int Width { get { return width; } } 22 | public int Channel { get { return channel; } } 23 | public int Batch { get { return batch; } } 24 | 25 | 26 | private int height; 27 | private int width; 28 | private int channel; 29 | private int batch; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Sigmoid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Sigmoid : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/SimpleRNN.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace NNSharp.KernelDescriptors 10 | { 11 | public class SimpleRNN : IKernelDescriptor 12 | { 13 | public SimpleRNN(int units, int inputDim, ActivationLambda lambda) 14 | { 15 | this.units = units; 16 | this.inputDim = inputDim; 17 | this.lambda = lambda; 18 | } 19 | 20 | public int Units { get { return units; } } 21 | public int InputDim { get { return inputDim; } } 22 | public ActivationLambda Lambda { get { return lambda; } } 23 | 24 | private int units; 25 | private int inputDim; 26 | private ActivationLambda lambda; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/SoftPlus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class SoftPlus : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/SoftSign.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Softsign : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/Softmax.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class Softmax : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/KernelDescriptors/TanH.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.KernelDescriptors 8 | { 9 | public class TanH : IKernelDescriptor 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/AvgPool1DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class AvgPool1DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | Dimension dimO = output.GetDimension(); 18 | int stH = 0; 19 | 20 | for (int batch = 0; batch < dimO.b; ++batch) 21 | { 22 | for (int channel = 0; channel < dimO.c; ++channel) 23 | { 24 | for (int l = 0; l < dimO.w; ++l) 25 | { 26 | stH = l * stride - padding; 27 | output[0, l, channel, batch] = 0.0; 28 | 29 | for (int idx = stH; idx < stH + kernelDim.w; ++idx) 30 | { 31 | output[0, l, channel, batch] += input[0, idx, channel, batch]; 32 | } 33 | 34 | output[0, l, channel, batch] = output[0, l, channel, batch] / (kernelDim.w); 35 | } 36 | } 37 | } 38 | } 39 | 40 | protected Data2D input; 41 | protected Data2D output; 42 | 43 | protected int padding; 44 | protected int stride; 45 | 46 | protected Dimension kernelDim; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/AvgPool2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class AvgPool2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | Dimension dimO = output.GetDimension(); 18 | int stH = 0; 19 | int stV = 0; 20 | 21 | for (int batch = 0; batch < dimO.b; ++batch) 22 | { 23 | for (int channel = 0; channel < dimO.c; ++channel) 24 | { 25 | for (int w = 0; w < dimO.w; ++w) 26 | { 27 | for (int h = 0; h < dimO.h; ++h) 28 | { 29 | stH = w * strideHorizontal - paddingHorizontal; 30 | stV = h * strideVertical - paddingVertical; 31 | output[h, w, channel, batch] = 0.0; 32 | 33 | for (int idxH = stH; idxH < stH + kernelDim.w; ++idxH) 34 | { 35 | for (int idxV = stV; idxV < stV + kernelDim.h; ++idxV) 36 | { 37 | output[h, w, channel, batch] += input[idxV, idxH, channel, batch]; 38 | } 39 | } 40 | 41 | output[h, w, channel, batch] = output[h, w, channel, batch] / (kernelDim.w * kernelDim.h); 42 | } 43 | } 44 | } 45 | } 46 | } 47 | 48 | protected Data2D input; 49 | protected Data2D output; 50 | 51 | protected int paddingVertical; 52 | protected int paddingHorizontal; 53 | protected int strideVertical; 54 | protected int strideHorizontal; 55 | 56 | protected Dimension kernelDim; 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/BatchNormKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | public class BatchNormKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | dim = input.GetDimension(); 16 | 17 | for (int c = 0; c < dim.c; ++c) 18 | { 19 | scale = gamma[c] / Math.Sqrt(variance[c] + epsilon); 20 | offset = beta[c] - scale * bias[c]; // see article (https://arxiv.org/abs/1502.03167) 21 | 22 | for (int b = 0; b < dim.b; ++b) 23 | { 24 | for (int h = 0; h < dim.h; ++h) 25 | { 26 | for (int w = 0; w < dim.w; ++w) 27 | { 28 | output[h, w, c, b] = scale * input[h, w, c, b] + offset; 29 | } 30 | } 31 | } 32 | } 33 | } 34 | 35 | protected Data2D input; 36 | protected Data2D output; 37 | 38 | protected List gamma; 39 | protected List beta; 40 | protected List bias; 41 | protected List variance; 42 | protected double epsilon; 43 | 44 | private Dimension dim; 45 | private double scale = 0.0; 46 | private double offset = 0.0; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/Bias2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class Bias2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dim = input.GetDimension(); 17 | 18 | for (int b = 0; b < dim.b; ++b) 19 | { 20 | for (int c = 0; c < dim.c; ++c) 21 | { 22 | for (int h = 0; h < dim.h; ++h) 23 | { 24 | for (int w = 0; w < dim.w; ++w) 25 | { 26 | input[h, w, c, b] += biases[c]; 27 | } 28 | } 29 | } 30 | } 31 | } 32 | 33 | protected Data2D input; 34 | protected DataArray biases; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/Conv1DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class Conv1DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimK = weights.GetDimension(); 17 | Dimension dimI = input.GetDimension(); 18 | Dimension dimO = output.GetDimension(); 19 | int stH = 0; 20 | 21 | for (int batch = 0; batch < dimO.b; ++batch) 22 | { 23 | for (int filter = 0; filter < dimO.c; ++filter) 24 | { 25 | for (int l = 0; l < dimO.w; ++l) 26 | { 27 | 28 | stH = l * stride - padding; 29 | output[0, l, filter, batch] = 0.0; 30 | 31 | for (int idx = stH; idx < stH + dimK.w; ++idx) 32 | { 33 | for (int idxC = 0; idxC < dimK.c; ++idxC) 34 | { 35 | output[0, l, filter, batch] += input[0, idx, idxC, batch] * 36 | weights[0, idx - stH, idxC, filter]; 37 | } 38 | } 39 | } 40 | } 41 | } 42 | } 43 | 44 | protected Data2D input; 45 | protected Data2D output; 46 | protected Data2D weights; 47 | 48 | protected int padding; 49 | protected int stride; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/Cropping1DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class Cropping1DKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | for (int l = trimBegin; l < input.GetDimension().w - trimEnd; ++l) 16 | { 17 | for (int c = 0; c < input.GetDimension().c; ++c) 18 | { 19 | for (int b = 0; b < input.GetDimension().b; ++b) 20 | { 21 | output[0, l - trimBegin, c, b] = input[0, l, c, b]; 22 | } 23 | } 24 | } 25 | } 26 | 27 | protected Data2D input; 28 | protected Data2D output; 29 | 30 | protected int trimBegin; 31 | protected int trimEnd; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/Cropping2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class Cropping2DKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | for (int h = topTrim; h < input.GetDimension().h - bottomTrim; ++h) 16 | { 17 | for (int w = leftTrim; w < input.GetDimension().w - rightTrim; ++w) 18 | { 19 | for (int c = 0; c < input.GetDimension().c; ++c) 20 | { 21 | for (int b = 0; b < input.GetDimension().b; ++b) 22 | { 23 | output[h - topTrim, w - leftTrim, c, b] = input[h, w, c, b]; 24 | } 25 | } 26 | } 27 | } 28 | } 29 | 30 | protected Data2D input; 31 | protected Data2D output; 32 | 33 | protected int topTrim; 34 | protected int bottomTrim; 35 | protected int leftTrim; 36 | protected int rightTrim; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/Dense2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class Dense2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimK = weights.GetDimension(); 17 | Dimension dimI = input.GetDimension(); 18 | 19 | for (int b = 0; b < dimI.b; ++b) 20 | { 21 | for (int kernel = 0; kernel < dimK.b; ++kernel) 22 | { 23 | output[0, 0, kernel, b] = 0; 24 | for (int c = 0; c < dimI.c; ++c) 25 | { 26 | output[0, 0, kernel, b] += input[0, 0, c, b] * weights[0, 0, c, kernel]; 27 | } 28 | } 29 | } 30 | 31 | } 32 | 33 | protected Data2D input; 34 | protected Data2D output; 35 | protected Data2D weights; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/DropoutKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | public class DropoutKernel : IKernel 11 | { 12 | public void Execute() 13 | { 14 | // does nothing in the forward direction 15 | output = input; 16 | } 17 | 18 | protected Data2D input; 19 | protected Data2D output; 20 | 21 | protected double rate; 22 | protected Data2D noiseShape; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/ELuKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class ELuKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | ELuLambda(input); 16 | 17 | output = input; 18 | } 19 | 20 | public static void ELuLambda(IData data) 21 | { 22 | data.ApplyToAll( p => 23 | { 24 | if (p >= 0.0) 25 | return p; 26 | else 27 | { 28 | return 1.0 * (Math.Exp(p) - 1.0); 29 | } 30 | }); 31 | } 32 | 33 | protected IData input; 34 | protected IData output; 35 | 36 | protected double alpha; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/FlattenKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class FlattenKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | int idx = 0; 17 | Dimension dim = input.GetDimension(); 18 | 19 | for (int row = 0; row < dim.h; ++row) 20 | { 21 | for (int col = 0; col < dim.w; ++col) 22 | { 23 | for (int chnl = 0; chnl < dim.c; ++chnl) 24 | { 25 | for (int batch = 0; batch < dim.b; ++batch) 26 | { 27 | output[0, 0, idx, batch] = input[row, col, chnl, batch]; 28 | } 29 | idx += 1; 30 | } 31 | } 32 | } 33 | } 34 | 35 | protected Data2D input; 36 | protected Data2D output; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/GlobalAvgPool1DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class GlobalAvgPool1DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | 18 | for (int batch = 0; batch < dimI.b; ++batch) 19 | { 20 | for (int channel = 0; channel < dimI.c; ++channel) 21 | { 22 | double sum = 0.0; 23 | for (int l = 0; l < dimI.w; ++l) 24 | { 25 | sum += input[0, l, channel, batch]; 26 | } 27 | 28 | output[0, 0, channel, batch] = sum / (dimI.w); 29 | } 30 | } 31 | } 32 | 33 | protected Data2D input; 34 | protected Data2D output; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/GlobalAvgPool2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class GlobalAvgPool2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | 18 | for (int batch = 0; batch < dimI.b; ++batch) 19 | { 20 | for (int channel = 0; channel < dimI.c; ++channel) 21 | { 22 | double sum = 0.0; 23 | for (int h = 0; h < dimI.h; ++h) 24 | { 25 | for (int w = 0; w < dimI.w; ++w) 26 | { 27 | sum += input[h, w, channel, batch]; 28 | } 29 | } 30 | 31 | output[0, 0, channel, batch] = sum / (dimI.h * dimI.w); 32 | } 33 | } 34 | } 35 | 36 | protected Data2D input; 37 | protected Data2D output; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/GlobalMaxPool1DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class GlobalMaxPool1DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | 18 | for (int batch = 0; batch < dimI.b; ++batch) 19 | { 20 | for (int channel = 0; channel < dimI.c; ++channel) 21 | { 22 | double maximum = 0.0; 23 | for (int l = 0; l < dimI.w; ++l) 24 | { 25 | maximum = Math.Max(maximum, input[0, l, channel, batch]); 26 | } 27 | 28 | output[0, 0, channel, batch] = maximum; 29 | } 30 | } 31 | } 32 | 33 | protected Data2D input; 34 | protected Data2D output; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/GlobalMaxPool2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class GlobalMaxPool2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | 18 | for (int batch = 0; batch < dimI.b; ++batch) 19 | { 20 | for (int channel = 0; channel < dimI.c; ++channel) 21 | { 22 | double maximum = 0.0; 23 | for (int h = 0; h < dimI.h; ++h) 24 | { 25 | for (int w = 0; w < dimI.w; ++w) 26 | { 27 | maximum = Math.Max(maximum, input[h, w, channel, batch]); 28 | } 29 | } 30 | 31 | output[0, 0, channel, batch] = maximum; 32 | } 33 | } 34 | } 35 | 36 | protected Data2D input; 37 | protected Data2D output; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/HardSigmoidKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class HardSigmoidKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | HardSigmoidLambda(input); 16 | output = input; 17 | } 18 | 19 | public static void HardSigmoidLambda(IData data) 20 | { 21 | data.ApplyToAll(p => 22 | { 23 | return Math.Max(0.0, Math.Min(1, 0.2 * p + 0.5)); 24 | }); 25 | } 26 | 27 | protected IData input; 28 | protected IData output; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/LeakyReLuKernel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class LeakyReLuKernel :IKernel 12 | { 13 | public void Execute() 14 | { 15 | LeakyReLuLambda(input); 16 | output = input; 17 | } 18 | 19 | public static void LeakyReLuLambda(IData data) 20 | { 21 | data.ApplyToAll(p => 22 | { 23 | if (p >= 0.0) 24 | return p; 25 | else 26 | { 27 | return 0.3*p; 28 | } 29 | }); 30 | } 31 | 32 | protected IData input; 33 | protected IData output; 34 | 35 | protected double alpha; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/MaxPool1DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class MaxPool1DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimO = output.GetDimension(); 17 | int stH = 0; 18 | 19 | for (int batch = 0; batch < dimO.b; ++batch) 20 | { 21 | for (int channel = 0; channel < dimO.c; ++channel) 22 | { 23 | for (int l = 0; l < dimO.w; ++l) 24 | { 25 | stH = l * stride - padding; 26 | output[0, l, channel, batch] = Double.MinValue; 27 | 28 | for (int idx = stH; idx < stH + kernelDim.w; ++idx) 29 | { 30 | output[0, l, channel, batch] = Math.Max(input[0, idx, channel, batch], 31 | output[0, l, channel, batch]); 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | protected Data2D input; 39 | protected Data2D output; 40 | 41 | protected int padding; 42 | protected int stride; 43 | 44 | protected Dimension kernelDim; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/MaxPool2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class MaxPool2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | Dimension dimO = output.GetDimension(); 18 | int stH = 0; 19 | int stV = 0; 20 | 21 | for (int batch = 0; batch < dimO.b; ++batch) 22 | { 23 | for (int channel = 0; channel < dimO.c; ++channel) 24 | { 25 | for (int w = 0; w < dimO.w; ++w) 26 | { 27 | for (int h = 0; h < dimO.h; ++h) 28 | { 29 | stH = w * strideHorizontal - paddingHorizontal; 30 | stV = h * strideVertical - paddingVertical; 31 | output[h, w, channel, batch] = Double.MinValue; 32 | 33 | for (int idxH = stH; idxH < stH + kernelDim.w; ++idxH) 34 | { 35 | for (int idxV = stV; idxV < stV + kernelDim.h; ++idxV) 36 | { 37 | output[h, w, channel, batch] = Math.Max(input[idxV, idxH, channel, batch], 38 | output[h, w, channel, batch]); 39 | } 40 | } 41 | 42 | } 43 | } 44 | } 45 | } 46 | } 47 | 48 | protected Data2D input; 49 | protected Data2D output; 50 | 51 | protected int paddingVertical; 52 | protected int paddingHorizontal; 53 | protected int strideVertical; 54 | protected int strideHorizontal; 55 | 56 | protected Dimension kernelDim; 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/MinPool2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | [Serializable()] 12 | public class MinPool2DKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | Dimension dimI = input.GetDimension(); 17 | Dimension dimO = output.GetDimension(); 18 | int stH = 0; 19 | int stV = 0; 20 | 21 | for (int batch = 0; batch < dimO.b; ++batch) 22 | { 23 | for (int channel = 0; channel < dimO.c; ++channel) 24 | { 25 | for (int w = 0; w < dimO.w; ++w) 26 | { 27 | for (int h = 0; h < dimO.h; ++h) 28 | { 29 | stH = w * strideHorizontal - paddingHorizontal; 30 | stV = h * strideVertical - paddingVertical; 31 | output[h, w, channel, batch] = Double.MaxValue; 32 | 33 | for (int idxH = stH; idxH < stH + kernelDim.w; ++idxH) 34 | { 35 | for (int idxV = stV; idxV < stV + kernelDim.h; ++idxV) 36 | { 37 | output[h, w, channel, batch] = Math.Min(input[idxV, idxH, channel, batch], 38 | output[h, w, channel, batch]); 39 | } 40 | } 41 | 42 | } 43 | } 44 | } 45 | } 46 | } 47 | 48 | protected Data2D input; 49 | protected Data2D output; 50 | 51 | protected int paddingVertical; 52 | protected int paddingHorizontal; 53 | protected int strideVertical; 54 | protected int strideHorizontal; 55 | 56 | protected Dimension kernelDim; 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/PermuteKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | public class PermuteKernel : IKernel 11 | { 12 | public void Execute() 13 | { 14 | int[] idx = { 0, 0, 0 }; 15 | for (int h = 0; h < input.GetDimension().h; ++h) 16 | { 17 | for (int w = 0; w < input.GetDimension().w; ++w) 18 | { 19 | for (int c = 0; c < input.GetDimension().c; ++c) 20 | { 21 | idx[0] = h; idx[1] = w; idx[2] = c; 22 | 23 | for (int b = 0; b < input.GetDimension().b; ++b) { 24 | 25 | output[idx[dim1 - 1], idx[dim2 - 1], idx[dim3 - 1], b] = input[h, w, c, b]; 26 | } 27 | } 28 | } 29 | } 30 | } 31 | 32 | protected Data2D input; 33 | protected Data2D output; 34 | 35 | protected int dim1, dim2, dim3; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/ReLuKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class ReLuKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | ReLuLambda(input); 16 | output = input; 17 | } 18 | 19 | public static void ReLuLambda(IData data) 20 | { 21 | data.ApplyToAll(p => 22 | { 23 | return Math.Max(0.0, p); 24 | }); 25 | } 26 | 27 | protected IData input; 28 | protected IData output; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/RepeatVectorKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | public class RepeatVectorKernel : IKernel 11 | { 12 | public void Execute() 13 | { 14 | for (int batch = 0; batch < input.GetDimension().b; ++batch) 15 | { 16 | for (int repeat = 0; repeat < num; ++repeat) 17 | { 18 | for (int channel = 0; channel < input.GetDimension().c; ++channel) 19 | { 20 | output[0, repeat, channel, batch] = input[0, 0, channel, batch]; 21 | } 22 | } 23 | } 24 | } 25 | 26 | protected Data2D input; 27 | protected Data2D output; 28 | protected int num; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/Reshape2DKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using static NNSharp.DataTypes.Data2D; 8 | 9 | namespace NNSharp.Kernels.CPUKernels 10 | { 11 | public class Reshape2DKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | Dimension dimI = input.GetDimension(); 16 | Dimension dimO = output.GetDimension(); 17 | 18 | int B1 = dimI.h * dimI.w * dimI.c; 19 | int B2 = dimO.h * dimO.w * dimO.c; 20 | 21 | int H1 = dimI.w * dimI.c; 22 | int H2 = dimO.w * dimO.c; 23 | 24 | int W1 = dimI.c; 25 | int W2 = dimO.c; 26 | 27 | int virtualIdx = 0; 28 | int cc, bb, hh, ww; 29 | 30 | for (int b = 0; b < dimI.b; ++b) 31 | { 32 | for (int h = 0; h < dimI.h; ++h) 33 | { 34 | for (int w = 0; w < dimI.w; ++w) 35 | { 36 | for (int c = 0; c < dimI.c; ++c) 37 | { 38 | virtualIdx = b * B1 + h * H1 + w * W1 + c; 39 | 40 | bb = (int)Math.Floor((double)virtualIdx / B2); 41 | virtualIdx = virtualIdx % B2; 42 | 43 | hh = (int)Math.Floor((double)virtualIdx / H2); 44 | virtualIdx = virtualIdx % H2; 45 | 46 | ww = (int)Math.Floor((double)virtualIdx / W2); 47 | cc = virtualIdx % W2; 48 | 49 | output[hh, ww, cc, bb] = input[h, w, c, b]; 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | protected Data2D input; 57 | protected Data2D output; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/SigmoidKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class SigmoidKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | SigmoidLambda(input); 16 | output = input; 17 | } 18 | 19 | public static void SigmoidLambda(IData data) 20 | { 21 | data.ApplyToAll(p => 22 | { 23 | return 1.0 / (1.0 + Math.Exp(-p)); 24 | }); 25 | } 26 | 27 | protected IData input; 28 | protected IData output; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/SoftPlusKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class SoftPlusKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | SoftPlusLambda(input); 16 | output = input; 17 | } 18 | 19 | public static void SoftPlusLambda(IData data) 20 | { 21 | data.ApplyToAll(p => 22 | { 23 | return Math.Log(1 + Math.Exp(p)); 24 | }); 25 | } 26 | 27 | protected IData input; 28 | protected IData output; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/SoftmaxKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class SoftmaxKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | SoftmaxLambda(data); 16 | } 17 | 18 | public static void SoftmaxLambda(IData data) 19 | { 20 | Data2D dat = data as Data2D; 21 | for (int b = 0; b < dat.GetDimension().b; ++b) 22 | { 23 | double sum = 0.0; 24 | for (int i = 0; i < dat.GetDimension().c; ++i) 25 | { 26 | sum += Math.Exp(dat[0, 0, i, b]); 27 | } 28 | 29 | for (int i = 0; i < dat.GetDimension().c; ++i) 30 | { 31 | dat[0, 0, i, b] = Math.Exp(dat[0, 0, i, b]) / sum; 32 | } 33 | } 34 | } 35 | 36 | protected Data2D data; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/SoftsignKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | [Serializable()] 11 | public class SoftsignKernel : IKernel 12 | { 13 | public void Execute() 14 | { 15 | SoftsignLambda(input); 16 | output = input; 17 | } 18 | 19 | public static void SoftsignLambda(IData data) 20 | { 21 | data.ApplyToAll(p => 22 | { 23 | return p / (1 + Math.Abs(p)); 24 | }); 25 | } 26 | 27 | protected IData input; 28 | protected IData output; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NNSharp/Kernels/CPUKernels/TanHKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels.CPUKernels 9 | { 10 | 11 | [Serializable()] 12 | public class TanHKernel : IKernel 13 | { 14 | public void Execute() 15 | { 16 | TanHLambda(input); 17 | output = input; 18 | } 19 | 20 | public static void TanHLambda(IData data) 21 | { 22 | data.ApplyToAll(p => 23 | { 24 | return 2.0 / (1 + Math.Exp(-2.0 * p)) - 1.0; 25 | }); 26 | } 27 | 28 | protected IData input; 29 | protected IData output; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /NNSharp/Kernels/IKernel.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.Kernels 9 | { 10 | public delegate void ActivationLambda(IData data); 11 | 12 | public interface IKernel 13 | { 14 | void Execute(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /NNSharp/Models/GraphModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.Models 8 | { 9 | class GraphModel : IModel 10 | { 11 | public IModelData GetSummary() 12 | { 13 | throw new NotImplementedException(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /NNSharp/Models/IModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace NNSharp.Models 8 | { 9 | public interface IModelData { } 10 | 11 | public interface IModel 12 | { 13 | IModelData GetSummary(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /NNSharp/Models/SequentialModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.SequentialBased.SequentialExecutors; 7 | using NNSharp.KernelDescriptors; 8 | using NNSharp.Kernels; 9 | using NNSharp.DataTypes; 10 | using static NNSharp.DataTypes.Data2D; 11 | 12 | namespace NNSharp.Models 13 | { 14 | [Serializable()] 15 | public class SequentialModel : IModel 16 | { 17 | public SequentialModel() 18 | { 19 | descriptors = new List(); 20 | } 21 | 22 | public void Add(IKernelDescriptor descriptor) 23 | { 24 | descriptors.Add(descriptor); 25 | } 26 | 27 | public void SetWeights(List weights) 28 | { 29 | compiled.SetWeights(weights); 30 | } 31 | 32 | public void Compile(ISequentialExecutor compiler) 33 | { 34 | // Compiling the model 35 | compiler.Compile(descriptors); 36 | compiled = compiler; 37 | 38 | // Saving the input dimension 39 | Input2D input = descriptors[0] as Input2D; 40 | dim = new Dimension(input.Height, input.Width, input.Channels, input.Batch); 41 | } 42 | 43 | public IData ExecuteNetwork(IData input) 44 | { 45 | return compiled.Execute(input); 46 | } 47 | 48 | public Dimension GetInputDimension() 49 | { 50 | return dim; 51 | } 52 | 53 | public IModelData GetSummary() 54 | { 55 | return compiled.GetSummary(); 56 | } 57 | 58 | [field: NonSerialized()] 59 | private List descriptors; 60 | private ISequentialExecutor compiled; 61 | private Dimension dim; 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /NNSharp/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("NNSharp")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("NNSharp")] 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("b64303f3-b967-4c29-8042-1a47c8859864")] 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 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialExecutors/IAbstractLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.KernelDescriptors; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace NNSharp.SequentialBased.SequentialExecutors 11 | { 12 | public interface IAbstractLayerFactory 13 | { 14 | ILayer CreateProduct(IKernelDescriptor descriptor); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialExecutors/ISequentialExecutor.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.Kernels; 2 | using NNSharp.KernelDescriptors; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using NNSharp.DataTypes; 9 | 10 | namespace NNSharp.SequentialBased.SequentialExecutors 11 | { 12 | public interface ISequentialExecutor 13 | { 14 | void Compile(List descriptors); 15 | IData Execute(IData input); 16 | void SetWeights(List weights); 17 | SequentialModelData GetSummary(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/AvgPool1DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class AvgPool1DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is AvgPooling1D) 15 | { 16 | AvgPooling1D pool = descriptor as AvgPooling1D; 17 | 18 | ILayer layer = new AvgPool1DLayer(pool.Padding, pool.Stride, pool.KernelSize); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/AvgPool2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class AvgPool2DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is AvgPooling2D) 15 | { 16 | AvgPooling2D conv = descriptor as AvgPooling2D; 17 | 18 | ILayer layer = new AvgPool2DLayer(conv.PaddingVertical, conv.PaddingHorizontal, 19 | conv.StrideVertical, conv.StrideHorizontal, 20 | conv.KernelHeight, conv.KernelWidth); 21 | 22 | return layer; 23 | } 24 | 25 | return null; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/BatchNormLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.KernelDescriptors; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class BatchNormLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is BatchNormalization) 15 | { 16 | BatchNormalization bnm = descriptor as BatchNormalization; 17 | 18 | ILayer layer = new BatchNormLayer(bnm.Epsilon); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Bias2DLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Kernels.CPUKernels; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | using static NNSharp.DataTypes.Data2D; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class Bias2DLayer : Bias2DKernel, ILayer 15 | { 16 | 17 | public Bias2DLayer() 18 | { 19 | } 20 | 21 | public IData GetOutput() 22 | { 23 | return input; 24 | } 25 | 26 | public void SetInput(IData input) 27 | { 28 | if (input == null) 29 | throw new Exception("Bias2DLayer: input is null."); 30 | else if (!(input is Data2D)) 31 | throw new Exception("Bias2DLayer: input is not Data2D."); 32 | 33 | this.input = input as Data2D; // Set the input value. 34 | 35 | int a, b; 36 | if ((a = this.input.GetDimension().c) != (b =this.biases.GetLength())) 37 | throw new Exception("Bias2DLayer: the number of biases is not suitable -> "+ a + " != " + b); 38 | } 39 | 40 | public void SetWeights(IData weights) 41 | { 42 | if (weights == null) 43 | throw new Exception("Bias2DLayer: biases is null."); 44 | else if (!(weights is DataArray)) 45 | throw new Exception("Bias2DLayer: biases is not DataArray."); 46 | 47 | this.biases = weights as DataArray; 48 | } 49 | 50 | public LayerData GetLayerSummary() 51 | { 52 | Dimension dimI = input.GetDimension(); 53 | Dimension dimO = input.GetDimension(); 54 | return new LayerData( 55 | this.ToString(), 56 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 57 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 58 | } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Bias2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.KernelDescriptors; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class Bias2DLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is Bias2D) 16 | { 17 | ILayer layer = new Bias2DLayer(); 18 | 19 | return layer; 20 | } 21 | 22 | return null; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Conv1DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class Conv1DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Convolution1D) 15 | { 16 | Convolution1D conv = descriptor as Convolution1D; 17 | 18 | ILayer layer = new Conv1DLayer(conv.Padding, conv.Stride); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Conv2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.KernelDescriptors; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class Conv2DLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is Convolution2D) 16 | { 17 | Convolution2D conv = descriptor as Convolution2D; 18 | 19 | ILayer layer = new Conv2DLayer(conv.PaddingVertical, conv.PaddingHorizontal, 20 | conv.StrideVertical, conv.StrideHorizontal); 21 | 22 | return layer; 23 | } 24 | 25 | return null; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Cropping1DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class Cropping1DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Cropping1D) 15 | { 16 | Cropping1D crop = descriptor as Cropping1D; 17 | 18 | ILayer layer = new Cropping1DLayer(crop.TrimBegin, crop.TrimEnd); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Cropping2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class Cropping2DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Cropping2D) 15 | { 16 | Cropping2D crop = descriptor as Cropping2D; 17 | 18 | ILayer layer = new Cropping2DLayer(crop.TopTrim, crop.BottomTrim, crop.LeftTrim, crop.RightTrim); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Dense2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.KernelDescriptors; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class Dense2DLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is Dense2D) 16 | { 17 | Dense2D dens = descriptor as Dense2D; 18 | 19 | ILayer layer = new Dense2DLayer(dens.Units); 20 | 21 | return layer; 22 | } 23 | 24 | return null; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/DropoutLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Kernels.CPUKernels; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | using static NNSharp.DataTypes.Data2D; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class DropoutLayer : DropoutKernel, ILayer 15 | { 16 | 17 | public DropoutLayer(double rate, Data2D noiseShape) 18 | { 19 | this.rate = rate; 20 | this.noiseShape = noiseShape; 21 | } 22 | 23 | public IData GetOutput() 24 | { 25 | return output; 26 | } 27 | 28 | public void SetInput(IData input) 29 | { 30 | this.input = input as Data2D; 31 | } 32 | 33 | public void SetWeights(IData weights) 34 | { 35 | // no weights 36 | } 37 | 38 | public SequentialModelData.LayerData GetLayerSummary() 39 | { 40 | Dimension dimI = input.GetDimension(); 41 | Dimension dimO = output.GetDimension(); 42 | return new LayerData( 43 | this.ToString(), 44 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 45 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/DropoutLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.KernelDescriptors; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class DropoutLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Dropout) 15 | { 16 | Dropout dropout = descriptor as Dropout; 17 | 18 | ILayer layer = new DropoutLayer(dropout.Rate, dropout.NoiseShape); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/ELuLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Kernels.CPUKernels; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | using static NNSharp.DataTypes.Data2D; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class ELuLayer : ELuKernel, ILayer 15 | { 16 | 17 | public ELuLayer(double alpha) 18 | { 19 | this.alpha = alpha; 20 | } 21 | 22 | public IData GetOutput() 23 | { 24 | return output; 25 | } 26 | 27 | public void SetInput(IData input) 28 | { 29 | this.input = input; 30 | } 31 | 32 | public void SetWeights(IData weights) 33 | { 34 | // No weights. 35 | } 36 | 37 | public LayerData GetLayerSummary() 38 | { 39 | // The input and the output have the same sizes as the output 40 | // of the previous layer. 41 | return new LayerData( 42 | this.ToString(), 43 | -1, -1, -1, -1, -1, 44 | -1, -1, -1, -1, -1); 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/ELuLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.KernelDescriptors; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class ELuLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is ELu) { 15 | 16 | ELu elu = descriptor as ELu; 17 | 18 | return new ELuLayer(elu.Alpha); 19 | } 20 | 21 | return null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/FlattenLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Kernels.CPUKernels; 8 | using static NNSharp.DataTypes.Data2D; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class FlattenLayer : FlattenKernel, ILayer 15 | { 16 | 17 | public IData GetOutput() 18 | { 19 | return output; 20 | } 21 | 22 | public void SetInput(IData input) 23 | { 24 | if (input == null) 25 | throw new Exception("Conv2DLayer: input is null."); 26 | else if (!(input is Data2D)) 27 | throw new Exception("Conv2DLayer: input is not Data2D."); 28 | 29 | this.input = input as Data2D; 30 | 31 | Dimension dim = this.input.GetDimension(); 32 | 33 | int chnlSize = dim.h * dim.w * dim.c; 34 | int batchSize = dim.b; 35 | 36 | output = new Data2D(1, 1, chnlSize, batchSize); 37 | } 38 | 39 | public void SetWeights(IData weights) 40 | { 41 | // No weights. 42 | } 43 | 44 | public LayerData GetLayerSummary() 45 | { 46 | Dimension dimI = input.GetDimension(); 47 | Dimension dimO = output.GetDimension(); 48 | return new LayerData( 49 | this.ToString(), 50 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 51 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/FlattenLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.KernelDescriptors; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class FlattenLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Flatten) 15 | { 16 | return new FlattenLayer(); 17 | } 18 | 19 | return null; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GRULayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class GRULayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is GRU) 15 | { 16 | GRU rnn = descriptor as GRU; 17 | 18 | ILayer layer = new GRULayer(rnn.Units, rnn.InputDim, rnn.Activation, 19 | rnn.RecurrentActivation); 20 | 21 | return layer; 22 | } 23 | 24 | return null; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalAvgPool1DLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.Data2D; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | public class GlobalAvgPool1DLayer : GlobalAvgPool1DKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | if (input == null) 24 | throw new Exception("GlobalAvgPool1DLayer: input is null."); 25 | else if (!(input is Data2D)) 26 | throw new Exception("GlobalAvgPool1DLayer: input is not Data2D."); 27 | 28 | this.input = input as Data2D; 29 | 30 | Dimension dimI = this.input.GetDimension(); 31 | 32 | int outputH = 1; 33 | int outputW = 1; 34 | int outputC = dimI.c; 35 | int outputB = dimI.b; 36 | 37 | output = new Data2D(outputH, outputW, outputC, outputB); 38 | } 39 | 40 | public void SetWeights(IData weights) 41 | { 42 | // No weights. 43 | } 44 | 45 | public LayerData GetLayerSummary() 46 | { 47 | Dimension dimI = input.GetDimension(); 48 | Dimension dimO = output.GetDimension(); 49 | return new LayerData( 50 | this.ToString(), 51 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 52 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalAvgPool1DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class GlobalAvgPool1DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is GlobalAvgPooling1D) 15 | { 16 | GlobalAvgPooling1D pool = descriptor as GlobalAvgPooling1D; 17 | 18 | ILayer layer = new GlobalAvgPool1DLayer(); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalAvgPool2DLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.Data2D; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | public class GlobalAvgPool2DLayer : GlobalAvgPool2DKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | if (input == null) 24 | throw new Exception("GlobalAvgPool2DLayer: input is null."); 25 | else if (!(input is Data2D)) 26 | throw new Exception("GlobalAvgPool2DLayer: input is not Data2D."); 27 | 28 | this.input = input as Data2D; 29 | 30 | Dimension dimI = this.input.GetDimension(); 31 | 32 | int outputH = 1; 33 | int outputW = 1; 34 | int outputC = dimI.c; 35 | int outputB = dimI.b; 36 | 37 | output = new Data2D(outputH, outputW, outputC, outputB); 38 | } 39 | 40 | public void SetWeights(IData weights) 41 | { 42 | // No weights. 43 | } 44 | 45 | public LayerData GetLayerSummary() 46 | { 47 | Dimension dimI = input.GetDimension(); 48 | Dimension dimO = output.GetDimension(); 49 | return new LayerData( 50 | this.ToString(), 51 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 52 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalAvgPool2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class GlobalAvgPool2DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is GlobalAvgPooling2D) 15 | { 16 | GlobalAvgPooling2D pool = descriptor as GlobalAvgPooling2D; 17 | 18 | ILayer layer = new GlobalAvgPool2DLayer(); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalMaxPool1DLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.Kernels.CPUKernels; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using NNSharp.DataTypes; 8 | using static NNSharp.DataTypes.Data2D; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | public class GlobalMaxPool1DLayer : GlobalMaxPool1DKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | if (input == null) 24 | throw new Exception("GlobalMaxPool1DLayer: input is null."); 25 | else if (!(input is Data2D)) 26 | throw new Exception("GlobalMaxPool1DLayer: input is not Data2D."); 27 | 28 | this.input = input as Data2D; 29 | 30 | Dimension dimI = this.input.GetDimension(); 31 | 32 | int outputH = 1; 33 | int outputW = 1; 34 | int outputC = dimI.c; 35 | int outputB = dimI.b; 36 | 37 | output = new Data2D(outputH, outputW, outputC, outputB); 38 | } 39 | 40 | public void SetWeights(IData weights) 41 | { 42 | // No weights. 43 | } 44 | 45 | public LayerData GetLayerSummary() 46 | { 47 | Dimension dimI = input.GetDimension(); 48 | Dimension dimO = output.GetDimension(); 49 | return new LayerData( 50 | this.ToString(), 51 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 52 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalMaxPool1DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class GlobalMaxPool1DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is GlobalMaxPooling1D) 15 | { 16 | GlobalMaxPooling1D pool = descriptor as GlobalMaxPooling1D; 17 | 18 | ILayer layer = new GlobalMaxPool1DLayer(); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalMaxPool2DLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.Data2D; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | public class GlobalMaxPool2DLayer : GlobalMaxPool2DKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | if (input == null) 24 | throw new Exception("GlobalMaxPool2DLayer: input is null."); 25 | else if (!(input is Data2D)) 26 | throw new Exception("GlobalMaxPool2DLayer: input is not Data2D."); 27 | 28 | this.input = input as Data2D; 29 | 30 | Dimension dimI = this.input.GetDimension(); 31 | 32 | int outputH = 1; 33 | int outputW = 1; 34 | int outputC = dimI.c; 35 | int outputB = dimI.b; 36 | 37 | output = new Data2D(outputH, outputW, outputC, outputB); 38 | } 39 | 40 | public void SetWeights(IData weights) 41 | { 42 | // No weights. 43 | } 44 | 45 | public LayerData GetLayerSummary() 46 | { 47 | Dimension dimI = input.GetDimension(); 48 | Dimension dimO = output.GetDimension(); 49 | return new LayerData( 50 | this.ToString(), 51 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 52 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/GlobalMaxPool2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class GlobalMaxPool2DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is GlobalMaxPooling2D) 15 | { 16 | GlobalMaxPooling2D pool = descriptor as GlobalMaxPooling2D; 17 | 18 | ILayer layer = new GlobalMaxPool2DLayer(); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/HardSigmoidLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | 10 | namespace NNSharp.SequentialBased.SequentialLayers 11 | { 12 | [Serializable()] 13 | public class HardSigmoidLayer : HardSigmoidKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | this.input = input; 24 | } 25 | 26 | public void SetWeights(IData weights) 27 | { 28 | // No weights. 29 | } 30 | 31 | public LayerData GetLayerSummary() 32 | { 33 | // The input and the output have the same sizes as the output 34 | // of the previous layer. 35 | return new LayerData( 36 | this.ToString(), 37 | -1, -1, -1, -1, -1, 38 | -1, -1, -1, -1, -1); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/HardSigmoidLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class HardSigmoidLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is HardSigmoid) 15 | return new HardSigmoidLayer(); 16 | 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/ILayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | 10 | namespace NNSharp.SequentialBased.SequentialLayers 11 | { 12 | public interface ILayer : IKernel 13 | { 14 | void SetInput(IData input); 15 | IData GetOutput(); 16 | void SetWeights(IData weights); 17 | LayerData GetLayerSummary(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/ILayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.KernelDescriptors; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public interface ILayerFactory 12 | { 13 | ILayer CreateProduct(IKernelDescriptor descriptor); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Input2DLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.SequentialBased.SequentialLayers; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using NNSharp.DataTypes; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | using static NNSharp.DataTypes.Data2D; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | /** 14 | * Stores the sizes of the input data 15 | * The data containes zeros as default values 16 | */ 17 | 18 | [Serializable()] 19 | public class Input2DLayer : ILayer 20 | { 21 | public Input2DLayer() 22 | { 23 | zerosInput = null; 24 | } 25 | 26 | public void Execute() 27 | { 28 | // nothing to do 29 | } 30 | 31 | public IData GetOutput() 32 | { 33 | return zerosInput; 34 | } 35 | 36 | public void SetInput(IData input) 37 | { 38 | if (input == null) 39 | throw new Exception("Input2DLayer: input is null."); 40 | else if (!(input is Data2D)) 41 | throw new Exception("Input2DLayer: input is not Data2D."); 42 | 43 | zerosInput = input as Data2D; 44 | } 45 | 46 | public void SetWeights(IData weights) 47 | { 48 | // No weights. 49 | } 50 | 51 | public LayerData GetLayerSummary() 52 | { 53 | Dimension dim = zerosInput.GetDimension(); 54 | return new LayerData( 55 | this.ToString(), 56 | dim.h, dim.w, 1, dim.c, dim.b, 57 | dim.h, dim.w, 1, dim.c, dim.b); 58 | } 59 | 60 | private Data2D zerosInput; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Input2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.KernelDescriptors; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class Input2DLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is Input2D) 16 | { 17 | Input2D inputDescriptor = descriptor as Input2D; 18 | 19 | Data2D data = new Data2D(inputDescriptor.Height, inputDescriptor.Width, 20 | inputDescriptor.Channels, inputDescriptor.Batch); 21 | 22 | data.ToZeros(); 23 | Input2DLayer layer = new Input2DLayer(); 24 | layer.SetInput(data); 25 | 26 | return layer; 27 | } 28 | 29 | return null; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/LSTMLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class LSTMLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is LSTM) 15 | { 16 | LSTM rnn = descriptor as LSTM; 17 | 18 | ILayer layer = new LSTMLayer(rnn.Units, rnn.InputDim, rnn.Activation, 19 | rnn.RecurrentActivation); 20 | 21 | return layer; 22 | } 23 | 24 | return null; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/LeakyReLuLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Kernels.CPUKernels; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | using static NNSharp.DataTypes.Data2D; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class LeakyReLuLayer : LeakyReLuKernel, ILayer 15 | { 16 | public LeakyReLuLayer(double alpha) 17 | { 18 | this.alpha = alpha; 19 | } 20 | 21 | public IData GetOutput() 22 | { 23 | return output; 24 | } 25 | 26 | public void SetInput(IData input) 27 | { 28 | this.input = input; 29 | } 30 | 31 | public void SetWeights(IData weights) 32 | { 33 | // No weights. 34 | } 35 | 36 | public LayerData GetLayerSummary() 37 | { 38 | // The input and the output have the same sizes as the output 39 | // of the previous layer. 40 | return new LayerData( 41 | this.ToString(), 42 | -1, -1, -1, -1, -1, 43 | -1, -1, -1, -1, -1); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/LeakyReLuLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.KernelDescriptors; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class LeakyReLuLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is LeakyReLu) { 15 | LeakyReLu leakyrelu = descriptor as LeakyReLu; 16 | 17 | return new LeakyReLuLayer(leakyrelu.Alpha); 18 | } 19 | 20 | return null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/MaxPool1DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class MaxPool1DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is MaxPooling1D) 15 | { 16 | MaxPooling1D pool = descriptor as MaxPooling1D; 17 | 18 | ILayer layer = new MaxPool1DLayer(pool.Padding, pool.Stride, pool.KernelSize); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/MaxPool2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.KernelDescriptors; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class MaxPool2DLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is MaxPooling2D) 16 | { 17 | MaxPooling2D pool = descriptor as MaxPooling2D; 18 | 19 | ILayer layer = new MaxPool2DLayer(pool.PaddingVertical, pool.PaddingHorizontal, 20 | pool.StrideVertical, pool.StrideHorizontal, 21 | pool.KernelHeight, pool.KernelWidth); 22 | 23 | return layer; 24 | } 25 | 26 | return null; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/MinPool2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class MinPool2DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is MinPooling2D) 15 | { 16 | MinPooling2D conv = descriptor as MinPooling2D; 17 | 18 | ILayer layer = new MaxPool2DLayer(conv.PaddingVertical, conv.PaddingHorizontal, 19 | conv.StrideVertical, conv.StrideHorizontal, 20 | conv.KernelHeight, conv.KernelWidth); 21 | 22 | return layer; 23 | } 24 | 25 | return null; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/PermuteLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class PermuteLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Permute) 15 | { 16 | Permute permute = descriptor as Permute; 17 | 18 | ILayer layer = new PermuteLayer(permute.Dim1, permute.Dim2, permute.Dim3); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/ReLuLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.Kernels.CPUKernels; 7 | using NNSharp.DataTypes; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | 10 | namespace NNSharp.SequentialBased.SequentialLayers 11 | { 12 | [Serializable()] 13 | public class ReLuLayer : ReLuKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | this.input = input; 24 | } 25 | 26 | public void SetWeights(IData weights) 27 | { 28 | // No weights. 29 | } 30 | 31 | public LayerData GetLayerSummary() 32 | { 33 | // The input and the output have the same sizes as the output 34 | // of the previous layer. 35 | return new LayerData( 36 | this.ToString(), 37 | -1, -1, -1, -1, -1, 38 | -1, -1, -1, -1, -1); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/ReLuLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.KernelDescriptors; 7 | using NNSharp.DataTypes; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class ReLuLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is ReLu) 16 | return new ReLuLayer(); 17 | 18 | return null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/RepeatVectorLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.Kernels.CPUKernels; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using NNSharp.DataTypes; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | using static NNSharp.DataTypes.Data2D; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class RepeatVectorLayer : RepeatVectorKernel, ILayer 15 | { 16 | public RepeatVectorLayer(int num) 17 | { 18 | this.num = num; 19 | } 20 | 21 | public IData GetOutput() 22 | { 23 | return output; 24 | } 25 | 26 | public void SetInput(IData input) 27 | { 28 | if (input == null) 29 | throw new Exception("RepeatVectorLayer: input is null."); 30 | else if (!(input is Data2D)) 31 | throw new Exception("RepeatVectorLayer: input is not Data2D."); 32 | 33 | this.input = input as Data2D; 34 | Dimension dimI = this.input.GetDimension(); 35 | 36 | if (dimI.h != 1) 37 | { 38 | throw new Exception("RepeatVectorLayer: The input height should be 1."); 39 | } 40 | 41 | if (dimI.w != 1) 42 | { 43 | throw new Exception("RepeatVectorLayer: The input width should be 1."); 44 | } 45 | 46 | output = new Data2D(1, num, dimI.c, dimI.b); 47 | } 48 | 49 | public void SetWeights(IData weights) 50 | { 51 | // No weights. 52 | } 53 | 54 | public LayerData GetLayerSummary() 55 | { 56 | Dimension dimI = input.GetDimension(); 57 | Dimension dimO = output.GetDimension(); 58 | return new LayerData( 59 | this.ToString(), 60 | dimI.h, dimI.w, 1, dimI.c, dimI.b, 61 | dimO.h, dimO.w, 1, dimO.c, dimO.b); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/RepeatVectorLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class RepeatVectorLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is RepeatVector) 15 | { 16 | RepeatVector repeat = descriptor as RepeatVector; 17 | 18 | ILayer layer = new RepeatVectorLayer(repeat.Num); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/Reshape2DLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class Reshape2DLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Reshape2D) 15 | { 16 | Reshape2D reshape = descriptor as Reshape2D; 17 | 18 | ILayer layer = new Reshape2DLayer(reshape.Height, reshape.Width, reshape.Channel, reshape.Batch); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SigmoidLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | 10 | namespace NNSharp.SequentialBased.SequentialLayers 11 | { 12 | [Serializable()] 13 | public class SigmoidLayer : SigmoidKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | this.input = input; 24 | } 25 | 26 | public void SetWeights(IData weights) 27 | { 28 | // No weights. 29 | } 30 | 31 | public LayerData GetLayerSummary() 32 | { 33 | // The input and the output have the same sizes as the output 34 | // of the previous layer. 35 | return new LayerData( 36 | this.ToString(), 37 | -1, -1, -1, -1, -1, 38 | -1, -1, -1, -1, -1); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SigmoidLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class SigmoidLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Sigmoid) 15 | return new SigmoidLayer(); 16 | 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SimpleRNNLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class SimpleRNNLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is SimpleRNN) 15 | { 16 | SimpleRNN rnn = descriptor as SimpleRNN; 17 | 18 | ILayer layer = new SimpleRNNLayer(rnn.Units, rnn.InputDim, rnn.Lambda); 19 | 20 | return layer; 21 | } 22 | 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SoftPlusLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | 10 | namespace NNSharp.SequentialBased.SequentialLayers 11 | { 12 | [Serializable()] 13 | public class SoftPlusLayer : SoftPlusKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | this.input = input; 24 | } 25 | 26 | public void SetWeights(IData weights) 27 | { 28 | // No weights. 29 | } 30 | 31 | public LayerData GetLayerSummary() 32 | { 33 | // The input and the output have the same sizes as the output 34 | // of the previous layer. 35 | return new LayerData( 36 | this.ToString(), 37 | -1, -1, -1, -1, -1, 38 | -1, -1, -1, -1, -1); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SoftPlusLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class SoftPlusLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is SoftPlus) 15 | return new SoftPlusLayer(); 16 | 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SoftmaxLayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Kernels.CPUKernels; 8 | using static NNSharp.DataTypes.Data2D; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class SoftmaxLayer : SoftmaxKernel, ILayer 15 | { 16 | 17 | public IData GetOutput() 18 | { 19 | return data; 20 | } 21 | 22 | public void SetInput(IData input) 23 | { 24 | if (input == null) 25 | throw new Exception("SoftmaxLayer: input is null."); 26 | else if (!(input is Data2D)) 27 | throw new Exception("SoftmaxLayer: input is not Data2D."); 28 | 29 | this.data = input as Data2D; 30 | 31 | Dimension dim = data.GetDimension(); 32 | 33 | if (!(dim.h == 1 && dim.w == 1)) 34 | throw new Exception("SoftmaxLayer: wrong intput size."); 35 | } 36 | 37 | public void SetWeights(IData weights) 38 | { 39 | // No weights. 40 | } 41 | 42 | public LayerData GetLayerSummary() 43 | { 44 | // The input and the output have the same sizes as the output 45 | // of the previous layer. 46 | return new LayerData( 47 | this.ToString(), 48 | -1, -1, -1, -1, -1, 49 | -1, -1, -1, -1, -1); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SoftmaxLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NNSharp.DataTypes; 7 | using NNSharp.KernelDescriptors; 8 | 9 | namespace NNSharp.SequentialBased.SequentialLayers 10 | { 11 | public class SoftmaxLayerFactory : ILayerFactory 12 | { 13 | public ILayer CreateProduct(IKernelDescriptor descriptor) 14 | { 15 | if (descriptor is Softmax) 16 | return new SoftmaxLayer(); 17 | 18 | return null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SoftsignLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.KernelDescriptors; 3 | using NNSharp.Kernels.CPUKernels; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using static NNSharp.DataTypes.SequentialModelData; 10 | 11 | namespace NNSharp.SequentialBased.SequentialLayers 12 | { 13 | [Serializable()] 14 | public class SoftsignLayer : SoftsignKernel, ILayer 15 | { 16 | 17 | public IData GetOutput() 18 | { 19 | return output; 20 | } 21 | 22 | public void SetInput(IData input) 23 | { 24 | this.input = input; 25 | } 26 | 27 | public void SetWeights(IData weights) 28 | { 29 | // No weights. 30 | } 31 | 32 | public LayerData GetLayerSummary() 33 | { 34 | // The input and the output have the same sizes as the output 35 | // of the previous layer. 36 | return new LayerData( 37 | this.ToString(), 38 | -1, -1, -1, -1, -1, 39 | -1, -1, -1, -1, -1); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/SoftsignLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class SoftsignLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is Softsign) 15 | return new SoftsignLayer(); 16 | 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/TanHLayer.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.DataTypes; 2 | using NNSharp.Kernels.CPUKernels; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using static NNSharp.DataTypes.SequentialModelData; 9 | 10 | namespace NNSharp.SequentialBased.SequentialLayers 11 | { 12 | [Serializable()] 13 | public class TanHLayer : TanHKernel, ILayer 14 | { 15 | 16 | public IData GetOutput() 17 | { 18 | return output; 19 | } 20 | 21 | public void SetInput(IData input) 22 | { 23 | this.input = input; 24 | } 25 | 26 | public void SetWeights(IData weights) 27 | { 28 | // No weights. 29 | } 30 | 31 | public LayerData GetLayerSummary() 32 | { 33 | // The input and the output have the same sizes as the output 34 | // of the previous layer. 35 | return new LayerData( 36 | this.ToString(), 37 | -1, -1, -1, -1, -1, 38 | -1, -1, -1, -1, -1); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /NNSharp/SequentialBased/SequentialLayers/TanHLayerFactory.cs: -------------------------------------------------------------------------------- 1 | using NNSharp.KernelDescriptors; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace NNSharp.SequentialBased.SequentialLayers 9 | { 10 | public class TanHLayerFactory : ILayerFactory 11 | { 12 | public ILayer CreateProduct(IKernelDescriptor descriptor) 13 | { 14 | if (descriptor is TanH) 15 | return new TanHLayer(); 16 | 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /NNSharp/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /PythonUtils/BatchNormalization.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import BatchNormalization, Conv2D 3 | import numpy as np 4 | 5 | model = Sequential() 6 | model.add(BatchNormalization(input_shape=(2, 1, 3))) 7 | model.add(Conv2D(2, (1, 1))) 8 | model.compile(optimizer='sgd', loss='mse') 9 | 10 | params = [0] * 4 11 | 12 | params[0] = np.array([3,3,3]) # gamma 13 | params[1] = np.array([1,2,-1]) # beta 14 | params[2] = np.array([2,2,2]) # bias 15 | params[3] = np.array([5,5,5]) # variance 16 | 17 | data = np.ndarray((4, 2, 1, 3)) 18 | 19 | l = 0 20 | for b in range(0, 4): 21 | for h in range(0, 2): 22 | for w in range(0, 1): 23 | for c in range(0, 3): 24 | l += 1 25 | data[b, h, w, c] = l % 7 - 3 26 | 27 | model.set_weights(params) 28 | output = model.predict(data, batch_size=1) # the batch_size has no impact on the result here 29 | 30 | print(output) 31 | 32 | 33 | print(model.summary()) 34 | 35 | print(model.get_config()) 36 | 37 | print(model.get_weights()) 38 | 39 | -------------------------------------------------------------------------------- /PythonUtils/Dropout.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Dropout, Convolution2D, Dense 3 | import numpy as np 4 | 5 | model = Sequential() 6 | model.add(Convolution2D(8, (2, 2), strides=(2, 2), input_shape=(42, 42, 3), activation='relu')) 7 | model.add(Dropout(0.8)) 8 | model.add(Dense(2, activation='linear')) 9 | 10 | model.compile(optimizer='sgd', loss='mse') 11 | 12 | input_data = np.random.randint(0, 255, size=(1, 42, 42, 3)) 13 | 14 | output_data = model.predict(input_data, 1) 15 | 16 | wght = model.get_weights() 17 | 18 | model2 = Sequential() 19 | model2.add(Convolution2D(8, (2, 2), strides=(2, 2), input_shape=(42, 42, 3), activation='relu')) 20 | model2.add(Dense(2, activation='linear')) 21 | 22 | model2.compile(optimizer='sgd', loss='mse') 23 | model2.set_weights(wght) 24 | 25 | output_data2 = model2.predict(input_data, 1) 26 | 27 | print(np.array_equal(output_data, output_data2)) 28 | print(model.get_config()) 29 | 30 | 31 | -------------------------------------------------------------------------------- /PythonUtils/FastTest.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Cropping1D, Reshape 3 | 4 | model = Sequential() 5 | 6 | model.add(Reshape((3, 2, 3) ,input_shape=(3, 3, 2))) 7 | model.compile(optimizer='rmsprop', loss='mse') 8 | 9 | print(model.get_config()) -------------------------------------------------------------------------------- /PythonUtils/GRU.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import GRU 3 | import numpy as np 4 | 5 | model = Sequential() 6 | ly = GRU(2, activation='tanh', recurrent_activation='relu',implementation = 1, stateful=False, batch_input_shape=(5, 3, 3)) 7 | model.add(ly) 8 | model.compile(optimizer='sgd', loss='mse') 9 | 10 | kernel = np.ones((3, 6)) 11 | rec_kernel = np.ones((2, 6)) 12 | bias = np.array([1, 2, -1, 0, 3, 4])/10 13 | 14 | k = 0 15 | for h in range(0, 3): 16 | for w in range(0, 6): 17 | k += 1 18 | kernel[h, w] = (k % 5 - 2)/10 19 | 20 | 21 | k = 0 22 | for h in range(0, 2): 23 | for w in range(0, 6): 24 | k += 1 25 | rec_kernel[h, w] = (k % 5 - 2)/10 26 | 27 | 28 | parameters = [kernel, rec_kernel, bias] 29 | model.set_weights(parameters) 30 | 31 | data = np.ndarray((5, 3, 3)) 32 | 33 | l = 0 34 | for b in range(0, 5): 35 | for h in range(0, 3): 36 | for c in range(0, 3): 37 | l += 1 38 | data[b, h, c] = (l % 5 + 1)/10 39 | 40 | 41 | output = model.predict(data, batch_size=5) # the batch_size has no impact on the result here 42 | 43 | print(output) 44 | 45 | 46 | print(model.summary()) 47 | 48 | print(model.get_config()) 49 | 50 | print(model.get_weights()) 51 | -------------------------------------------------------------------------------- /PythonUtils/LSTM.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import LSTM 3 | import numpy as np 4 | 5 | model = Sequential() 6 | ly = LSTM(2, activation='tanh', recurrent_activation='relu',implementation = 1, stateful=False, batch_input_shape=(5, 3, 3)) 7 | model.add(ly) 8 | model.compile(optimizer='sgd', loss='mse') 9 | 10 | kernel = np.ones((3, 8)) 11 | rec_kernel = np.ones((2, 8)) 12 | bias = np.array([1, 2, -1, 0, 3, 4, 5, -2])/10 13 | 14 | k = 0 15 | for h in range(0, 3): 16 | for w in range(0, 8): 17 | k += 1 18 | kernel[h, w] = (k % 5 - 2)/10 19 | 20 | 21 | k = 0 22 | for h in range(0, 2): 23 | for w in range(0, 8): 24 | k += 1 25 | rec_kernel[h, w] = (k % 5 - 2)/10 26 | 27 | 28 | parameters = [kernel, rec_kernel, bias] 29 | model.set_weights(parameters) 30 | 31 | data = np.ndarray((5, 3, 3)) 32 | 33 | l = 0 34 | for b in range(0, 5): 35 | for h in range(0, 3): 36 | for c in range(0, 3): 37 | l += 1 38 | data[b, h, c] = (l % 5 + 1)/10 39 | 40 | 41 | output = model.predict(data, batch_size=5) # the batch_size has no impact on the result here 42 | 43 | print(output) 44 | 45 | 46 | print(model.summary()) 47 | 48 | print(model.get_config()) 49 | 50 | print(model.get_weights()) 51 | -------------------------------------------------------------------------------- /PythonUtils/RLmodel.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Dense, Activation, Conv2D, Flatten 3 | from keras.optimizers import RMSprop, Adam 4 | import KerasModeltoJSON as js 5 | import numpy as np 6 | import time 7 | 8 | model = Sequential() 9 | model.add(Conv2D(32, (8, 8), padding='valid', input_shape=(84, 84, 4), strides=(4, 4))) 10 | model.add(Activation('relu')) 11 | model.add(Conv2D(64, (4, 4), padding='valid', strides=(2, 2))) 12 | model.add(Activation('relu')) 13 | model.add(Conv2D(64, (3, 3), padding='valid', strides=(1, 1))) 14 | model.add(Activation('relu')) 15 | model.add(Flatten()) 16 | model.add(Dense(512)) 17 | model.add(Activation('relu')) 18 | model.add(Dense(6)) 19 | 20 | # rmsprop = RMSprop(lr=alpha, epsilon=0.01, clipvalue=1.0, decay=0.01) 21 | adam = Adam(lr=0.001) 22 | model.compile(optimizer=adam, loss='mse') 23 | 24 | inp = np.random.rand(1, 84, 84, 4) 25 | 26 | start = time.process_time() 27 | model.predict(inp, batch_size = 1) 28 | end = time.process_time() 29 | 30 | print (end-start) 31 | 32 | wrt = js.JSONwriter(model, "tests/test_cnn_model.json") 33 | wrt.save() -------------------------------------------------------------------------------- /PythonUtils/RepeatVector.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras.models import Sequential 3 | from keras.layers import RepeatVector 4 | from keras import optimizers 5 | 6 | inp = 5 7 | 8 | model = Sequential() 9 | model.add(RepeatVector(3, input_shape = (inp,))) 10 | model.compile(loss='mse', optimizer='sgd') 11 | 12 | input_data = np.ndarray((1, inp)) 13 | 14 | for i in range(0, inp): 15 | input_data[0, i] = i * 2 +1 16 | 17 | output_data = model.predict(input_data) 18 | 19 | print(output_data) 20 | print(output_data.shape) -------------------------------------------------------------------------------- /PythonUtils/SimpleRNN.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import SimpleRNN 3 | import numpy as np 4 | 5 | model = Sequential() 6 | model.add(SimpleRNN(4, activation='relu', stateful=False, batch_input_shape=(4, 3, 3))) 7 | model.compile(optimizer='sgd', loss='mse') 8 | 9 | data = np.ndarray((4, 3, 3)) 10 | kernel = np.ones((3, 4)) 11 | rec_kernel = np.ones((4, 4)) 12 | bias = np.array([1.0, -1.0, 2.0, -4.0]) 13 | 14 | k = 0 15 | for h in range(0, 3): 16 | for w in range(0, 4): 17 | k += 1 18 | kernel[h, w] = k % 5 - 2 19 | 20 | k = 0 21 | for h in range(0, 4): 22 | for w in range(0, 4): 23 | k += 1 24 | rec_kernel[h, w] = k % 5 - 2 25 | 26 | parameters = [kernel, rec_kernel, bias] 27 | 28 | model.set_weights(parameters) 29 | 30 | l = 0 31 | for b in range(0, 4): 32 | for h in range(0, 3): 33 | for c in range(0, 3): 34 | l += 1 35 | data[b, h, c] = l % 5 + 1 36 | 37 | output = model.predict(data, batch_size=4) # the batch_size has no impact on the result here 38 | 39 | print(output) 40 | 41 | 42 | print(model.summary()) 43 | 44 | print(model.get_config()) 45 | 46 | print(model.get_weights()) 47 | -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_1D_1_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[52.0, 162.0], [230.0, 162.0], [92.0, 66.0], [111.0, 35.0], [240.0, 6.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_1D_1_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 5, "batch": 1}, {"layer": "AvgPooling1D", "padding": 0, "kernel_size": 3, "stride": 1}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_1D_1_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[124.66666412353516, 130.0], [144.3333282470703, 87.66666412353516], [147.6666717529297, 35.66666793823242]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_1D_2_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[236.0, 137.0], [94.0, 87.0], [199.0, 10.0], [40.0, 100.0], [178.0, 30.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_1D_2_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 5, "batch": 1}, {"layer": "AvgPooling1D", "padding": 0, "kernel_size": 3, "stride": 2}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_1D_2_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[176.3333282470703, 78.0], [139.0, 46.66666793823242]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_2D_1_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[209, 137], [52, 253], [2, 122], [103, 40], [47, 46]], [[206, 143], [131, 138], [88, 98], [129, 222], [30, 1]], [[192, 120], [135, 106], [127, 188], [68, 208], [175, 3]], [[102, 76], [116, 244], [53, 64], [216, 70], [251, 114]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_2D_1_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"stride_vl": 1, "kernel_height": 3, "kernel_width": 4, "padding_hz": 0, "layer": "AvgPooling2D", "stride_hz": 1, "padding_vl": 0}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_2D_1_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[120.16666412353516, 147.9166717529297], [90.58333587646484, 118.75]], [[130.25, 139.75], [126.58333587646484, 121.33333587646484]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_2D_2_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[128, 186], [35, 91], [94, 123], [17, 102], [124, 116]], [[75, 187], [68, 97], [66, 29], [67, 109], [234, 22]], [[125, 235], [47, 128], [18, 147], [106, 69], [107, 153]], [[254, 51], [225, 148], [43, 95], [178, 65], [236, 89]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_2D_2_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"stride_vl": 1, "kernel_height": 3, "kernel_width": 4, "padding_hz": 0, "layer": "AvgPooling2D", "stride_hz": 1, "padding_vl": 0}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_avgpool_2D_2_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[70.5, 125.25], [81.91666412353516, 98.83333587646484]], [[106.0, 113.33333587646484], [116.25, 95.91666412353516]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_batchnorm_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[99, 92, 66]], [[248, 77, 193]]], [[[20, 193, 90]], [[69, 157, 191]]], [[[164, 100, 188]], [[60, 231, 70]]], [[[243, 191, 46]], [[24, 170, 113]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_batchnorm_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 3, "height": 2, "width": 1, "batch": 1}, {"layer": "BatchNormalization", "epsilon": 0.001}], "weights": [[[[[3.0, 1.0, 2.0, 5.0], [3.0, 2.0, 2.0, 5.0], [3.0, -1.0, 2.0, 5.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_batchnorm_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[131.1261444091797, 122.735595703125, 84.8564224243164]], [[331.0106201171875, 102.61299133300781, 255.22775268554688]]], [[[25.147117614746094, 258.2277526855469, 117.05258178710938]], [[90.88094329833984, 209.9335174560547, 252.54473876953125]]], [[[218.32406616210938, 133.4676513671875, 248.52023315429688]], [[78.80738067626953, 309.20501708984375, 90.22245025634766]]], [[[324.3031005859375, 255.54473876953125, 58.02629089355469]], [[30.513145446777344, 227.37310791015625, 147.9072265625]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_1D_1_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[228.0, 180.0, 10.0, 188.0], [12.0, 29.0, 132.0, 52.0], [70.0, 150.0, 212.0, 237.0], [71.0, 244.0, 47.0, 121.0], [82.0, 176.0, 254.0, 194.0], [199.0, 41.0, 28.0, 11.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_1D_1_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 4, "height": 1, "width": 6, "batch": 1}, {"layer": "Convolution1D", "padding": 0, "stride": 1, "kernel_size": 2, "kernel_num": 3}, {"layer": "Bias2D", "units": 3}], "weights": [[[[[-2.0, 0.0, -2.0], [-4.0, 1.0, 2.0], [-2.0, 0.0, 3.0], [-4.0, -4.0, 2.0]], [[-1.0, 2.0, 0.0], [1.0, 4.0, 4.0], [4.0, 3.0, 3.0], [-5.0, 3.0, -5.0]]]], [[[[0.5, 1.5, 2.5]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_1D_1_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-1662.5, 121.5, 564.5], [-868.5, 1909.5, 587.5], [-2355.5, 825.5, 1784.5], [-1555.5, 1973.5, 1227.5], [-2252.5, 80.5, 1533.5]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_1D_2_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[74.0, 145.0, 23.0, 137.0], [31.0, 233.0, 245.0, 189.0], [221.0, 223.0, 159.0, 69.0], [104.0, 16.0, 132.0, 28.0], [253.0, 38.0, 134.0, 191.0], [245.0, 143.0, 209.0, 2.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_1D_2_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 4, "height": 1, "width": 6, "batch": 1}, {"layer": "Convolution1D", "padding": 0, "stride": 2, "kernel_size": 2, "kernel_num": 3}, {"layer": "Bias2D", "units": 3}], "weights": [[[[[-3.0, -2.0, 1.0], [2.0, 2.0, 0.0], [-5.0, 2.0, -4.0], [-1.0, 0.0, 4.0]], [[0.0, -5.0, 3.0], [-3.0, -4.0, 4.0], [-4.0, -2.0, 0.0], [1.0, -4.0, -3.0]]]], [[[[0.5, 1.5, 2.5]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_1D_2_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-1673.5, -2143.5, 990.5], [-1628.5, -636.5, 155.5], [-2806.5, -2383.5, 1784.5]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_2D_1_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[245, 194], [35, 151], [178, 203], [0, 147], [198, 186]], [[230, 113], [169, 254], [217, 45], [153, 17], [104, 97]], [[245, 175], [69, 10], [252, 89], [52, 197], [112, 134]], [[208, 18], [38, 172], [75, 122], [58, 229], [147, 253]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_2D_1_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"stride_vl": 1, "kernel_height": 3, "kernel_width": 4, "padding_hz": 0, "layer": "Convolution2D", "stride_hz": 1, "padding_vl": 0, "kernel_num": 2}, {"layer": "Bias2D", "units": 2}], "weights": [[[[[-5.0, 1.0], [-5.0, -5.0]], [[2.0, 4.0], [-3.0, -1.0]], [[2.0, 2.0], [0.0, -1.0]], [[4.0, 4.0], [4.0, 2.0]]], [[[-2.0, 4.0], [4.0, -2.0]], [[-1.0, -2.0], [4.0, -5.0]], [[-4.0, 3.0], [3.0, 2.0]], [[0.0, 0.0], [1.0, 2.0]]], [[[1.0, 3.0], [-5.0, -3.0]], [[-2.0, 2.0], [-5.0, 4.0]], [[2.0, -1.0], [-2.0, 1.0]], [[-2.0, -1.0], [-2.0, 0.0]]]], [[[[0.5, 1.5]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_2D_1_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-2500.5, -253.5], [-1181.5, 2073.5]], [[-2873.5, 4216.5], [-2906.5, 1007.5]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_2D_2_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[202, 76], [220, 205], [220, 16], [158, 129], [245, 241]], [[196, 138], [159, 64], [250, 197], [114, 128], [75, 232]], [[125, 185], [81, 14], [147, 160], [156, 234], [226, 45]], [[73, 157], [253, 160], [250, 113], [213, 250], [47, 191]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_2D_2_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"stride_vl": 2, "kernel_height": 2, "kernel_width": 4, "padding_hz": 0, "layer": "Convolution2D", "stride_hz": 1, "padding_vl": 0, "kernel_num": 2}, {"layer": "Bias2D", "units": 2}], "weights": [[[[[2.0, 4.0], [2.0, 3.0]], [[-1.0, -2.0], [-4.0, -4.0]], [[-4.0, 2.0], [-1.0, 4.0]], [[4.0, -5.0], [3.0, -5.0]]], [[[-2.0, 0.0], [1.0, -5.0]], [[1.0, 0.0], [3.0, -3.0]], [[0.0, -4.0], [-3.0, -4.0]], [[-5.0, -1.0], [-1.0, 1.0]]]], [[[[0.5, 1.5]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_conv_2D_2_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-1552.5, -3809.5], [1104.5, -2327.5]], [[151.5, -2857.5], [-1348.5, -3520.5]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_crop_1D_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[234.0, 57.0], [170.0, 176.0], [149.0, 135.0], [251.0, 166.0], [242.0, 211.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_crop_1D_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 5, "batch": 1}, {"layer": "Cropping1D", "trimBegin": 1, "trimEnd": 2}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_crop_1D_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[170.0, 176.0], [149.0, 135.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_crop_2D_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[190, 130], [199, 131], [130, 7], [85, 170], [52, 60]], [[69, 123], [117, 44], [30, 70], [79, 65], [98, 253]], [[220, 33], [53, 248], [89, 248], [108, 105], [137, 160]], [[172, 223], [180, 203], [166, 241], [28, 197], [178, 222]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_crop_2D_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"layer": "Cropping2D", "topTrim": 1, "rightTrim": 2, "leftTrim": 1, "bottomTrim": 1}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_crop_2D_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[117.0, 44.0], [30.0, 70.0]], [[53.0, 248.0], [89.0, 248.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_dense_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[2]], [[188]], [[224]], [[73]], [[29]], [[143]], [[4]], [[98]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_dense_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}], "weights": [[[[[0.0, 3.0, 1.0, -4.0], [-4.0, 4.0, 0.0, -5.0], [2.0, -5.0, 4.0, -5.0], [3.0, -1.0, 4.0, -1.0], [-3.0, -2.0, -4.0, -3.0], [-1.0, 4.0, -3.0, 1.0], [1.0, 4.0, 3.0, -3.0], [-3.0, 1.0, 0.0, 4.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_dense_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-604.5, 194.5, 658.0, -1702.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_dropout_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[152], [211], [74], [252]], [[93], [213], [197], [101]], [[189], [152], [80], [130]], [[40], [144], [251], [232]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_dropout_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-55.45210266113281, -3.6754608154296875]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_elu_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[161]], [[115]], [[130]], [[197]], [[180]], [[190]], [[1]], [[39]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_elu_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "ELu"}], "weights": [[[[[-4.0, -4.0, 4.0, 3.0], [1.0, 4.0, 1.0, 3.0], [-2.0, -3.0, 3.0, -3.0], [3.0, -1.0, -1.0, -5.0], [3.0, -4.0, 4.0, -3.0], [-3.0, -2.0, -2.0, -1.0], [-1.0, -1.0, -1.0, -1.0], [3.0, 2.0, 1.0, 4.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_elu_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-1.0, -1.0, 1331.0, -1.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_flat_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[227, 228], [48, 167], [122, 98], [19, 141], [226, 138]], [[176, 181], [179, 67], [162, 189], [212, 202], [115, 89]], [[44, 243], [137, 196], [245, 2], [200, 15], [157, 237]], [[209, 183], [74, 120], [252, 41], [159, 3], [53, 93]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_flat_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"layer": "Flatten"}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_flat_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[227.0, 228.0, 48.0, 167.0, 122.0, 98.0, 19.0, 141.0, 226.0, 138.0, 176.0, 181.0, 179.0, 67.0, 162.0, 189.0, 212.0, 202.0, 115.0, 89.0, 44.0, 243.0, 137.0, 196.0, 245.0, 2.0, 200.0, 15.0, 157.0, 237.0, 209.0, 183.0, 74.0, 120.0, 252.0, 41.0, 159.0, 3.0, 53.0, 93.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalavgpool_1D_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[1.0, 3.0], [2.0, 4.0], [0.0, 0.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalavgpool_1D_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 3, "batch": 1}, {"layer": "GlobalAveragePooling1D"}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalavgpool_1D_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[1.0, 2.3333334922790527]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalavgpool_2D_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[70, 179], [223, 32], [213, 239]], [[29, 123], [149, 134], [106, 157]], [[123, 43], [215, 250], [58, 146]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalavgpool_2D_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 3, "width": 3, "batch": 1}, {"layer": "GlobalAveragePooling2D"}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalavgpool_2D_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[131.77777099609375, 144.7777862548828]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalmaxpool_1D_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[1.0, 3.0], [2.0, 4.0], [0.0, 0.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalmaxpool_1D_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 3, "batch": 1}, {"layer": "GlobalMaxPooling1D"}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalmaxpool_1D_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[2.0, 4.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalmaxpool_2D_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[85, 243], [106, 175], [24, 43]], [[60, 100], [56, 15], [185, 32]], [[120, 61], [162, 205], [26, 131]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalmaxpool_2D_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 3, "width": 3, "batch": 1}, {"layer": "GlobalMaxPooling2D"}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_globalmaxpool_2D_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[185.0, 243.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_gru_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.2, 0.3, 0.4], [0.5, 0.1, 0.2], [0.3, 0.4, 0.5]]], [[[0.1, 0.2, 0.3], [0.4, 0.5, 0.1], [0.2, 0.3, 0.4]]], [[[0.5, 0.1, 0.2], [0.3, 0.4, 0.5], [0.1, 0.2, 0.3]]], [[[0.4, 0.5, 0.1], [0.2, 0.3, 0.4], [0.5, 0.1, 0.2]]], [[[0.3, 0.4, 0.5], [0.1, 0.2, 0.3], [0.4, 0.5, 0.1]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_gru_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 3, "height": 1, "width": 3, "batch": 5}, {"layer": "GRU", "input_dim": 3, "activation": "tanh", "rec_act": "relu", "units": 2}], "weights": [[[[[-0.10000000149011612, 0.10000000149011612, -0.20000000298023224, -0.10000000149011612, 0.10000000149011612, -0.20000000298023224, 0.10000000149011612, -0.10000000149011612, 0.30000001192092896], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.20000000298023224, 0.0, 0.4000000059604645]], [[0.0, 0.20000000298023224, -0.10000000149011612, 0.0, 0.20000000298023224, -0.10000000149011612, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[0.10000000149011612, -0.20000000298023224, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]], [[[0.0, 0.20000000298023224, -0.10000000149011612, 0.0, 0.20000000298023224, -0.10000000149011612, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[0.10000000149011612, -0.20000000298023224, 0.0, 0.10000000149011612, -0.20000000298023224, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[0.20000000298023224, -0.10000000149011612, 0.10000000149011612, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_gru_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.19632486999034882, 0.37259262800216675]]], [[[0.2199191153049469, 0.3747340142726898]]], [[[0.2483406662940979, 0.3817603886127472]]], [[[0.18727295100688934, 0.3526705205440521]]], [[[0.16661928594112396, 0.35275423526763916]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_hard_sigmoid_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[149]], [[206]], [[104]], [[131]], [[43]], [[237]], [[149]], [[182]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_hard_sigmoid_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "HardSigmoid"}], "weights": [[[[[2.0, 4.0, -2.0, 3.0], [4.0, -1.0, -3.0, -5.0], [-3.0, -3.0, -2.0, -2.0], [3.0, -4.0, 2.0, 0.0], [-3.0, -1.0, -4.0, 4.0], [3.0, 3.0, -2.0, -4.0], [2.0, 0.0, -5.0, -2.0], [2.0, 4.0, 0.0, 1.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_hard_sigmoid_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[1.0, 1.0, 0.0, 0.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_lstm_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.2, 0.3, 0.4], [0.5, 0.1, 0.2], [0.3, 0.4, 0.5]]], [[[0.1, 0.2, 0.3], [0.4, 0.5, 0.1], [0.2, 0.3, 0.4]]], [[[0.5, 0.1, 0.2], [0.3, 0.4, 0.5], [0.1, 0.2, 0.3]]], [[[0.4, 0.5, 0.1], [0.2, 0.3, 0.4], [0.5, 0.1, 0.2]]], [[[0.3, 0.4, 0.5], [0.1, 0.2, 0.3], [0.4, 0.5, 0.1]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_lstm_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 3, "height": 1, "width": 3, "batch": 5}, {"layer": "LSTM", "input_dim": 3, "activation": "tanh", "rec_act": "relu", "units": 2}], "weights": [[[[[-0.10000000149011612, 0.10000000149011612, -0.20000000298023224, 0.0, -0.10000000149011612, 0.10000000149011612, -0.20000000298023224, 0.0, 0.10000000149011612, -0.10000000149011612, 0.30000001192092896, 0.5], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.20000000298023224, 0.0, 0.4000000059604645, -0.20000000298023224]], [[0.20000000298023224, -0.10000000149011612, 0.10000000149011612, -0.20000000298023224, 0.20000000298023224, -0.10000000149011612, 0.10000000149011612, -0.20000000298023224, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[0.0, 0.20000000298023224, -0.10000000149011612, 0.10000000149011612, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]], [[[0.0, 0.20000000298023224, -0.10000000149011612, 0.10000000149011612, 0.0, 0.20000000298023224, -0.10000000149011612, 0.10000000149011612, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[-0.20000000298023224, 0.0, 0.20000000298023224, -0.10000000149011612, -0.20000000298023224, 0.0, 0.20000000298023224, -0.10000000149011612, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[0.10000000149011612, -0.20000000298023224, 0.0, 0.20000000298023224, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_lstm_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.01577799580991268, 0.0]]], [[[0.016057360917329788, 0.0]]], [[[0.016398193314671516, 0.0]]], [[[0.0063141752034425735, 0.0]]], [[[0.016302965581417084, 0.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_1D_1_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[82.0, 192.0], [172.0, 64.0], [132.0, 243.0], [140.0, 39.0], [204.0, 1.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_1D_1_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 5, "batch": 1}, {"layer": "MaxPooling1D", "padding": 0, "kernel_size": 3, "stride": 1}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_1D_1_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[172.0, 243.0], [172.0, 243.0], [204.0, 243.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_1D_2_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[10.0, 98.0], [189.0, 222.0], [194.0, 206.0], [73.0, 185.0], [33.0, 69.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_1D_2_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 1, "width": 5, "batch": 1}, {"layer": "MaxPooling1D", "padding": 0, "kernel_size": 3, "stride": 2}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_1D_2_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[194.0, 222.0], [194.0, 206.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_2D_1_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[33, 186], [207, 63], [244, 85], [192, 114], [58, 26]], [[226, 6], [44, 201], [160, 65], [55, 194], [160, 249]], [[97, 4], [137, 220], [147, 185], [22, 54], [65, 231]], [[11, 169], [241, 210], [13, 191], [23, 236], [41, 151]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_2D_1_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"stride_vl": 1, "kernel_height": 3, "kernel_width": 4, "padding_hz": 0, "layer": "MaxPooling2D", "stride_hz": 1, "padding_vl": 0}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_2D_1_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[244.0, 220.0], [244.0, 249.0]], [[241.0, 236.0], [241.0, 249.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_2D_2_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[36, 64], [205, 61], [143, 92], [27, 245], [244, 64]], [[225, 14], [173, 94], [99, 238], [9, 91], [27, 180]], [[172, 41], [212, 207], [98, 191], [199, 67], [44, 206]], [[232, 3], [167, 27], [227, 196], [199, 84], [186, 41]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_2D_2_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 4, "width": 5, "batch": 1}, {"stride_vl": 2, "kernel_height": 2, "kernel_width": 4, "padding_hz": 0, "layer": "MaxPooling2D", "stride_hz": 1, "padding_vl": 0}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_maxpool_2D_2_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[225.0, 245.0], [244.0, 245.0]], [[232.0, 207.0], [227.0, 207.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_permute_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[152, 1, 153, 193], [194, 121, 57, 4], [218, 83, 197, 155]], [[108, 76, 22, 205], [86, 205, 1, 36], [11, 241, 28, 52]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_permute_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 4, "height": 2, "width": 3, "batch": 1}, {"layer": "Permute", "dim2": 1, "dim1": 3, "dim3": 2}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_permute_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[152.0, 194.0, 218.0], [108.0, 86.0, 11.0]], [[1.0, 121.0, 83.0], [76.0, 205.0, 241.0]], [[153.0, 57.0, 197.0], [22.0, 1.0, 28.0]], [[193.0, 4.0, 155.0], [205.0, 36.0, 52.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_relu_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[210]], [[51]], [[195]], [[233]], [[209]], [[77]], [[105]], [[193]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_relu_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "ReLu"}], "weights": [[[[[-5.0, 2.0, -4.0, -5.0], [4.0, 3.0, 3.0, 3.0], [-2.0, 4.0, -3.0, -2.0], [2.0, 0.0, 1.0, -5.0], [3.0, -4.0, -5.0, 0.0], [0.0, -3.0, 1.0, 0.0], [0.0, -1.0, 3.0, -2.0], [-2.0, 1.0, 4.0, 4.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_relu_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.0, 375.5, 0.0, 0.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_repeatvector_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[72, 67, 96, 140]]], [[[106, 134, 24, 136]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_repeatvector_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 4, "height": 1, "width": 1, "batch": 1}, {"layer": "RepeatVector", "num": 3}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_repeatvector_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[72.0, 67.0, 96.0, 140.0], [72.0, 67.0, 96.0, 140.0], [72.0, 67.0, 96.0, 140.0]]], [[[106.0, 134.0, 24.0, 136.0], [106.0, 134.0, 24.0, 136.0], [106.0, 134.0, 24.0, 136.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_reshape_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[194, 236], [181, 155], [141, 89]], [[88, 102], [137, 197], [86, 172]], [[118, 23], [34, 162], [44, 190]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_reshape_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 2, "height": 3, "width": 3, "batch": 1}, {"layer": "Reshape", "channel": 3, "height": 3, "width": 2}], "weights": []} -------------------------------------------------------------------------------- /PythonUtils/tests/test_reshape_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[194.0, 236.0, 181.0], [155.0, 141.0, 89.0]], [[88.0, 102.0, 137.0], [197.0, 86.0, 172.0]], [[118.0, 23.0, 34.0], [162.0, 44.0, 190.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_sigmoid_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[141]], [[147]], [[189]], [[103]], [[49]], [[18]], [[107]], [[1]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_sigmoid_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "Sigmoid"}], "weights": [[[[[-3.0, -4.0, -4.0, 2.0], [4.0, -5.0, 1.0, -3.0], [-4.0, 0.0, -3.0, 0.0], [-2.0, 2.0, -5.0, 0.0], [2.0, 1.0, 2.0, -1.0], [-1.0, -5.0, 1.0, -2.0], [-2.0, 2.0, 2.0, 2.0], [1.0, -1.0, 4.0, 0.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_sigmoid_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.0, 0.0, 0.0, 1.8795288676126676e-12]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_simplernn_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[2.0, 3.0, 4.0], [5.0, 1.0, 2.0], [3.0, 4.0, 5.0]]], [[[1.0, 2.0, 3.0], [4.0, 5.0, 1.0], [2.0, 3.0, 4.0]]], [[[5.0, 1.0, 2.0], [3.0, 4.0, 5.0], [1.0, 2.0, 3.0]]], [[[4.0, 5.0, 1.0], [2.0, 3.0, 4.0], [5.0, 1.0, 2.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_simplernn_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 3, "height": 1, "width": 3, "batch": 4}, {"layer": "SimpleRNN", "input_dim": 3, "activation": "linear", "units": 4}], "weights": [[[[[-1.0, -1.0, 1.0], [0.0, 0.0, -1.0], [1.0, 1.0, 2.0], [2.0, 2.0, -4.0]], [[-2.0, -2.0, 0.0], [-1.0, -1.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]], [[2.0, 2.0, 0.0], [-2.0, -2.0, 0.0], [-1.0, -1.0, 0.0], [0.0, 0.0, 0.0]], [[0.0, 1.0, 0.0], [0.0, 2.0, 0.0], [0.0, -2.0, 0.0], [0.0, -1.0, 0.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_simplernn_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-54.0, -39.0, 36.0, 72.0]]], [[[12.0, -19.0, -10.0, 10.0]]], [[[-72.0, 16.0, 74.0, 68.0]]], [[[-161.0, -14.0, 158.0, 141.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softmax_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[222]], [[154]], [[205]], [[213]], [[201]], [[229]], [[196]], [[30]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softmax_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "Softmax"}], "weights": [[[[[3.0, -5.0, -3.0, 2.0], [0.0, -3.0, 1.0, -3.0], [-2.0, 0.0, -2.0, 4.0], [2.0, -5.0, -2.0, 2.0], [3.0, 0.0, 2.0, -5.0], [-1.0, 4.0, -1.0, -1.0], [-3.0, -1.0, -5.0, 4.0], [0.0, -5.0, 4.0, -3.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softmax_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.0, 0.0, 0.0, 1.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softplus_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[6]], [[121]], [[82]], [[227]], [[17]], [[179]], [[33]], [[135]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softplus_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "SoftPlus"}], "weights": [[[[[-5.0, -3.0, 0.0, 3.0], [2.0, -3.0, 4.0, -5.0], [-5.0, -4.0, -5.0, 3.0], [0.0, -4.0, 3.0, 3.0], [2.0, 4.0, 3.0, 4.0], [-3.0, -1.0, -3.0, -1.0], [2.0, 3.0, -4.0, 4.0], [3.0, 4.0, 3.0, 1.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softplus_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[0.0, 0.0, 543.0, 499.0]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softsign_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[105]], [[121]], [[152]], [[85]], [[181]], [[197]], [[145]], [[229]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softsign_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "SoftSign"}], "weights": [[[[[1.0, -3.0, -3.0, -2.0], [-5.0, 4.0, -5.0, 3.0], [1.0, -3.0, 1.0, -3.0], [0.0, -1.0, 4.0, 1.0], [4.0, 3.0, 3.0, 4.0], [-5.0, 3.0, -2.0, -2.0], [-4.0, -3.0, 4.0, 3.0], [-3.0, -2.0, -2.0, 2.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_softsign_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-0.9994670748710632, -0.992337167263031, -0.993630588054657, 0.9990088939666748]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_tanh_input.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[139]], [[132]], [[88]], [[116]], [[143]], [[193]], [[158]], [[67]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_tanh_model.json: -------------------------------------------------------------------------------- 1 | {"model_type": "Sequential", "descriptors": [{"layer": "Input2D", "channel": 1, "height": 8, "width": 1, "batch": 1}, {"layer": "Flatten"}, {"layer": "Dense2D", "units": 4}, {"layer": "Bias2D", "units": 4}, {"layer": "TanH"}], "weights": [[[[[4.0, -5.0, -4.0, -4.0], [-1.0, 4.0, -3.0, -2.0], [4.0, 1.0, 2.0, -2.0], [-5.0, -5.0, -4.0, 1.0], [2.0, -5.0, -3.0, -1.0], [-4.0, -3.0, 1.0, 1.0], [3.0, 4.0, -4.0, 3.0], [-3.0, 3.0, -1.0, 3.0]]]], [[[[0.5, 1.5, 1.0, 3.0]]]]]} -------------------------------------------------------------------------------- /PythonUtils/tests/test_tanh_output.json: -------------------------------------------------------------------------------- 1 | {"data": [[[[-1.0, -1.0, -1.0, -1.0]]]]} -------------------------------------------------------------------------------- /UnitTests/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("UnitTests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("UnitTests")] 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("c03f98c2-803c-4b27-a331-7d2a83f3987d")] 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 | -------------------------------------------------------------------------------- /UnitTests/TestDataArray.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.DataTypes; 4 | 5 | namespace UnitTests 6 | { 7 | [TestClass] 8 | public class TestDataArray 9 | { 10 | [TestInitialize] 11 | public void SetUp() 12 | { 13 | data = new DataArray(10); 14 | } 15 | 16 | [TestMethod] 17 | public void Test_DataArray_ToZeros() 18 | { 19 | data.ToZeros(); 20 | 21 | Assert.AreEqual(data[0], 0.0, 0.00000001); 22 | Assert.AreEqual(data[1], 0.0, 0.00000001); 23 | Assert.AreEqual(data[2], 0.0, 0.00000001); 24 | } 25 | 26 | [TestMethod] 27 | public void Test_DataArray_ApplyToAll() 28 | { 29 | for (int idx = 0; idx < data.GetLength(); ++idx) 30 | { 31 | 32 | data[idx] = 0.0; 33 | 34 | } 35 | 36 | data.ApplyToAll(p => { return p + 1; }); 37 | 38 | Assert.AreEqual(data[1], 1.0, 0.00000001); 39 | Assert.AreEqual(data[3], 1.0, 0.00000001); 40 | Assert.AreEqual(data[5], 1.0, 0.00000001); 41 | } 42 | 43 | [TestMethod] 44 | public void Test_DataArray_Foreach() 45 | { 46 | double sum = 0.0; 47 | data.ToZeros(); 48 | 49 | for (int i = 0; i < 10; ++i) 50 | { 51 | data[i] = i + 1; 52 | sum += i + 1; 53 | } 54 | 55 | double sumCounted = 0.0; 56 | foreach(double x in data) 57 | { 58 | sumCounted += x; 59 | } 60 | 61 | Assert.AreEqual(sum, sumCounted, 0.00000001); 62 | } 63 | 64 | private DataArray data; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /UnitTests/TestDropout.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using System.Collections.Generic; 4 | using Microsoft.VisualStudio.TestTools.UnitTesting; 5 | using NNSharp.IO; 6 | using NNSharp.DataTypes; 7 | using NNSharp.Models; 8 | using static NNSharp.DataTypes.Data2D; 9 | using UnitTests.Properties; 10 | 11 | namespace UnitTests 12 | { 13 | [TestClass] 14 | public class TestDropout 15 | { 16 | [TestMethod] 17 | public void Test_Dropout_KerasModel() 18 | { 19 | string pathModel = Resources.TestsFolder + "test_dropout_model.json"; 20 | string pathInput = Resources.TestsFolder + "test_dropout_input.json"; 21 | string pathOutput = Resources.TestsFolder + "test_dropout_output.json"; 22 | 23 | Utils.KerasModelTest(pathInput, pathModel, pathOutput, 0.001); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /UnitTests/TestELu.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using NNSharp.DataTypes; 3 | using NNSharp.IO; 4 | using NNSharp.Models; 5 | using NNSharp.SequentialBased.SequentialLayers; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using UnitTests.Properties; 12 | 13 | namespace UnitTests 14 | { 15 | [TestClass] 16 | public class TestELu 17 | { 18 | 19 | [TestMethod] 20 | public void Test_ELu_Execute() 21 | { 22 | double alpha = 1.0; 23 | elu = new ELuLayer(alpha); 24 | 25 | Data2D data = new Data2D(2, 3, 1, 1); 26 | data[0, 0, 0, 0] = 4; 27 | data[0, 1, 0, 0] = 2; 28 | data[0, 2, 0, 0] = -2; 29 | 30 | data[1, 0, 0, 0] = 3; 31 | data[1, 1, 0, 0] = -1; 32 | data[1, 2, 0, 0] = -3; 33 | 34 | elu.SetInput(data); 35 | 36 | elu.Execute(); 37 | 38 | Data2D output = elu.GetOutput() as Data2D; 39 | 40 | Assert.AreEqual(output[0, 0, 0, 0], 4.0, 0.00000001); 41 | Assert.AreEqual(output[0, 1, 0, 0], 2.0, 0.00000001); 42 | Assert.AreEqual(output[0, 2, 0, 0], alpha*(Math.Exp(-2) - 1), 0.00000001); 43 | 44 | Assert.AreEqual(output[1, 0, 0, 0], 3.0, 0.00000001); 45 | Assert.AreEqual(output[1, 1, 0, 0], alpha * (Math.Exp(-1) - 1), 0.00000001); 46 | Assert.AreEqual(output[1, 2, 0, 0], alpha * (Math.Exp(-3) - 1), 0.00000001); 47 | } 48 | 49 | [TestMethod] 50 | public void Test_ELu_KerasModel() 51 | { 52 | string pathModel = Resources.TestsFolder + "test_elu_model.json"; 53 | string pathInput = Resources.TestsFolder + "test_elu_input.json"; 54 | string pathOutput = Resources.TestsFolder + "test_elu_output.json"; 55 | 56 | Utils.KerasModelTest(pathInput, pathModel, pathOutput); 57 | } 58 | 59 | private ELuLayer elu; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /UnitTests/TestInput2D.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.DataTypes; 4 | using NNSharp.SequentialBased.SequentialLayers; 5 | 6 | namespace UnitTests 7 | { 8 | [TestClass] 9 | public class TestInput2D 10 | { 11 | [TestMethod] 12 | [ExpectedException(typeof(System.Exception))] 13 | public void Test_Input2D_NullInput() 14 | { 15 | Data2D data = null; 16 | Input2DLayer inp = new Input2DLayer(); 17 | inp.SetInput(data); 18 | } 19 | 20 | [TestMethod] 21 | [ExpectedException(typeof(System.Exception))] 22 | public void Test_input2D_DifferentData_Input() 23 | { 24 | DataArray data = new DataArray(5); 25 | Input2DLayer inp = new Input2DLayer(); 26 | inp.SetInput(data); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /UnitTests/TestLeakyReLu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using NNSharp.DataTypes; 5 | using NNSharp.IO; 6 | using NNSharp.Models; 7 | using UnitTests.Properties; 8 | using System.Collections.Generic; 9 | using System.Linq; 10 | using System.Text; 11 | using System.Threading.Tasks; 12 | 13 | namespace UnitTests 14 | { 15 | [TestClass] 16 | public class TestLeakyReLu 17 | { 18 | [TestMethod] 19 | public void Test_LeakyReLu_Execute() 20 | { 21 | double alpha = 0.3; 22 | leakyrelu = new LeakyReLuLayer(alpha); 23 | 24 | Data2D data = new Data2D(2, 3, 1, 1); 25 | data[0, 0, 0, 0] = 4; 26 | data[0, 1, 0, 0] = 2; 27 | data[0, 2, 0, 0] = -2; 28 | 29 | data[1, 0, 0, 0] = 3; 30 | data[1, 1, 0, 0] = -1; 31 | data[1, 2, 0, 0] = -3; 32 | 33 | leakyrelu.SetInput(data); 34 | 35 | leakyrelu.Execute(); 36 | 37 | Data2D output = leakyrelu.GetOutput() as Data2D; 38 | 39 | Assert.AreEqual(output[0, 0, 0, 0], 4.0, 0.00000001); 40 | Assert.AreEqual(output[0, 1, 0, 0], 2.0, 0.00000001); 41 | Assert.AreEqual(output[0, 2, 0, 0], alpha*(-2), 0.00000001); 42 | 43 | Assert.AreEqual(output[1, 0, 0, 0], 3.0, 0.00000001); 44 | Assert.AreEqual(output[1, 1, 0, 0], alpha * (-1), 0.00000001); 45 | Assert.AreEqual(output[1, 2, 0, 0], alpha * (-3), 0.00000001); 46 | } 47 | 48 | /*[TestMethod] 49 | public void Test_LeakyReLu_KerasModel() 50 | { 51 | }*/ 52 | 53 | private LeakyReLuLayer leakyrelu; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /UnitTests/TestReLu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using NNSharp.DataTypes; 5 | using NNSharp.IO; 6 | using NNSharp.Models; 7 | using UnitTests.Properties; 8 | 9 | namespace UnitTests 10 | { 11 | [TestClass] 12 | public class TestReLu 13 | { 14 | 15 | [TestMethod] 16 | public void Test_ReLu_Execute() 17 | { 18 | relu = new ReLuLayer(); 19 | 20 | Data2D data = new Data2D(2, 3, 1, 1); 21 | data[0, 0, 0, 0] = 4; 22 | data[0, 1, 0, 0] = 2; 23 | data[0, 2, 0, 0] = -2; 24 | 25 | data[1, 0, 0, 0] = 3; 26 | data[1, 1, 0, 0] = -1; 27 | data[1, 2, 0, 0] = -3; 28 | 29 | relu.SetInput(data); 30 | 31 | relu.Execute(); 32 | 33 | Data2D output = relu.GetOutput() as Data2D; 34 | 35 | Assert.AreEqual(output[0, 0, 0, 0], 4.0, 0.00000001); 36 | Assert.AreEqual(output[0, 1, 0, 0], 2.0, 0.00000001); 37 | Assert.AreEqual(output[0, 2, 0, 0], 0.0, 0.00000001); 38 | 39 | Assert.AreEqual(output[1, 0, 0, 0], 3.0, 0.00000001); 40 | Assert.AreEqual(output[1, 1, 0, 0], 0.0, 0.00000001); 41 | Assert.AreEqual(output[1, 2, 0, 0], 0.0, 0.00000001); 42 | } 43 | 44 | [TestMethod] 45 | public void Test_ReLu_KerasModel() 46 | { 47 | string pathModel = Resources.TestsFolder + "test_relu_model.json"; 48 | string pathInput = Resources.TestsFolder + "test_relu_input.json"; 49 | string pathOutput = Resources.TestsFolder + "test_relu_output.json"; 50 | 51 | Utils.KerasModelTest(pathInput, pathModel, pathOutput); 52 | } 53 | 54 | private ReLuLayer relu; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /UnitTests/TestSigmoid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using NNSharp.DataTypes; 5 | using NNSharp.IO; 6 | using NNSharp.Models; 7 | using UnitTests.Properties; 8 | 9 | namespace UnitTests 10 | { 11 | [TestClass] 12 | public class TestSigmoid 13 | { 14 | [TestMethod] 15 | public void Test_Sigmoid_Execute() 16 | { 17 | sigmoid = new SigmoidLayer(); 18 | 19 | Data2D data = new Data2D(2, 3, 1, 1); 20 | data[0, 0, 0, 0] = 4; 21 | data[0, 1, 0, 0] = 2; 22 | data[0, 2, 0, 0] = -2; 23 | 24 | data[1, 0, 0, 0] = 3; 25 | data[1, 1, 0, 0] = -1; 26 | data[1, 2, 0, 0] = -3; 27 | 28 | sigmoid.SetInput(data); 29 | 30 | sigmoid.Execute(); 31 | 32 | Data2D output = sigmoid.GetOutput() as Data2D; 33 | 34 | Assert.AreEqual(output[0, 0, 0, 0], SigmoidFunc(4.0), 0.00000001); 35 | Assert.AreEqual(output[0, 1, 0, 0], SigmoidFunc(2.0), 0.00000001); 36 | Assert.AreEqual(output[0, 2, 0, 0], SigmoidFunc(-2.0), 0.00000001); 37 | 38 | Assert.AreEqual(output[1, 0, 0, 0], SigmoidFunc(3.0), 0.00000001); 39 | Assert.AreEqual(output[1, 1, 0, 0], SigmoidFunc(-1.0), 0.00000001); 40 | Assert.AreEqual(output[1, 2, 0, 0], SigmoidFunc(-3.0), 0.00000001); 41 | } 42 | 43 | private double SigmoidFunc(double x) 44 | { 45 | return 1.0/(1 + Math.Exp(-x)); 46 | } 47 | 48 | [TestMethod] 49 | public void Test_Sigmoid_KerasModel() 50 | { 51 | string pathModel = Resources.TestsFolder + "test_sigmoid_model.json"; 52 | string pathInput = Resources.TestsFolder + "test_sigmoid_input.json"; 53 | string pathOutput = Resources.TestsFolder + "test_sigmoid_output.json"; 54 | 55 | Utils.KerasModelTest(pathInput, pathModel, pathOutput); 56 | } 57 | 58 | private SigmoidLayer sigmoid; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /UnitTests/TestSoftPlus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using NNSharp.DataTypes; 5 | using NNSharp.IO; 6 | using NNSharp.Models; 7 | using UnitTests.Properties; 8 | 9 | namespace UnitTests 10 | { 11 | [TestClass] 12 | public class TestSoftPlus 13 | { 14 | [TestMethod] 15 | public void Test_SoftPlus_Execute() 16 | { 17 | softplus = new SoftPlusLayer(); 18 | 19 | Data2D data = new Data2D(2, 3, 1, 1); 20 | data[0, 0, 0, 0] = 4; 21 | data[0, 1, 0, 0] = 2; 22 | data[0, 2, 0, 0] = -2; 23 | 24 | data[1, 0, 0, 0] = 3; 25 | data[1, 1, 0, 0] = -1; 26 | data[1, 2, 0, 0] = -3; 27 | 28 | softplus.SetInput(data); 29 | 30 | softplus.Execute(); 31 | 32 | Data2D output = softplus.GetOutput() as Data2D; 33 | 34 | Assert.AreEqual(output[0, 0, 0, 0], SoftPlusFunc(4.0), 0.00000001); 35 | Assert.AreEqual(output[0, 1, 0, 0], SoftPlusFunc(2.0), 0.00000001); 36 | Assert.AreEqual(output[0, 2, 0, 0], SoftPlusFunc(-2.0), 0.00000001); 37 | 38 | Assert.AreEqual(output[1, 0, 0, 0], SoftPlusFunc(3.0), 0.00000001); 39 | Assert.AreEqual(output[1, 1, 0, 0], SoftPlusFunc(-1.0), 0.00000001); 40 | Assert.AreEqual(output[1, 2, 0, 0], SoftPlusFunc(-3.0), 0.00000001); 41 | } 42 | 43 | private double SoftPlusFunc(double x) 44 | { 45 | return Math.Log(1 + Math.Exp(x)); 46 | } 47 | 48 | [TestMethod] 49 | public void Test_SoftPlus_KerasModel() 50 | { 51 | string pathModel = Resources.TestsFolder + "test_softplus_model.json"; 52 | string pathInput = Resources.TestsFolder + "test_softplus_input.json"; 53 | string pathOutput = Resources.TestsFolder + "test_softplus_output.json"; 54 | 55 | Utils.KerasModelTest(pathInput, pathModel, pathOutput); 56 | } 57 | 58 | private SoftPlusLayer softplus; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /UnitTests/TestSoftsign.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using NNSharp.DataTypes; 5 | using NNSharp.IO; 6 | using NNSharp.Models; 7 | using UnitTests.Properties; 8 | 9 | namespace UnitTests 10 | { 11 | [TestClass] 12 | public class TestSoftsign 13 | { 14 | [TestMethod] 15 | public void Test_Softsign_Execute() 16 | { 17 | softsign = new SoftsignLayer(); 18 | 19 | Data2D data = new Data2D(2, 3, 1, 1); 20 | data[0, 0, 0, 0] = 4; 21 | data[0, 1, 0, 0] = 2; 22 | data[0, 2, 0, 0] = -2; 23 | 24 | data[1, 0, 0, 0] = 3; 25 | data[1, 1, 0, 0] = -1; 26 | data[1, 2, 0, 0] = -3; 27 | 28 | softsign.SetInput(data); 29 | 30 | softsign.Execute(); 31 | 32 | Data2D output = softsign.GetOutput() as Data2D; 33 | 34 | Assert.AreEqual(output[0, 0, 0, 0], SoftsignFunc(4.0), 0.00000001); 35 | Assert.AreEqual(output[0, 1, 0, 0], SoftsignFunc(2.0), 0.00000001); 36 | Assert.AreEqual(output[0, 2, 0, 0], SoftsignFunc(-2.0), 0.00000001); 37 | 38 | Assert.AreEqual(output[1, 0, 0, 0], SoftsignFunc(3.0), 0.00000001); 39 | Assert.AreEqual(output[1, 1, 0, 0], SoftsignFunc(-1.0), 0.00000001); 40 | Assert.AreEqual(output[1, 2, 0, 0], SoftsignFunc(-3.0), 0.00000001); 41 | } 42 | 43 | private double SoftsignFunc(double x) 44 | { 45 | return x / (1 + Math.Abs(x)); 46 | } 47 | 48 | [TestMethod] 49 | public void Test_SoftSign_KerasModel() 50 | { 51 | string pathModel = Resources.TestsFolder + "test_softsign_model.json"; 52 | string pathInput = Resources.TestsFolder + "test_softsign_input.json"; 53 | string pathOutput = Resources.TestsFolder + "test_softsign_output.json"; 54 | 55 | Utils.KerasModelTest(pathInput, pathModel, pathOutput); 56 | } 57 | 58 | private SoftsignLayer softsign; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /UnitTests/TestTanH.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using NNSharp.SequentialBased.SequentialLayers; 4 | using NNSharp.DataTypes; 5 | using NNSharp.IO; 6 | using NNSharp.Models; 7 | using UnitTests.Properties; 8 | 9 | namespace UnitTests 10 | { 11 | [TestClass] 12 | public class TestTanH 13 | { 14 | [TestMethod] 15 | public void Test_TanH_Execute() 16 | { 17 | relu = new TanHLayer(); 18 | 19 | Data2D data = new Data2D(2, 3, 1, 1); 20 | data[0, 0, 0, 0] = 4; 21 | data[0, 1, 0, 0] = 2; 22 | data[0, 2, 0, 0] = -2; 23 | 24 | data[1, 0, 0, 0] = 3; 25 | data[1, 1, 0, 0] = -1; 26 | data[1, 2, 0, 0] = -3; 27 | 28 | relu.SetInput(data); 29 | 30 | relu.Execute(); 31 | 32 | Data2D output = relu.GetOutput() as Data2D; 33 | 34 | Assert.AreEqual(output[0, 0, 0, 0], tanh(4.0), 0.00000001); 35 | Assert.AreEqual(output[0, 1, 0, 0], tanh(2.0), 0.00000001); 36 | Assert.AreEqual(output[0, 2, 0, 0], tanh(-2.0), 0.00000001); 37 | 38 | Assert.AreEqual(output[1, 0, 0, 0], tanh(3.0), 0.00000001); 39 | Assert.AreEqual(output[1, 1, 0, 0], tanh(-1.0), 0.00000001); 40 | Assert.AreEqual(output[1, 2, 0, 0], tanh(-3.0), 0.00000001); 41 | } 42 | 43 | private double tanh(double x) 44 | { 45 | return 2.0 / (1 + Math.Exp(-2 * x)) - 1.0; 46 | } 47 | 48 | [TestMethod] 49 | public void Test_TanH_KerasModel() 50 | { 51 | string pathModel = Resources.TestsFolder + "test_tanh_model.json"; 52 | string pathInput = Resources.TestsFolder + "test_tanh_input.json"; 53 | string pathOutput = Resources.TestsFolder + "test_tanh_output.json"; 54 | 55 | Utils.KerasModelTest(pathInput, pathModel, pathOutput); 56 | } 57 | 58 | private TanHLayer relu; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /UnitTests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /cppcore/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | project(cppcore) 3 | 4 | message("Start generating!") 5 | 6 | 7 | set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/thirdparty") 8 | if(BACKEND STREQUAL "tensorflow") 9 | 10 | find_package(TF REQUIRED) 11 | include("implementation_tf/GenTF.cmake") 12 | 13 | elseif(BACKEND STREQUAL "cntk") 14 | 15 | find_package(CNTK REQUIRED) 16 | include("implementation_cntk/GenCNTK.cmake") 17 | 18 | else() 19 | 20 | message("Unknown backand!") 21 | 22 | endif() 23 | 24 | -------------------------------------------------------------------------------- /cppcore/README.md: -------------------------------------------------------------------------------- 1 | # Highlevel C++ API for deep learning 2 | 3 | ## Vision 4 | 5 | The goal of this subproject is to create a highlevel C++ API which wraps the Tensorflow and CNTK C++ APIs in a Keras like manner and with some extensions. This can be two major advantages: 6 | 7 | * it is easier to bind it to other languages and get a high level API in them without further implementations 8 | * performance losses can be avoided due to the C++ implementation behind the API. 9 | 10 | ## Platform independency 11 | 12 | This subproject can be treated independently from NNSharp which is a C# oriented deep learning library. The C++ API is a cmake-based project and provides platform independent builds. Therefore the API can be used on any platform. 13 | 14 | The necessary third party shared libraries can be downloaded from the following links: 15 | 16 | [CNTK-Windows](https://drive.google.com/file/d/0B97L9zqg-lnwci1CSVFyRDd5ZWM/view?usp=sharing) 17 | 18 | [CNTK-Linux](https://drive.google.com/open?id=0B97L9zqg-lnwUTM4ZC16QlVUZUk) 19 | 20 | [Tensorflow-Windows](https://drive.google.com/open?id=0B97L9zqg-lnwejFEeVRWY1MtUGs) 21 | 22 | [Tensorflow-Linux](https://drive.google.com/open?id=0B97L9zqg-lnwNEE2UDZIUXFhX1U) -------------------------------------------------------------------------------- /cppcore/implementation_cntk/GenCNTK.cmake: -------------------------------------------------------------------------------- 1 | message("Jeee, CNTK!") 2 | 3 | #-------------------------------------------- 4 | # Generating the CNTK project 5 | #-------------------------------------------- 6 | 7 | # Define executable target 8 | set(CORE "${CMAKE_CURRENT_LIST_DIR}/..") 9 | include_directories(${CNTK_INCLUDE_DIR} ${CMAKE_BINARY_DIR} ${CORE}) 10 | 11 | add_executable(cppcore nnsharp.h implementation_cntk/cppcore.cpp) 12 | 13 | target_link_libraries(cppcore ${CNTK_LIB}) 14 | 15 | set(GCC_COMPILE_FLAG "-std=c++11") 16 | add_definitions(${GCC_COMPILE_FLAG}) 17 | 18 | # Copy CNTK DLLs to output folder on Windows 19 | if(WIN32) 20 | foreach(DLL ${CNTK_DLL}) 21 | add_custom_command(TARGET cppcore POST_BUILD COMMAND 22 | ${CMAKE_COMMAND} -E copy_if_different ${DLL} $) 23 | endforeach() 24 | endif() 25 | -------------------------------------------------------------------------------- /cppcore/implementation_tf/GenTF.cmake: -------------------------------------------------------------------------------- 1 | message("Jeee, TF!") 2 | 3 | #-------------------------------------------- 4 | # Generating the TF project 5 | #-------------------------------------------- 6 | 7 | # Define executable target 8 | set(CORE "${CMAKE_CURRENT_LIST_DIR}/..") 9 | include_directories(${TF_INCLUDE_DIR} ${CMAKE_BINARY_DIR} ${CORE}) 10 | 11 | add_library(cppcore SHARED nnsharp.h implementation_tf/cppcore.cpp) 12 | 13 | target_link_libraries(cppcore ${TF_LIB}) 14 | 15 | # Copy TF DLLs to output folder on Windows 16 | #if(WIN32) 17 | #foreach(DLL ${TF_DLL}) 18 | #add_custom_command(TARGET cppcore POST_BUILD COMMAND 19 | #${CMAKE_COMMAND} -E copy_if_different ${DLL} #$) 20 | #endforeach() 21 | #endif() -------------------------------------------------------------------------------- /cppcore/implementation_tf/cppcore.cpp: -------------------------------------------------------------------------------- 1 | // TryCNTKcpp.cpp : Defines the entry point for the console application. 2 | // 3 | 4 | #include 5 | #include 6 | 7 | //region: TF MINI TEST 8 | 9 | #define COMPILER_MSVC 10 | #include "tensorflow.h" 11 | #include "nnsharp.h" 12 | 13 | void tf_test() { 14 | 15 | std::cout << "Hi!" << std::endl; 16 | 17 | const char* version = TF_Version(); 18 | std::cout << version[0] << version[1] << version[2] << version[3] << version[4] << std::endl; 19 | TF_Buffer* ops = TF_GetAllOpList(); 20 | int d; 21 | std::cin >> d; 22 | 23 | } 24 | 25 | // endregion (TF MINI TEST) 26 | 27 | int main(int argc, char *argv[]) 28 | { 29 | 30 | //CNTK_test(); 31 | //tf_test(); 32 | 33 | 34 | return 0; 35 | } 36 | 37 | // DLL test 38 | 39 | core::tensor::Tensor::Tensor(DataType data_type, int dim, int* shape) { 40 | 41 | if (data_type == DataType::Integer) { 42 | 43 | // Calculate the size. 44 | int length = 1; 45 | for (int i = 0; i < dim; ++i) { 46 | length *= shape[i]; 47 | } 48 | 49 | values = new int[length]; 50 | this->dim = dim; 51 | this->shape = shape; 52 | } 53 | else 54 | values = nullptr; 55 | } 56 | 57 | core::tensor::Tensor::~Tensor() { 58 | if (values != nullptr) 59 | delete[] values; 60 | } 61 | 62 | core::tensor::TensorInteger::TensorInteger(int size): 63 | Tensor(DataType::Integer, 1, &size){ 64 | } 65 | 66 | void core::tensor::TensorInteger::Get(int indices, int* value_out) const { 67 | const char* version = TF_Version(); 68 | std::cout << version[0] << version[1] << version[2] << version[3] << version[4] << std::endl; 69 | *value_out = version[1]; 70 | } 71 | 72 | void core::tensor::TensorInteger::Set(int idx, int value_in){ 73 | 74 | } 75 | 76 | core::tensor::TensorInteger* core::tensor::create_tensor_integer(int size) 77 | { 78 | return new TensorInteger(size); 79 | } 80 | 81 | int core::tensor::tensor_integer_get(TensorInteger * tensor_in, int idx) 82 | { 83 | int retVal = 0; 84 | tensor_in->Get(idx, &retVal); 85 | 86 | return retVal; 87 | } 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /cppcore/nnsharp.h: -------------------------------------------------------------------------------- 1 | #ifndef __NNSHARP__ 2 | #define __NNSHARP__ 3 | 4 | #ifndef CORE_EXPORT 5 | # if defined COMPILER_MSVC 6 | # define CORE_EXPORT __declspec(dllexport) 7 | # else 8 | # define CORE_EXPORT __attribute__((visibility("default"))) 9 | # endif 10 | #endif 11 | 12 | #ifndef CORE_EXTERN_C 13 | # if defined __cplusplus 14 | # define CORE_EXTERN_C extern "C" 15 | # else 16 | # define CORE_EXTERN_C 17 | # endif 18 | #endif 19 | 20 | #if defined WIN32 || _WIN32 21 | # define CORE_CDECL __cdecl 22 | # define CORE_STDCALL __stdcall 23 | #else 24 | # define CORE_CDECL 25 | # define CORE_STDCALL 26 | #endif 27 | 28 | #ifndef CORE 29 | # define CORE(rettype) CORE_EXTERN_C CORE_EXPORT rettype CORE_CDECL 30 | #endif 31 | 32 | #include 33 | 34 | namespace core { 35 | 36 | struct CORE_EXPORT nnsharp_status { 37 | 38 | nnsharp_status(int id, char* message) : 39 | id(id), message(message) {} 40 | 41 | int id; 42 | char* message; 43 | }; 44 | typedef nnsharp_status* nnsharp_status_p; 45 | 46 | namespace tensor { 47 | 48 | enum class DataType 49 | { 50 | Integer, 51 | Float, 52 | Double, 53 | Binary 54 | }; 55 | 56 | struct Tensor { 57 | 58 | public: 59 | Tensor(DataType data_type, int dim, int* shape); 60 | 61 | ~Tensor(); 62 | 63 | protected: 64 | 65 | DataType data_type; 66 | void* values; 67 | int dim; 68 | int* shape; 69 | }; 70 | 71 | class TensorInteger : public Tensor { 72 | 73 | public: 74 | TensorInteger(int size); 75 | 76 | // Access 77 | void Get(int idx, int* value_out) const; 78 | void Set(int idx, int value_in); 79 | }; 80 | 81 | CORE(TensorInteger*) create_tensor_integer(int size); 82 | CORE(int) tensor_integer_get(TensorInteger* tensor_in, int idx); 83 | } 84 | 85 | } // core 86 | 87 | #endif // __NNSHARP__ -------------------------------------------------------------------------------- /cppcore/thirdparty/FindTF.cmake: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------- 2 | # Goal: finding the necessary components of tensorflow 3 | # on the current platform. 4 | # 5 | # Sets variables: 6 | # TF_INCLUDE_DIR 7 | # TF_LIB 8 | # TF_DLL 9 | #----------------------------------------------------- 10 | 11 | include(FindPackageHandleStandardArgs) 12 | 13 | set(TF_ROOT "${CMAKE_CURRENT_LIST_DIR}/tf") 14 | if(WIN32) 15 | if (EXISTS "${TF_ROOT}/tensorflow.h") 16 | set(TF_INCLUDE_DIR "${TF_ROOT}") 17 | set(TF_LIB "${TF_ROOT}/tensorflow.lib") 18 | set(TF_DLL "${TF_ROOT}/tensorflow.dll") 19 | endif() 20 | mark_as_advanced(TF_ROOT) 21 | find_package_handle_standard_args(TF DEFAULT_MSG TF_INCLUDE_DIR TF_LIB TF_DLL) 22 | 23 | else() 24 | if (EXISTS "${TF_ROOT}/tensorflow.h") 25 | set(TF_INCLUDE_DIR "${TF_ROOT}") 26 | set(TF_LIB "${TF_ROOT}/libtensorflow.so") 27 | endif() 28 | mark_as_advanced(TF_ROOT) 29 | find_package_handle_standard_args(TF DEFAULT_MSG TF_INCLUDE_DIR TF_LIB) 30 | 31 | endif() 32 | -------------------------------------------------------------------------------- /docs/custom_theme/img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamtiger/NNSharp/b7ab012f76b1a25fe9c38863c0371d5a3e862165/docs/custom_theme/img/favicon.ico -------------------------------------------------------------------------------- /docs/docs/batchnormalization.md: -------------------------------------------------------------------------------- 1 | [[source]](https://github.com/adamtiger/NNSharp/blob/master/NNSharp/Kernels/CPUKernels/BatchNormKernel.cs#L11) 2 | ## Batch Normalization 3 | 4 | The intuition behind Batch Normalization is that the distribution of each layer's inputs changes during training, as the parameters of the previous layers change. This tends to slow down the learning process. Normalization transforms the input data in a way that its mean becomes 0 and its standard deviation becomes 1. For further details see the original article: 5 | 6 | [Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift](https://arxiv.org/abs/1502.03167). 7 | 8 | **Input:** 9 | 10 | A Data2D array with arbitrary shape. 11 | 12 | **Output:** 13 | 14 | The same as the input. The numbers are normalized in the channels separately. 15 | 16 | **Methods:** 17 | 18 | The following equations are implemented. *The normalization is performed according to the feature (channel) axis.* 19 | 20 | ![batchnorm](https://drive.google.com/uc?export=download&id=0B97L9zqg-lnwaTlqbEZvOXpCOGM) 21 | 22 | The **gamma**, **beta** are parameters to learn, **sigma** is the variance, **b** is the bias. The **epsilon** is a small number to avoid the problem with small variances. (Ensures numerical stability.) 23 | -------------------------------------------------------------------------------- /docs/docs/install.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | [![NuGet Badge](https://buildstats.info/nuget/NNSharp)](https://www.nuget.org/packages/NNSharp) 4 | 5 | NNSharp is available as a [nuget package](https://www.nuget.org/packages/NNSharp/). It was built on Visual Studio 2015 with .NET Framework 4.5.2. The easiest way to install is to use the [nuget package manager](https://docs.microsoft.com/en-us/nuget/tools/package-manager-ui) in Visual Studio. As an other option the necessary dll can be built from the source code ([download link](https://github.com/adamtiger/NNSharp/archive/v1.1.zip)). The current release is the v1.1. It has only one dependency: [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/). 6 | -------------------------------------------------------------------------------- /docs/docs/models.md: -------------------------------------------------------------------------------- 1 | # Models 2 | 3 | ## Interface: IModel 4 | 5 | This is the interface for all of the models. It contains only one function which returns the structure of the model. This is important to get access to the model details. 6 | 7 | ## Sequential model 8 | [[source]](https://github.com/adamtiger/NNSharp/blob/dev/NNSharp/Models/SequentialModel.cs#L15) 9 | 10 |
11 | The smallest building block of the sequential model is the **layer**. The layers are organised consequtively. The output of a layer is the input to the next layer. 12 | 13 | NNSharp architecture is based on general descriptors of the possible operations (called kernels in this context) and then it applies a compiler to get the executable model. The compilation is done when a reader function (see Keras part for example) is called. The reader function reads the descriptors, builds a model then compiles it. After compilation calculations on new date reuires to call: 14 | 15 | IData ExecuteNetwork(IData input) 16 | 17 | Where IData can be a Data2D data type (or later Data3D). At the detailed descriptions the exact meaning of the data format for the particular operation is described. 18 | 19 | Dimension GetInputDimension() 20 | 21 | Gives a structure, Dimension, containing the dimension of the expected input data. 22 | 23 | IModelData GetSummary() 24 | 25 | Gives an object of **SequentialModelData** type. Therefore it is necessary to cast the output for that type. For the details of **SequentialModelData** see the documentation's *Data structure* part. 26 | 27 | 28 | ## Graph model 29 | -------------------------------------------------------------------------------- /docs/docs/startkeras.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Keras models 2 | 3 | If the training was done with Keras then the model can be saved by using the python script named **KerasModeltoJSON.py**. It will create a json file which can be read by NNSharp. NNSharp will build the model in C# and provides function to execute it. The python script takes the created and compiled Keras *model* and the *output file name* as arguments. Then the json file can be created in the following way in your Python program: 4 | 5 | ```python 6 | import KerasModeltoJSON as js 7 | wrt = js.JSONwriter(model, file_path) 8 | wrt.save() 9 | ``` 10 | 11 | Then in the C# program use the following: 12 | 13 | ```csharp 14 | // Read the previously created json. 15 | var reader = new ReaderKerasModel(filePath); 16 | SequentialModel model = reader.GetSequentialExecutor(); 17 | 18 | // Then create the data to run the executer on. 19 | // batch: should be set in the Keras model. 20 | Data2D input = new Data2D(height, width, channel, batch); 21 | 22 | // Calculate the network's output. 23 | IData output = model.ExecuteNetwork(input); 24 | ``` 25 | 26 | In order to know what data should be the *input* and the *output* see the corresponding documentations for the *layer at input* and the *layer at output*, respectively. 27 | -------------------------------------------------------------------------------- /docs/docs/starttensorflow.md: -------------------------------------------------------------------------------- 1 | (Next release) 2 | -------------------------------------------------------------------------------- /docs/mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: NNSharp Documentation 2 | site_author: Adam Budai 3 | pages: 4 | - Overview: index.md 5 | - Installation: install.md 6 | - Models: models.md 7 | - Data structure: data.md 8 | - Keras: 9 | - Quick start: startkeras.md 10 | - Core: core.md 11 | - Convolutions: convolutions.md 12 | - Pooling: pooling.md 13 | - Activations: activations.md 14 | - Recurrent layers: recurrents.md 15 | - BatchNormalization: batchnormalization.md 16 | - TensorFlow: 17 | - Quick start: starttensorflow.md 18 | 19 | theme: readthedocs 20 | theme_dir: custom_theme 21 | repo_url: https://github.com/adamtiger/NNSharp 22 | --------------------------------------------------------------------------------