Exemple #1
0
        private void DrawAdaGrad(Graphics graphics, DrawF draw)
        {
            ArrayList history   = new ArrayList();
            PointF2D  point     = new PointF2D(-4.0f, 2.0f);
            Function2 fun       = new Function2();
            AdaGrad   optimizer = new AdaGrad(0.7f);

            for (int index = 0; index < 30; index++)
            {
                PointF2D xyPoint = draw.getBlockPoint(point.X, point.Y);
                history.Add(xyPoint);
                PointF2D diff = fun.DiffFormula(point.X, point.Y);
                optimizer.Update(point, diff);
            }

            PointF2D prePoint = ((PointF2D)history[0]);

            for (int index = 0; index < 30; index++)
            {
                draw.drawPoint(graphics, Brushes.Blue, ((PointF2D)history[index]));
                draw.drawLine(graphics, prePoint, ((PointF2D)history[index]));
                prePoint = ((PointF2D)history[index]);
            }
        }
Exemple #2
0
        public void InitNetwork(ECostType costType, CostSettings costSettings, EOptimizerType optimizerType, OptimizerSettings optimizerSettings)
        {
            Utility.Dims InShape;
            Utility.Dims OutShape;
            Utility.Dims WShape;

            for (int i = 1; i < Layers.Count; i++)
            {
                Data.Data["a" + i.ToString()] = new Matrix(Layers[i].NCount, 1);
                InShape = new Utility.Dims(Layers[i].NCount, 1);

                Data.Data["b" + i.ToString()] = Matrix.RandomMatrix(Layers[i].NCount, 1, 1, EDistrubution.Gaussian);

                OutShape = new Utility.Dims(Layers[i].NCount, 1);

                Data.Data["W" + i.ToString()] = Matrix.RandomMatrix(Layers[i - 1].NCount, Layers[i].NCount, 1, EDistrubution.Gaussian);
                WShape = new Utility.Dims(Layers[i - 1].NCount, Layers[i].NCount);

                Layers[i].SetSettings(new LayerSettings(InShape, OutShape, WShape));
            }

            Data.Data["a0"] = new Matrix(Layers[0].NCount, 1);
            InShape         = new Utility.Dims(Layers[0].NCount, 1);

            Data.Data["b0"] = new Matrix(Layers[0].NCount, 1);
            OutShape        = new Utility.Dims(Layers[0].NCount, 1);

            Data.Data["W0"] = new Matrix(Layers[0].NCount * Layers[1].NCount, Layers[1].NCount);
            WShape          = new Utility.Dims(Layers[0].NCount * Layers[1].NCount, Layers[1].NCount);

            Layers[0].SetSettings(new LayerSettings(InShape, OutShape, WShape));

            switch (costType)
            {
            case ECostType.Invalid:
                throw new ArgumentException("Invalid Cost Function Selected!");

            case ECostType.CrossEntropyCost:
                CostFunction = new CrossEntropyCost((CrossEntropyCostSettings)costSettings);
                break;

            case ECostType.ExponentionalCost:
                CostFunction = new ExponentionalCost((ExponentionalCostSettings)costSettings);
                break;

            case ECostType.GeneralizedKullbackLeiblerDivergence:
                CostFunction = new GeneralizedKullbackLeiblerDivergence((GeneralizedKullbackLeiblerDivergenceSettings)costSettings);
                break;

            case ECostType.HellingerDistance:
                CostFunction = new HellingerDistance((HellingerDistanceSettings)costSettings);
                break;

            case ECostType.ItakuraSaitoDistance:
                CostFunction = new ItakuraSaitoDistance((ItakuraSaitoDistanceSettings)costSettings);
                break;

            case ECostType.KullbackLeiblerDivergence:
                CostFunction = new KullbackLeiblerDivergence((KullbackLeiblerDivergenceSettings)costSettings);
                break;

            case ECostType.QuadraticCost:
                CostFunction = new QuadraticCost((QuadraticCostSettings)costSettings);
                break;

            default:
                throw new ArgumentException("Invalid Cost Function Selected!");
            }

            switch (optimizerType)
            {
            case EOptimizerType.Invalid:
                throw new ArgumentException("Invalid Optimizer Function Selected!");

            case EOptimizerType.AdaDelta:
                OptimizerFunction = new AdaDelta((AdaDeltaSettings)optimizerSettings);
                break;

            case EOptimizerType.AdaGrad:
                OptimizerFunction = new AdaGrad((AdaGradSettings)optimizerSettings);
                break;

            case EOptimizerType.Adam:
                OptimizerFunction = new Adam((AdamSettings)optimizerSettings);
                break;

            case EOptimizerType.Adamax:
                OptimizerFunction = new Adamax((AdamaxSettings)optimizerSettings);
                break;

            case EOptimizerType.GradientDescent:
                OptimizerFunction = new GradientDescent((GradientDescentSettings)optimizerSettings);
                break;

            case EOptimizerType.Momentum:
                OptimizerFunction = new Momentum((MomentumSettings)optimizerSettings);
                break;

            case EOptimizerType.Nadam:
                OptimizerFunction = new Nadam((NadamSettings)optimizerSettings);
                break;

            case EOptimizerType.NesterovMomentum:
                OptimizerFunction = new NesterovMomentum((NesterovMomentumSettings)optimizerSettings);
                break;

            case EOptimizerType.RMSProp:
                OptimizerFunction = new RMSProp((RMSPropSettings)optimizerSettings);
                break;

            default:
                throw new ArgumentException("Invalid Optimizer Function Selected!");
            }
        }