コード例 #1
0
        static float QuadraticWholeCost(NeuralNet neuralNet, LearningExample[] learningExamples)
        {
            float wholeCost = 0;

            for (int i = 0; i < learningExamples.Length; i++)
            {
                wholeCost += QuadraticSingleCost(learningExamples[i].Output, neuralNet.Compute(learningExamples[i].Input));
            }
            return(wholeCost / learningExamples.Length);
        }
コード例 #2
0
        static float CrossEntropyWholeCost(NeuralNet neuralNet, LearningExample[] learningExamples)
        {
            float wholeCost = 0;

            for (int i = 0; i < learningExamples.Length; i++)
            {
                float singleCost = CrossEntropySingleCost(learningExamples[i].Output, neuralNet.Compute(learningExamples[i].Input));
                wholeCost += singleCost;
            }
            return(-wholeCost / learningExamples.Length);
        }
コード例 #3
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            NeuralNet neuralNet = target as NeuralNet;

            if (neuralNet.Initialized)
            {
                EditorGUILayout.HelpBox(string.Format("InputSize: {0}\nCost funtion: {1}", neuralNet.InputSize, neuralNet.CostFunction), MessageType.Info, true);
            }
            else
            {
                EditorGUILayout.HelpBox("Not initialized", MessageType.Info, true);
            }
        }
コード例 #4
0
        static float L1Cost(NeuralNet neuralNet, int numberOfTests)
        {
            float l1Cost = 0;

            for (int layer = 0; layer < neuralNet.NumberOfLayers; layer++)
            {
                for (int node = 0; node < neuralNet.NodesInLayer(layer); node++)
                {
                    for (int weight = 0; weight < neuralNet.WeightsOfNodeInLayer(layer); weight++)
                    {
                        l1Cost += Mathf.Abs(neuralNet.WeightAccessor[layer, node, weight]);
                    }
                }
            }

            return(l1Cost * neuralNet.RegularizationRate / numberOfTests);
        }
コード例 #5
0
        public static float ComputeCost(NeuralNet neuralNet, LearningExample[] learningExamples)
        {
            float cost = 0;

            // C0
            switch (neuralNet.CostFunction)
            {
            case NeuralNet.CostFuntionKind.Quadratic:
                cost = QuadraticWholeCost(neuralNet, learningExamples);
                break;

            case NeuralNet.CostFuntionKind.CrossEntropy:
                cost = CrossEntropyWholeCost(neuralNet, learningExamples);
                break;

            default:
                throw new System.NotImplementedException();
            }

            // Regularization
            switch (neuralNet.RegularizationMethod)
            {
            case NeuralNet.RegularizationMethodKind.None:
                break;

            case NeuralNet.RegularizationMethodKind.L2:
                cost += L2Cost(neuralNet, learningExamples.Length);
                break;

            case NeuralNet.RegularizationMethodKind.L1:
                cost += L1Cost(neuralNet, learningExamples.Length);
                break;

            default:
                throw new System.NotImplementedException();
            }

            //
            return(cost);
        }