예제 #1
0
        public void Serialize(NeuralLayeredNetwork network, Stream stream)
        {
            var             data      = network.GetNetworkInfo();
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, data);
        }
예제 #2
0
        public NeuralLayeredNetwork Deserialize(Stream stream)
        {
            using var reader = new StreamReader(stream);
            var json    = reader.ReadToEnd();
            var options = new JsonSerializerOptions
            {
                Converters    = { new LayerInfoConverter() },
                WriteIndented = true
            };

            var info = JsonSerializer.Deserialize <NetworkInfo>(json, options);

            var inputShape = new Shape(info.InputShape.B, info.InputShape.C, info.InputShape.H, info.InputShape.W);
            var network    = new NeuralLayeredNetwork(inputShape);

            foreach (var layerInfo in info.LayersInfo)
            {
                var layerType = Type.GetType(layerInfo.LayerType);
                if (layerType is null)
                {
                    throw new ArgumentException();
                }
                var layer = (BaseLayer)Activator.CreateInstance(layerType, layerInfo);
                network.AddLayer(layer);
            }
            return(network);
        }
예제 #3
0
        public NeuralLayeredNetwork Deserialize(string filePath)
        {
            FileInfo file = new FileInfo(filePath);

            if (!file.Exists)
            {
                throw new ArgumentException($"File {filePath} does not exists");
            }
            if (file.Extension != ".cnn")
            {
                throw new ArgumentException($"File {filePath} has wrong format");
            }

            NetworkInfo     info;
            BinaryFormatter formatter = new BinaryFormatter();

            using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                info = (NetworkInfo)formatter.Deserialize(stream);
            }
            var inputShape = new Shape(info.InputShape.B, info.InputShape.C, info.InputShape.H, info.InputShape.W);
            var network    = new NeuralLayeredNetwork(inputShape);

            foreach (var layerInfo in info.LayersInfo)
            {
                var layerType = Type.GetType(layerInfo.LayerType);
                if (layerType is null)
                {
                    throw new ArgumentException();
                }
                var layer = (BaseLayer)Activator.CreateInstance(layerType, layerInfo);
                network.AddLayer(layer);
            }
            return(network);
        }
예제 #4
0
        public override void TrainModel(NeuralLayeredNetwork network)
        {
            base.TrainModel(network);
            Stopwatch sw = Stopwatch.StartNew();

            for ( ; Epoch <= EpochsCount; Epoch++)
            {
                Iteration = 1;
                foreach (var example in TrainingExamples)
                {
                    Network.Forward(example.Input);
                    CalculateLoss(example.Output);
                    Network.Output.LossDerivative(example.Output, LossFunction, Dy);
                    Network.Backward(Dy);
                    CorrectWeights();

                    var result = new IterationResult
                    {
                        Epoch            = this.Epoch,
                        Iteration        = this.Iteration,
                        IterationTime    = sw.Elapsed,
                        ExamplesPerEpoch = ExamplesCount,
                        Loss             = this.Loss.Storage.Data.Average(),
                        Accuracy         = Metric?.Evaluate(example.Output, Network.Output) ?? 0,
                        EpochsCount      = this.EpochsCount
                    };
                    RaiseIterationFinishedEvent(result);
                    Iteration++;
                    sw.Restart();
                }

                RaiseEpochFinishedEvent(new EpochResult());
            }
        }
예제 #5
0
        public void Serialize(NeuralLayeredNetwork network, string filePath)
        {
            FileInfo file = new FileInfo(filePath);

            if (file.Extension != ".json")
            {
                throw new ArgumentException($"File {filePath} has wrong format");
            }

            using var stream = new FileStream(filePath, FileMode.Create);
            Serialize(network, stream);
        }
예제 #6
0
        public void Serialize(NeuralLayeredNetwork network, Stream stream)
        {
            var data = network.GetNetworkInfo();

            using TextWriter writer = new StreamWriter(stream);
            var options = new JsonSerializerOptions
            {
                Converters    = { new LayerInfoConverter() },
                WriteIndented = true
            };
            var json = JsonSerializer.Serialize(data, options);

            writer.Write(json);
        }
예제 #7
0
        public void Serialize(NeuralLayeredNetwork network, string filePath)
        {
            FileInfo file = new FileInfo(filePath);

            if (file.Extension != ".cnn")
            {
                throw new ArgumentException($"File {filePath} has wrong format");
            }

            var             data      = network.GetNetworkInfo();
            BinaryFormatter formatter = new BinaryFormatter();

            using var stream = new FileStream(filePath, FileMode.OpenOrCreate);
            formatter.Serialize(stream, data);
        }
예제 #8
0
        public NeuralLayeredNetwork Deserialize(string filePath)
        {
            FileInfo file = new FileInfo(filePath);

            if (!file.Exists)
            {
                throw new ArgumentException($"File {filePath} does not exists");
            }
            if (file.Extension != ".json")
            {
                throw new ArgumentException($"File {filePath} has wrong format");
            }

            NetworkInfo info;

            using (var stream = new FileStream(filePath, FileMode.Open))
            {
                using var reader = new StreamReader(stream);
                var json    = reader.ReadToEnd();
                var options = new JsonSerializerOptions
                {
                    Converters    = { new LayerInfoConverter() },
                    WriteIndented = true
                };

                info = JsonSerializer.Deserialize <NetworkInfo>(json, options);
            }
            var inputShape = new Shape(info.InputShape.B, info.InputShape.C, info.InputShape.H, info.InputShape.W);
            var network    = new NeuralLayeredNetwork(inputShape);

            foreach (var layerInfo in info.LayersInfo)
            {
                var layerType = Type.GetType(layerInfo.LayerType);
                if (layerType is null)
                {
                    throw new ArgumentException();
                }
                var layer = (BaseLayer)Activator.CreateInstance(layerType, layerInfo);
                network.AddLayer(layer);
            }
            return(network);
        }
예제 #9
0
        public NeuralLayeredNetwork Deserialize(Stream stream)
        {
            NetworkInfo     info;
            BinaryFormatter formatter = new BinaryFormatter();

            info = (NetworkInfo)formatter.Deserialize(stream);
            var inputShape = new Shape(info.InputShape.B, info.InputShape.C, info.InputShape.H, info.InputShape.W);
            var network    = new NeuralLayeredNetwork(inputShape);

            foreach (var layerInfo in info.LayersInfo)
            {
                var layerType = Type.GetType(layerInfo.LayerType);
                if (layerType is null)
                {
                    throw new ArgumentException();
                }
                var layer = (BaseLayer)Activator.CreateInstance(layerType, layerInfo);
                network.AddLayer(layer);
            }
            return(network);
        }
예제 #10
0
        static void Main(string[] args)
        {
            Global.ComputationType = ComputationType.Cpu;
            NeuralLayeredNetwork network = new NeuralLayeredNetwork(new Shape(BatchSize, 1, 28, 28));

            network
            .Conv(8, 3, 1, new HeInitializer())
            .Relu()
            .MaxPool(2, 2)
            .Flatten()
            .Fully(64, new HeInitializer())
            .Relu()
            .Fully(10, new HeInitializer())
            .Softmax();

            _trainExamples = Dataset.CreateTrainDataset(BatchSize);
            _testExamples  = Dataset.CreateTestDataset(BatchSize);

            var optimizerFactory = ComponentsFactories.OptimizerFactory;
            var metricFactory    = ComponentsFactories.MetricFactory;

            BaseTrainer trainer = new MiniBatchTrainer(_trainExamples, new MiniBatchTrainerSettings
            {
                EpochsCount  = 1,
                BatchSize    = BatchSize,
                LossFunction = new CrossEntropy(),
                Optimizer    = optimizerFactory.CreateAdam(0.01f),
                Metric       = metricFactory.CreateClassificationAccuracy() //Warning: GPU metrics have not implemented yet
            });

            trainer.AddEventHandler(new ConsoleLogger());
            trainer.TrainModel(network);

            ITester tester = new ClassificationTester(_testExamples);

            Console.WriteLine("Testing...");
            var testResult = tester.TestModel(network);

            Console.WriteLine(testResult);
        }
예제 #11
0
 public NeuralNetworkComponent(int inputSize)
 {
     _inputBuilder = new CpuBuilder();
     _network      = CreateNetwork(inputSize);
 }
예제 #12
0
 public virtual void TrainModel(NeuralLayeredNetwork network)
 {
     Network = network;
     Epoch   = 1;
     InitializeTraining();
 }
예제 #13
0
        static void Main(string[] args)
        {
            #region Reading Data

            string[]  fields     = new string[ParametersCount];
            float[][] parameters = new float[RowsCount][];
            float[]   output     = new float[RowsCount];

            for (int i = 0; i < parameters.Length; i++)
            {
                parameters[i] = new float[ParametersCount];
            }

            using (StreamReader streamReader = new StreamReader(DataFilePath))
            {
                using (CsvReader csvReader = new CsvReader(streamReader, new CultureInfo("en-US")))
                {
                    csvReader.Configuration.Delimiter = ",";

                    csvReader.Read();
                    for (int i = 0; i < ParametersCount; i++)
                    {
                        fields[i] = csvReader.GetField <string>(i + 1);
                    }

                    var index = 0;
                    while (csvReader.Read())
                    {
                        for (int i = 0; i < ParametersCount; i++)
                        {
                            parameters[index][i] = csvReader.GetField <float>(i + 1);
                        }

                        output[index] = csvReader.GetField <float>(ParametersCount + 1);
                        index++;
                    }
                }
            }

            #endregion

            #region Data standartization

            float[] mean      = new float[ParametersCount];
            float[] deviation = new float[ParametersCount];
            for (int i = 0; i < ParametersCount; i++)
            {
                for (int j = 0; j < RowsCount; j++)
                {
                    mean[i]      += parameters[j][i];
                    deviation[i] += MathF.Pow(parameters[j][i] - mean[i], 2);
                }

                mean[i]     /= RowsCount;
                deviation[i] = MathF.Sqrt(deviation[i]) / RowsCount;
            }

            for (int i = 0; i < RowsCount; i++)
            {
                for (int j = 0; j < ParametersCount; j++)
                {
                    parameters[i][j] = (parameters[i][j] - mean[j]) / deviation[j];
                }
            }

            #endregion

            #region Training

            var examples = new List <Example>();
            var builder  = TensorBuilder.Create();
            for (int i = 0; i < RowsCount; i++)
            {
                var inTensor  = builder.OfShape(new Shape(1, 1, 1, ParametersCount));
                var outTensor = builder.OfShape(Shape.ForScalar());

                inTensor.Storage.Data = parameters[i];
                outTensor[0]          = output[i];
                examples.Add(new Example
                {
                    Input  = inTensor,
                    Output = outTensor
                });
            }

            var network = new NeuralLayeredNetwork(new Shape(1, 1, 1, ParametersCount));
            network
            .Fully(64)
            .Relu()
            .Fully(1);

            var optimizerFactory = ComponentsFactories.OptimizerFactory;
            var metricFactory    = ComponentsFactories.MetricFactory;

            var trainer = new MiniBatchTrainer(examples, new MiniBatchTrainerSettings
            {
                BatchSize    = 32,
                EpochsCount  = 50,
                LossFunction = new MeanSquaredError(),
                Metric       = metricFactory.CreateMAE(), //Warning: GPU metrics have not implemented yet
                Optimizer    = optimizerFactory.CreateAdam(0.001f)
            });

            trainer.AddEventHandler(new ConsoleLogger());
            trainer.TrainModel(network);

            #endregion
        }