nClasses() public abstract method

public abstract nClasses ( ) : int
return int
コード例 #1
0
 public override int nClasses()
 {
     return(_ds.nClasses());
 }
コード例 #2
0
ファイル: MlpClassifier.cs プロジェクト: nickun/OCRonet
 public void InitData(IDataset ds, int nhidden, Intarray newc2i = null, Intarray newi2c = null)
 {
     CHECK_ARG(nhidden > 1 && nhidden < 1000000, "nhidden > 1 && nhidden < 1000000");
     int ninput = ds.nFeatures();
     int noutput = ds.nClasses();
     w1.Resize(nhidden, ninput);
     b1.Resize(nhidden);
     w2.Resize(noutput, nhidden);
     b2.Resize(noutput);
     Intarray indexes = new Intarray();
     NarrayUtil.RPermutation(indexes, ds.nSamples());
     Floatarray v = new Floatarray();
     for (int i = 0; i < w1.Dim(0); i++)
     {
         int row = indexes[i];
         ds.Input1d(v, row);
         float normv = (float)NarrayUtil.Norm2(v);
         v /= normv * normv;
         NarrayRowUtil.RowPut(w1, i, v);
     }
     ClassifierUtil.fill_random(b1, -1e-6f, 1e-6f);
     ClassifierUtil.fill_random(w2, -1.0f / nhidden, 1.0f / nhidden);
     ClassifierUtil.fill_random(b2, -1e-6f, 1e-6f);
     if (newc2i != null)
         c2i.Copy(newc2i);
     if (newi2c != null)
         i2c.Copy(newi2c);
 }
コード例 #3
0
ファイル: MlpClassifier.cs プロジェクト: nickun/OCRonet
 public override void TrainDense(IDataset ds)
 {
     int nclasses = ds.nClasses();
     float miters = PGetf("miters");
     int niters = (int)(ds.nSamples() * miters);
     niters = Math.Max(1000, Math.Min(10000000,niters));
     double err = 0.0;
     Floatarray x = new Floatarray();
     Floatarray z = new Floatarray();
     Floatarray target = new Floatarray(nclasses);
     int count = 0;
     for (int i = 0; i < niters; i++)
     {
         int row = i % ds.nSamples();
         ds.Output(target, row);
         ds.Input1d(x, row);
         TrainOne(z, target, x, PGetf("eta"));
         err += NarrayUtil.Dist2Squared(z, target);
         count++;
     }
     err /= count;
     Global.Debugf("info", "   {4} n {0} niters={1} eta={2:0.#####} errors={3:0.########}",
            ds.nSamples(), niters, PGetf("eta"), err, FullName);
 }
コード例 #4
0
ファイル: AutoMlpClassifier.cs プロジェクト: nickun/OCRonet
        public virtual void TrainBatch(IDataset ds, IDataset ts)
        {
            Stopwatch sw = Stopwatch.StartNew();
            bool parallel = PGetb("parallel");
            float eta_init = PGetf("eta_init"); // 0.5
            float eta_varlog = PGetf("eta_varlog"); // 1.5
            float hidden_varlog = PGetf("hidden_varlog"); // 1.2
            int hidden_lo = PGeti("hidden_lo");
            int hidden_hi = PGeti("hidden_hi");
            int rounds = PGeti("rounds");
            int mlp_noopt = PGeti("noopt");
            int hidden_min = PGeti("hidden_min");
            int hidden_max = PGeti("hidden_max");
            CHECK_ARG(hidden_min > 1 && hidden_max < 1000000, "hidden_min > 1 && hidden_max < 1000000");
            CHECK_ARG(hidden_hi >= hidden_lo, "hidden_hi >= hidden_lo");
            CHECK_ARG(hidden_max >= hidden_min, "hidden_max >= hidden_min");
            CHECK_ARG(hidden_lo >= hidden_min && hidden_hi <= hidden_max, "hidden_lo >= hidden_min && hidden_hi <= hidden_max");
            int nn = PGeti("nensemble");
            ObjList<MlpClassifier> nets = new ObjList<MlpClassifier>();
            nets.Resize(nn);
            for (int i = 0; i < nn; i++)
                nets[i] = new MlpClassifier(i);
            Floatarray errs = new Floatarray(nn);
            Floatarray etas = new Floatarray(nn);
            Intarray index = new Intarray();
            float best = 1e30f;
            if (PExists("%error"))
                best = PGetf("%error");
            int nclasses = ds.nClasses();

            /*Floatarray v = new Floatarray();
            for (int i = 0; i < ds.nSamples(); i++)
            {
                ds.Input1d(v, i);
                CHECK_ARG(NarrayUtil.Min(v) > -100 && NarrayUtil.Max(v) < 100, "min(v)>-100 && max(v)<100");
            }*/
            CHECK_ARG(ds.nSamples() >= 10 && ds.nSamples() < 100000000, "ds.nSamples() >= 10 && ds.nSamples() < 100000000");

            for (int i = 0; i < nn; i++)
            {
                // nets(i).init(data.dim(1),logspace(i,nn,hidden_lo,hidden_hi),nclasses);
                if (w1.Length() > 0)
                {
                    nets[i].Copy(this);
                    etas[i] = ClassifierUtil.rLogNormal(eta_init, eta_varlog);
                }
                else
                {
                    nets[i].InitData(ds, (int)(logspace(i, nn, hidden_lo, hidden_hi)), c2i, i2c);
                    etas[i] = PGetf("eta");
                }
            }
            etas[0] = PGetf("eta");     // zero position is identical to itself

            Global.Debugf("info", "mlp training n {0} nc {1}", ds.nSamples(), nclasses);
            for (int round = 0; round < rounds; round++)
            {
                Stopwatch swRound = Stopwatch.StartNew();
                errs.Fill(-1);
                if (parallel)
                {
                    // For each network i
                    Parallel.For(0, nn, i =>
                    {
                        nets[i].PSet("eta", etas[i]);
                        nets[i].TrainDense(ds);     // было XTrain
                        errs[i] = ClassifierUtil.estimate_errors(nets[i], ts);
                    });
                }
                else
                {
                    for (int i = 0; i < nn; i++)
                    {
                        nets[i].PSet("eta", etas[i]);
                        nets[i].TrainDense(ds);     // было XTrain
                        errs[i] = ClassifierUtil.estimate_errors(nets[i], ts);
                        //Global.Debugf("detail", "net({0}) {1} {2} {3}", i,
                        //       errs[i], nets[i].Complexity(), etas[i]);
                    }
                }
                NarrayUtil.Quicksort(index, errs);
                if (errs[index[0]] < best)
                {
                    best = errs[index[0]];
                    cv_error = best;
                    this.Copy(nets[index[0]]);
                    this.PSet("eta", etas[index[0]]);
                    Global.Debugf("info", "  best mlp[{0}] update errors={1} {2}", index[0], best, crossvalidate ? "cv" : "");
                }
                if (mlp_noopt == 0)
                {
                    for (int i = 0; i < nn / 2; i++)
                    {
                        int j = i + nn / 2;
                        nets[index[j]].Copy(nets[index[i]]);
                        int n = nets[index[j]].nHidden();
                        int nm = Math.Min(Math.Max(hidden_min, (int)(ClassifierUtil.rLogNormal(n, hidden_varlog))), hidden_max);
                        nets[index[j]].ChangeHidden(nm);
                        etas[index[j]] = ClassifierUtil.rLogNormal(etas[index[i]], eta_varlog);
                    }
                }
                Global.Debugf("info", " end mlp round {0} err {1} nHidden {2}", round, best, nHidden());
                swRound.Stop();
                int totalTest= ts.nSamples();
                int errCnt = Convert.ToInt32(best * totalTest);
                OnTrainRound(this, new TrainEventArgs(
                    round, best, totalTest - errCnt, totalTest, best, swRound.Elapsed, TimeSpan.Zero
                    ));
            }

            sw.Stop();
            Global.Debugf("info", String.Format("          training time: {0} minutes, {1} seconds",
                (int)sw.Elapsed.TotalMinutes, sw.Elapsed.Seconds));
            PSet("%error", best);
            int nsamples = ds.nSamples() * rounds;
            if (PExists("%nsamples"))
                nsamples += PGeti("%nsamples");
            PSet("%nsamples", nsamples);
        }