Inheritance: BasicTraining, ILearningRate
        public void TestSOM()
        {
            // create the training set
            IMLDataSet training = new BasicMLDataSet(
                SOMInput, null);

            // Create the neural network.
            var network = new SOMNetwork(4, 2) {Weights = new Matrix(MatrixArray)};

            var train = new BasicTrainSOM(network, 0.4,
                                          training, new NeighborhoodSingle()) {ForceWinner = true};
            int iteration = 0;

            for (iteration = 0; iteration <= 100; iteration++)
            {
                train.Iteration();
            }

            IMLData data1 = new BasicMLData(
                SOMInput[0]);
            IMLData data2 = new BasicMLData(
                SOMInput[1]);

            int result1 = network.Classify(data1);
            int result2 = network.Classify(data2);

            Assert.IsTrue(result1 != result2);
        }
 public void Init(IMLTrain train)
 {
     basicTrainSOM = train as BasicTrainSOM;
     if (basicTrainSOM == null)
     {
         throw new ArgumentException(
             String.Format("Argument shoud be of {0} type.", typeof(BasicTrainSOM)), "train");
     }
 }
        /// <summary>
        /// Uczy siec Kohonena z podanymi parametrami.
        /// </summary>
        /// <param name="learningRate">Początkowy współczynnik nauki.</param>
        /// <param name="learningChangeRate">Współczynnik zmiany współczynnika nauki</param>
        /// <param name="neighbourhoodRate">Początkowy współczynnik sąsiedztwa.</param>
        /// <param name="neighbourhoodChangeRate">Współczynnik zmiany współczynnika sąsiedztwa.</param>
        /// <param name="trainIterations">Na ilu przykladach przebiega nauka.</param>
        /// <param name="learningSet">Zbiór danych uczących.</param>
        public void Train(double learningRate, double learningChangeRate,
            double neighbourhoodRate, double neighbourhoodChangeRate,
            int trainIterations, InputDataSet learningSet)
        {
            var basicMlDataSet = new BasicMLDataSet(learningSet.InputSet, null);
            INeighborhoodFunction neighborhoodFunc =
                new KohonenNeighbourhoodFunction(neighbourhoodRate, rows, columns);
            var train = new BasicTrainSOM(network, learningRate, basicMlDataSet, neighborhoodFunc);
            IStrategy strategy = new KohonenTrainStrategy(learningChangeRate, neighbourhoodChangeRate);
            strategy.Init(train);
            train.Strategies.Add(strategy);

            train.Iteration(trainIterations);
            train.FinishTraining();
        }
        public SOMColors()
        {
            InitializeComponent();

            network = CreateNetwork();
            gaussian = new NeighborhoodRBF(RBFEnum.Gaussian, WIDTH, HEIGHT);
            train = new BasicTrainSOM(network, 0.01, null, gaussian);

            train.ForceWinner = false;

            samples = new List<IMLData>();
            for (int i = 0; i < 15; i++)
            {
                IMLData data = new BasicMLData(3);
                data.Data[0] = RangeRandomizer.Randomize(-1, 1);
                data.Data[1] = RangeRandomizer.Randomize(-1, 1);
                data.Data[2] = RangeRandomizer.Randomize(-1, 1);
                samples.Add(data);
            }

            train.SetAutoDecay(100, 0.8, 0.003, 30, 5);
        }
        /// <summary>
        /// Create a LMA trainer.
        /// </summary>
        ///
        /// <param name="method">The method to use.</param>
        /// <param name="training">The training data to use.</param>
        /// <param name="argsStr">The arguments to use.</param>
        /// <returns>The newly created trainer.</returns>
        public IMLTrain Create(IMLMethod method,
                              IMLDataSet training, String argsStr)
        {
            if (!(method is SupportVectorMachine))
            {
                throw new EncogError(
                    "Neighborhood training cannot be used on a method of type: "
                    + method.GetType().FullName);
            }

            IDictionary<String, String> args = ArchitectureParse.ParseParams(argsStr);
            var holder = new ParamsHolder(args);

            double learningRate = holder.GetDouble(
                MLTrainFactory.PropertyLearningRate, false, 0.7d);
            String neighborhoodStr = holder.GetString(
                MLTrainFactory.PropertyNeighborhood, false, "rbf");
            String rbfTypeStr = holder.GetString(
                MLTrainFactory.PropertyRBFType, false, "gaussian");

            RBFEnum t;

            if (rbfTypeStr.Equals("Gaussian", StringComparison.InvariantCultureIgnoreCase))
            {
                t = RBFEnum.Gaussian;
            }
            else if (rbfTypeStr.Equals("Multiquadric", StringComparison.InvariantCultureIgnoreCase))
            {
                t = RBFEnum.Multiquadric;
            }
            else if (rbfTypeStr.Equals("InverseMultiquadric", StringComparison.InvariantCultureIgnoreCase))
            {
                t = RBFEnum.InverseMultiquadric;
            }
            else if (rbfTypeStr.Equals("MexicanHat", StringComparison.InvariantCultureIgnoreCase))
            {
                t = RBFEnum.MexicanHat;
            }
            else
            {
                t = RBFEnum.Gaussian;
            }

            INeighborhoodFunction nf = null;

            if (neighborhoodStr.Equals("bubble", StringComparison.InvariantCultureIgnoreCase))
            {
                nf = new NeighborhoodBubble(1);
            }
            else if (neighborhoodStr.Equals("rbf", StringComparison.InvariantCultureIgnoreCase))
            {
                String str = holder.GetString(
                    MLTrainFactory.PropertyDimensions, true, null);
                int[] size = NumberList.FromListInt(CSVFormat.EgFormat, str);
                nf = new NeighborhoodRBF(size, t);
            }
            else if (neighborhoodStr.Equals("rbf1d", StringComparison.InvariantCultureIgnoreCase))
            {
                nf = new NeighborhoodRBF1D(t);
            }
            if (neighborhoodStr.Equals("single", StringComparison.InvariantCultureIgnoreCase))
            {
                nf = new NeighborhoodSingle();
            }

            var result = new BasicTrainSOM((SOMNetwork) method,
                                           learningRate, training, nf);

            if (args.ContainsKey(MLTrainFactory.PropertyIterations))
            {
                int plannedIterations = holder.GetInt(
                    MLTrainFactory.PropertyIterations, false, 1000);
                double startRate = holder.GetDouble(
                    MLTrainFactory.PropertyStartLearningRate, false, 0.05d);
                double endRate = holder.GetDouble(
                    MLTrainFactory.PropertyEndLearningRate, false, 0.05d);
                double startRadius = holder.GetDouble(
                    MLTrainFactory.PropertyStartRadius, false, 10);
                double endRadius = holder.GetDouble(
                    MLTrainFactory.PropertyEndRadius, false, 1);
                result.SetAutoDecay(plannedIterations, startRate, endRate,
                                    startRadius, endRadius);
            }

            return result;
        }
Ejemplo n.º 6
0
        public void TestSOM2()
        {
            // create the training set
            IMLDataSet training = new BasicMLDataSet(
                SOMInput2, null);

            // Create the neural network.
            var network = new SOMNetwork(4,4);

            var train = new BasicTrainSOM(network, 0.01,
                                       training, new NeighborhoodSingle()) { ForceWinner = true };

            int iteration = 0;

            for (iteration = 0; iteration <= 1000; iteration++)
            {
                train.Iteration();
            }

            IMLData data1 = new BasicMLData(
                SOMInput2[2]);
            IMLData data2 = new BasicMLData(
                SOMInput2[0]);

            IMLData data3 = new BasicMLData(
               SOMInput2[1]);
            IMLData data4 = new BasicMLData(
                SOMInput2[3]);

            int result1 = network.Classify(data1);
            int result2 = network.Classify(data2);
            int result3 = network.Classify(data3);
            int result4 = network.Classify(data4);

            Console.WriteLine("Winner in someinput 2 "+network.Winner(new BasicMLData(SOMInput2[0])));

            Console.WriteLine("First  :" +result1);
            Console.WriteLine("Second "+result2);
            Console.WriteLine("Third  :" + result3);
            Console.WriteLine("Fourth " + result4);

            Assert.IsTrue(result1 != result2);

            train.TrainPattern(new BasicMLData(SOMInput2[2]));
            Console.WriteLine("After training pattern: " + network.Winner(new BasicMLData(SOMInput2[1])));

            var result = new SupportVectorMachine(4, SVMType.SupportVectorClassification, KernelType.Sigmoid);
            training = new BasicMLDataSet(
                SOMInput2, SOMInput2);
            SVMTrain trainsvm = new SVMTrain(result, training);

            trainsvm.Iteration(50);

            result1 = result.Classify(data1);

            result2 = result.Classify(data2);
            result3 = result.Classify(data3);
            result4 = result.Classify(data4);

            Console.WriteLine("SVM classification : EURUSD 1 :"+result1 + "  GBPUSD:"+result2 + " EURCHF :"+result3+  " EURJPY:"+result4 );
        }
Ejemplo n.º 7
0
 public IMLTrain Create(IMLMethod method, IMLDataSet training, string argsStr)
 {
     IDictionary<string, string> dictionary;
     ParamsHolder holder;
     double num;
     string str;
     string str2;
     RBFEnum mexicanHat;
     INeighborhoodFunction function;
     string str3;
     int[] numArray;
     BasicTrainSOM nsom;
     int num2;
     double num3;
     double num4;
     double num6;
     if (method is SupportVectorMachine)
     {
         dictionary = ArchitectureParse.ParseParams(argsStr);
         holder = new ParamsHolder(dictionary);
         num = holder.GetDouble("LR", false, 0.7);
         str = holder.GetString("NEIGHBORHOOD", false, "rbf");
         if (2 != 0)
         {
             goto Label_03DF;
         }
         goto Label_039F;
     }
     goto Label_03F4;
     Label_0083:
     num4 = holder.GetDouble("END_LR", false, 0.05);
     double startRadius = holder.GetDouble("START_RADIUS", false, 10.0);
     if ((((uint) num4) + ((uint) num4)) > uint.MaxValue)
     {
         return nsom;
     }
     if ((((uint) num3) + ((uint) num2)) <= uint.MaxValue)
     {
         num6 = holder.GetDouble("END_RADIUS", false, 1.0);
         nsom.SetAutoDecay(num2, num3, num4, startRadius, num6);
         return nsom;
     }
     Label_00E4:
     if (4 == 0)
     {
         if ((((uint) num3) + ((uint) num2)) > uint.MaxValue)
         {
             goto Label_0292;
         }
         goto Label_02F8;
     }
     Label_00EE:
     nsom = new BasicTrainSOM((SOMNetwork) method, num, training, function);
     do
     {
         if (dictionary.ContainsKey("ITERATIONS"))
         {
             do
             {
                 num2 = holder.GetInt("ITERATIONS", false, 0x3e8);
                 num3 = holder.GetDouble("START_LR", false, 0.05);
             }
             while ((((uint) num3) | 15) == 0);
             goto Label_0083;
         }
     }
     while ((((uint) num6) | 0xff) == 0);
     if (0 == 0)
     {
         if ((((uint) num3) + ((uint) num3)) >= 0)
         {
             return nsom;
         }
         goto Label_03F4;
     }
     if ((((uint) num2) - ((uint) startRadius)) <= uint.MaxValue)
     {
         goto Label_00E4;
     }
     goto Label_0083;
     Label_0184:
     if (!str.Equals("single", StringComparison.InvariantCultureIgnoreCase))
     {
         goto Label_00EE;
     }
     function = new NeighborhoodSingle();
     if ((((uint) num6) - ((uint) num3)) >= 0)
     {
         if ((((uint) num) - ((uint) startRadius)) >= 0)
         {
             goto Label_00E4;
         }
         goto Label_0324;
     }
     if ((((uint) num2) & 0) == 0)
     {
         goto Label_0233;
     }
     Label_01E2:
     while (!str.Equals("rbf1d", StringComparison.InvariantCultureIgnoreCase))
     {
         if (0 == 0)
         {
             if ((((uint) num3) - ((uint) num6)) >= 0)
             {
                 goto Label_0184;
             }
             goto Label_0233;
         }
     }
     function = new NeighborhoodRBF1D(mexicanHat);
     if ((((uint) num2) + ((uint) num)) >= 0)
     {
         if (((uint) num6) < 0)
         {
             goto Label_01E2;
         }
         goto Label_0184;
     }
     if (((uint) num2) < 0)
     {
         goto Label_03DF;
     }
     goto Label_01E2;
     Label_0233:
     function = new NeighborhoodRBF(numArray, mexicanHat);
     goto Label_0184;
     Label_0243:
     if (!str.Equals("rbf", StringComparison.InvariantCultureIgnoreCase))
     {
         if ((((uint) num2) & 0) != 0)
         {
             goto Label_03DF;
         }
         goto Label_01E2;
     }
     Label_0292:
     str3 = holder.GetString("DIM", true, null);
     if ((((uint) num3) + ((uint) num)) > uint.MaxValue)
     {
         goto Label_0292;
     }
     numArray = NumberList.FromListInt(CSVFormat.EgFormat, str3);
     if ((((uint) num6) & 0) == 0)
     {
         goto Label_0233;
     }
     goto Label_0243;
     Label_02F8:
     if (str.Equals("bubble", StringComparison.InvariantCultureIgnoreCase))
     {
         function = new NeighborhoodBubble(1);
         goto Label_0184;
     }
     if ((((uint) num3) & 0) == 0)
     {
         goto Label_0243;
     }
     goto Label_0292;
     Label_0324:
     function = null;
     goto Label_02F8;
     Label_0362:
     mexicanHat = RBFEnum.Multiquadric;
     goto Label_0324;
     Label_039F:
     mexicanHat = RBFEnum.Gaussian;
     goto Label_0324;
     Label_03DF:
     str2 = holder.GetString("RBF_TYPE", false, "gaussian");
     if (str2.Equals("Gaussian", StringComparison.InvariantCultureIgnoreCase))
     {
         goto Label_039F;
     }
     if (((uint) startRadius) <= uint.MaxValue)
     {
         if (str2.Equals("Multiquadric", StringComparison.InvariantCultureIgnoreCase))
         {
             goto Label_0362;
         }
         if (!str2.Equals("InverseMultiquadric", StringComparison.InvariantCultureIgnoreCase) || ((((uint) num2) + ((uint) num2)) < 0))
         {
             if (str2.Equals("MexicanHat", StringComparison.InvariantCultureIgnoreCase))
             {
                 mexicanHat = RBFEnum.MexicanHat;
             }
             else
             {
                 mexicanHat = RBFEnum.Gaussian;
             }
             goto Label_0324;
         }
     }
     else if (((uint) num3) <= uint.MaxValue)
     {
         goto Label_0362;
     }
     if ((((uint) num3) - ((uint) num3)) <= uint.MaxValue)
     {
         mexicanHat = RBFEnum.InverseMultiquadric;
         goto Label_0324;
     }
     goto Label_00E4;
     Label_03F4:
     throw new EncogError("Neighborhood training cannot be used on a method of type: " + method.GetType().FullName);
 }