コード例 #1
0
        private IEnumerator TrainingProcess()
        {
            var neuralNetworkConfig = new NetworkDescription()
            {
                Layers = new List <LayerParamaters>()
                {
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Input,
                        NeuronCount = InputAttributeCount,
                        NeuronType  = ENeruonType.Input
                    },
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Hidden,
                        NeuronCount = 64,
                        NeuronType  = ENeruonType.Sigmoid
                    },
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Hidden,
                        NeuronCount = 16,
                        NeuronType  = ENeruonType.Sigmoid
                    },
                    new LayerParamaters()
                    {
                        LayerType   = ELayerType.Output,
                        NeuronCount = 1,
                        NeuronType  = ENeruonType.Sigmoid
                    },
                }
            };

            var neuralNetwork = new NeuralNetwork(neuralNetworkConfig);

            neuralNetwork.InitNetworkWithRandomValues(-0.05f, 0.05f);

            var networkEvaluator = new NetworkEvaluator(neuralNetwork);


            var tempResult = new NativeArray2D <float>(1, 1);

            //Test inital accuracy
            TestInitialAccuracy(networkEvaluator, tempResult.Slice());

            //Learn over a number of iterations
            for (int epoch = 0; epoch < Epochs; epoch++)
            {
                //For each epoch, perform training
                for (int tc = 0; tc < _dataset.TrainingSetSize; tc++)
                {
                    _dataset.GetTrainingCase(tc, out var trainingInput, out var trainingResult);

                    //Evolve network
                    var jobHandle = networkEvaluator.GradientDescentBackpropigate(trainingInput, trainingResult, 1, out _);
                    jobHandle.Complete();
                }

                float totalError   = 0.0f;
                int   totalCorrect = 0;

                for (int i = 0; i < _dataset.TestingSetSize; i++)
                {
                    _dataset.GetTestCase(i, out var trainingInput, out var trainingResult);
                    networkEvaluator.Evaluate(trainingInput, tempResult.Slice(), 1).Complete();

                    var  error      = Math.Abs(tempResult[0, 0] - trainingResult[0, 0]);
                    bool wasCorrect = error < 0.5f;
                    totalCorrect += wasCorrect ? 1 : 0;
                    totalError   += error;
                }

                float averageError = totalError / (float)_dataset.TestingSetSize;
                float accuracy     = (float)totalCorrect / (float)_dataset.TestingSetSize;

                //Forward test
                Debug.Log($"Epoch {epoch}: Accuracy:{accuracy:P2}  Average Error:{averageError:F4}");

                yield return(null);
            }

            tempResult.BackingStore.Dispose();
        }