예제 #1
0
        public override void FindBestCuts()
        {
            unchecked
            {
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(cutcosts[i] + 10), 0xff0000);
                }
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(min_thresh + 10), 0x800000);
                }
            }
            Floatarray temp = new Floatarray();

            Gauss.Gauss1d(temp, cutcosts, 3.0f);
            cutcosts.Move(temp);
            SegmRoutine.local_minima(ref bestcuts, cutcosts, min_range, min_thresh);
            for (int i = 0; i < bestcuts.Length(); i++)
            {
                Narray <Point> cut = cuts[bestcuts[i]];
                for (int j = 0; j < cut.Length(); j++)
                {
                    Point p = cut[j];
                    NarrayUtil.ExtPut(dimage, p.X, p.Y, 0x00ff00);
                }
            }
            ///-if(debug.Length > 0) write_image_packed(debug, dimage);
            // dshow1d(cutcosts,"Y");
            //dshow(dimage,"Y");
        }
예제 #2
0
        /// <summary>
        /// Randomly sample an FST, assuming any input.
        /// </summary>
        /// <param name="result">The array of output symbols, excluding epsilons.</param>
        /// <param name="fst">The FST.</param>
        /// <param name="max">The maximum length of the result.</param>
        /// <returns>total cost</returns>
        public static double fst_sample(Intarray result, IGenericFst fst, int max = 1000)
        {
            double total_cost = 0;
            int    current    = fst.GetStart();

            for (int counter = 0; counter < max; counter++)
            {
                Intarray   inputs  = new Intarray();
                Intarray   outputs = new Intarray();
                Intarray   targets = new Intarray();
                Floatarray costs   = new Floatarray();

                fst.Arcs(inputs, targets, outputs, costs, current);

                // now we need to deal with the costs uniformly, so:
                costs.Push(fst.GetAcceptCost(current));
                int choice = sample_by_costs(costs);
                if (choice == costs.Length() - 1)
                {
                    break;
                }
                result.Push(outputs[choice]);
                total_cost += costs[choice];
                current     = targets[choice];
            }
            return(total_cost + fst.GetAcceptCost(current));
        }
예제 #3
0
        /// <summary>
        /// Copy one FST to another.
        /// </summary>
        /// <param name="dst">The destination. Will be cleared before copying.</param>
        /// <param name="src">The FST to copy.</param>
        public static void fst_copy(IGenericFst dst, IGenericFst src)
        {
            dst.Clear();
            int n = src.nStates();

            for (int i = 0; i < n; i++)
            {
                dst.NewState();
            }
            dst.SetStart(src.GetStart());
            for (int i = 0; i < n; i++)
            {
                dst.SetAccept(i, src.GetAcceptCost(i));
                Intarray   targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
                Floatarray costs = new Floatarray();
                src.Arcs(inputs, targets, outputs, costs, i);
                int inlen = inputs.Length();
                if (inlen != targets.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == targets.length()");
                }
                if (inlen != outputs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == outputs.length()");
                }
                if (inlen != costs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == costs.length()");
                }
                for (int j = 0; j < inputs.Length(); j++)
                {
                    dst.AddTransition(i, targets.At1d(j), outputs.At1d(j), costs.At1d(j), inputs.At1d(j));
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Pick an array element with probability proportional to exp(-cost).
        /// </summary>
        public static int sample_by_costs(Floatarray costs)
        {
            Doublearray p = new Doublearray();

            p.Copy(costs);
            double mincost = NarrayUtil.Min(costs);

            p -= mincost;

            for (int i = 0; i < p.Length(); i++)
            {
                p.UnsafePut1d(i, Math.Exp(-p.UnsafeAt1d(i)));
            }
            double sump = NarrayUtil.Sum(p);

            p /= sump;

            double choice = rnd.NextDouble();
            double s      = 0;

            for (int i = 0; i < p.Length(); i++)
            {
                s += p[i];
                if (choice < s)
                {
                    return(i);
                }
            }

            // shouldn't happen...
            return(costs.Length() - 1);
        }
예제 #5
0
        public static void local_min(ref Floatarray result, Floatarray data, int r)
        {
            int n = data.Length();

            result.Resize(n);
            for (int i = 0; i < n; i++)
            {
                float lmin = data[i];
                for (int j = -r; j <= r; j++)
                {
                    int k = i + j;
                    unchecked
                    {
                        if ((uint)(k) >= (uint)(n))
                        {
                            continue;
                        }
                    }
                    if (data[k] >= lmin)
                    {
                        continue;
                    }
                    lmin = data[k];
                }
                result[i] = lmin;
            }
        }
예제 #6
0
        public override float OutputsDense(Floatarray result, Floatarray x_raw)
        {
            CHECK_ARG(x_raw.Length() == w1.Dim(1), "x_raw.Length() == w1.Dim(1)");
            Floatarray z      = new Floatarray();
            int        sparse = PGeti("sparse");
            Floatarray y      = new Floatarray();
            Floatarray x      = new Floatarray();

            x.Copy(x_raw);
            mvmul0(y, w1, x);
            y += b1;
            for (int i = 0; i < y.Length(); i++)
            {
                y[i] = sigmoid(y[i]);
            }
            if (sparse > 0)
            {
                ClassifierUtil.Sparsify(y, sparse);
            }
            mvmul0(z, w2, y);
            z += b2;
            for (int i = 0; i < z.Length(); i++)
            {
                z[i] = sigmoid(z[i]);
            }
            result.Copy(z);
            //int idx = NarrayUtil.ArgMax(result);
            //float val = NarrayUtil.Max(result);
            return(Convert.ToSingle(Math.Abs(NarrayUtil.Sum(z) - 1.0)));
        }
예제 #7
0
        /// <summary>
        /// Copy one FST to another, preserving only lowest-cost arcs.
        /// This is useful for visualization.
        /// </summary>
        /// <param name="dst">The destination. Will be cleared before copying.</param>
        /// <param name="src">The FST to copy.</param>
        public static void fst_copy_best_arcs_only(IGenericFst dst, IGenericFst src)
        {
            dst.Clear();
            int n = src.nStates();

            for (int i = 0; i < n; i++)
            {
                dst.NewState();
            }
            dst.SetStart(src.GetStart());
            for (int i = 0; i < n; i++)
            {
                dst.SetAccept(i, src.GetAcceptCost(i));
                Intarray   targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
                Floatarray costs = new Floatarray();
                src.Arcs(inputs, targets, outputs, costs, i);
                int inlen = inputs.Length();
                if (inlen != targets.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == targets.length()");
                }
                if (inlen != outputs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == outputs.length()");
                }
                if (inlen != costs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == costs.length()");
                }
                Dictionary <int, int> hash = new Dictionary <int, int>();
                for (int j = 0; j < n; j++)
                {
                    int t           = targets[j];
                    int best_so_far = -1;
                    if (hash.ContainsKey(t))
                    {
                        best_so_far = hash[t];
                    }
                    if (best_so_far == -1 || costs[j] < costs[best_so_far])
                    {
                        hash[t] = j;
                    }
                }
                Intarray keys = new Intarray();
                //hash.keys(keys);
                keys.Clear();
                foreach (int key in hash.Keys)
                {
                    keys.Push(key);
                }

                for (int k = 0; k < keys.Length(); k++)
                {
                    int j = hash[keys[k]];
                    dst.AddTransition(i, targets[j], outputs[j], costs[j], inputs[j]);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// This looks at the transition from state pair
        /// (f1,f2) -> (t1,t2), withthe given cost.
        /// </summary>
        public void Relax(int f1, int f2,   // input state pair
                          int t1, int t2,   // output state pair
                          float cost,       // transition cost
                          int arc_id1,      // (unused)
                          int arc_id2,      // (unused)
                          int input,        // input label
                          int intermediate, // (unused)
                          int output,       // output label
                          float base_cost,  // cost of the path so far
                          int trail_index)
        {
            //logger.format("relaxing %d %d -> %d %d (bcost %f, cost %f)", f1, f2, t1, t2, base_cost, cost);

            if (!nbest.AddReplacingId(t1 * fst2.nStates() + t2,
                                      all_costs.Length(),
                                      -base_cost - cost))
            {
                return;
            }

            //logger.format("nbest changed");
            //nbest.log(logger);

            if (input > 0)
            {
                // The candidate for the next beam is stored in all_XX arrays.
                // (can we store it in the stree instead?)
                all_inputs.Push(input);
                all_targets1.Push(t1);
                all_targets2.Push(t2);
                all_outputs.Push(output);
                all_costs.Push(cost);
                parent_trails.Push(trail_index);
            }
            else
            {
                // Beam control hack
                // -----------------
                // if a node is important (changes nbest) AND its input is 0,
                // then it's added to the CURRENT beam.

                //logger.format("pushing control point from trail %d to %d, %d",
                //trail_index, t1, t2);
                int new_node = stree.Add(beam[trail_index], t1, t2, input, output, (float)cost);
                beam.Push(new_node);
                beamcost.Push(base_cost + cost);

                // This is a stub entry indicating that the node should not
                // be added to the next generation beam.
                all_inputs.Push(0);
                all_targets1.Push(-1);
                all_targets2.Push(-1);
                all_outputs.Push(0);
                all_costs.Push(0);
                parent_trails.Push(-1);
            }
        }
예제 #9
0
 protected override float Outputs(OutputVector result, Floatarray v)
 {
     Floatarray outar = new Floatarray();
     float cost = OutputsDense(outar, v);
     result.Clear();
     for (int i = 0; i < outar.Length(); i++)
         result[i2c[i]] = outar[i];
     return cost;
 }
예제 #10
0
        public virtual void Extract(Floatarray outa, Floatarray ina)
        {
            outa.Clear();
            Narray <Floatarray> items = new Narray <Floatarray>();

            Extract(items, ina);
            //int num = 0;
            for (int i = 0; i < items.Length(); i++)
            {
                Floatarray a = items[i];
                outa.ReserveTo(outa.Length() + a.Length());    // optimization
                for (int j = 0; j < a.Length(); j++)
                {
                    outa.Push(a.At1d(j));
                    //outa[num++] = a.At1d(j);
                }
            }
        }
예제 #11
0
        public float Sum()
        {
            float total = 0.0f;

            for (int i = 0; i < _values.Length(); i++)
            {
                total += _values.UnsafeAt1d(i);
            }
            return(total);
        }
예제 #12
0
 public override void PutCosts(Floatarray costs, int page, int line, string variant = null)
 {
     using (var fs = Open(FileMode.Create, page, line, variant, "costs"))
         using (var writer = new StreamWriter(fs))
         {
             for (int i = 0; i < costs.Length(); i++)
             {
                 writer.WriteLine("{0} {1}", i, costs[i]);
             }
         }
 }
예제 #13
0
        protected override float Outputs(OutputVector result, Floatarray v)
        {
            Floatarray outar = new Floatarray();
            float      cost  = OutputsDense(outar, v);

            result.Clear();
            for (int i = 0; i < outar.Length(); i++)
            {
                result[i2c[i]] = outar[i];
            }
            return(cost);
        }
예제 #14
0
        public void Copy(Floatarray v, float eps = 1e-11f)
        {
            Clear();
            int n = v.Length();

            for (int i = 0; i < n; i++)
            {
                float value = v.At1d(i);
                if (Math.Abs(value) >= eps)
                {
                    _keys.Push(i);
                    _values.Push(value);
                }
            }
            _len = v.Length();
            _keys.Resize(_len);
            for (int i = 0; i < _len; i++)
            {
                _keys.Put1d(i, i);
            }
            _values.Copy(v);
        }
예제 #15
0
        /// <summary>
        /// do a single stochastic gradient descent step
        /// </summary>
        public void TrainOne(Floatarray z, Floatarray target, Floatarray x, float eta)
        {
            CHECK_ARG(target.Length() == w2.Dim(0), "target.Length() == w2.Dim(0)");
            CHECK_ARG(x.Length() == w1.Dim(1), "x.Length() == w1.Dim(1)");

            int        sparse  = PGeti("sparse");
            int        nhidden = this.nHidden();
            int        noutput = nClasses();
            Floatarray delta1  = new Floatarray(nhidden);
            Floatarray delta2  = new Floatarray(noutput);
            Floatarray y       = new Floatarray(nhidden);

            mvmul0(y, w1, x);
            y += b1;
            for (int i = 0; i < nhidden; i++)
            {
                y[i] = sigmoid(y[i]);
            }
            if (sparse > 0)
            {
                ClassifierUtil.Sparsify(y, sparse);
            }
            mvmul0(z, w2, y);
            z += b2;
            for (int i = 0; i < noutput; i++)
            {
                z[i] = sigmoid(z[i]);
            }

            for (int i = 0; i < noutput; i++)
            {
                delta2[i] = (z[i] - target[i]) * dsigmoidy(z[i]);
            }
            vmmul0(delta1, delta2, w2);
            for (int i = 0; i < nhidden; i++)
            {
                delta1[i] = delta1[i] * dsigmoidy(y[i]);
            }

            outer_add(w2, delta2, y, -eta);
            for (int i = 0; i < noutput; i++)
            {
                b2[i] -= eta * delta2[i];
            }
            outer_add(w1, delta1, x, -eta);
            for (int i = 0; i < nhidden; i++)
            {
                b1[i] -= eta * delta1[i];
            }
        }
예제 #16
0
        public static double Perplexity(Floatarray weights)
        {
            Floatarray w = new Floatarray();

            w.Copy(weights);
            w /= NarrayUtil.Sum(w);
            double total = 0.0;

            for (int i = 0; i < w.Length(); i++)
            {
                float value = w[i];
                total += value * Math.Log(value);
            }
            return(Math.Exp(-total));
        }
예제 #17
0
        public void FindBestCuts()
        {
            /*Intarray segm = new Intarray();
             * segm.Copy(dimage);
             * ImgLabels.simple_recolor(segm);
             * ImgIo.write_image_packed("debug1.png", segm);*/
            unchecked
            {
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(cutcosts[i] + 10), 0xff0000);
                }
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(min_thresh + 10), 0x800000);
                }
            }
            Floatarray temp = new Floatarray();

            Gauss.Gauss1d(temp, cutcosts, cost_smooth);
            cutcosts.Move(temp);
            SegmRoutine.local_minima(ref bestcuts, cutcosts, min_range, min_thresh);
            for (int i = 0; i < bestcuts.Length(); i++)
            {
                Narray <Point> cut = cuts[bestcuts[i]];
                for (int j = 0; j < cut.Length(); j++)
                {
                    Point p = cut[j];
                    NarrayUtil.ExtPut(dimage, p.X, p.Y, 0x00ff00);
                }
            }

            /*segm.Copy(dimage);
             * ImgLabels.simple_recolor(segm);
             * ImgIo.write_image_packed("debug2.png", segm);*/
        }
예제 #18
0
 public override void Add(Floatarray v, int c)
 {
     CHECK_ARG(NarrayUtil.Min(v) > -1.2f && NarrayUtil.Max(v) < 1.2f, "float8: value out of range (-1.2..1.2)");
     CHECK_ARG(c >= -1, "c>=-1");
     if (c >= nc)
     {
         nc = c + 1;
     }
     if (nf < 0)
     {
         nf = v.Length();
     }
     RowPush(data, v);
     classes.Push(c);
 }
예제 #19
0
        public static double Entropy(Floatarray a)
        {
            double z     = NarrayUtil.Sum(a);
            double total = 0.0;

            for (int i = 0; i < a.Length(); i++)
            {
                double p = a[i] / z;
                if (p < 1e-8)
                {
                    continue;
                }
                total += p * Math.Log(p);
            }
            return(-total);
        }
예제 #20
0
 public virtual void Extract(Floatarray outa, Floatarray ina)
 {
     outa.Clear();
     Narray<Floatarray> items = new Narray<Floatarray>();
     Extract(items, ina);
     //int num = 0;
     for (int i = 0; i < items.Length(); i++)
     {
         Floatarray a = items[i];
         outa.ReserveTo(outa.Length() + a.Length());    // optimization
         for (int j = 0; j < a.Length(); j++)
         {
             outa.Push(a.At1d(j));
             //outa[num++] = a.At1d(j);
         }
     }
 }
예제 #21
0
        public static void local_minima(ref Intarray result, Floatarray data, int r, float threshold)
        {
            int n = data.Length();

            result.Clear();
            Floatarray lmin = new Floatarray();

            local_min(ref lmin, data, r);
            for (int i = 1; i < n - 1; i++)
            {
                if (data[i] <= threshold && data[i] <= lmin[i] &&
                    data[i] <= data[i - 1] && data[i] < data[i + 1])
                {
                    result.Push(i);
                }
            }
        }
예제 #22
0
파일: Gauss.cs 프로젝트: liaoheping/OCRonet
        public static void Gauss1d(Floatarray outa, Floatarray ina, float sigma)
        {
            outa.Resize(ina.Dim(0));
            // make a normalized mask
            int        range = 1 + (int)(3.0 * sigma);
            Floatarray mask  = new Floatarray(2 * range + 1);

            for (int i = 0; i <= range; i++)
            {
                float y = (float)Math.Exp(-i * i / 2.0 / sigma / sigma);
                mask[range + i] = mask[range - i] = y;
            }
            float total = 0.0f;

            for (int i = 0; i < mask.Dim(0); i++)
            {
                total += mask[i];
            }
            for (int i = 0; i < mask.Dim(0); i++)
            {
                mask[i] /= total;
            }

            // apply it
            int n = ina.Length();

            for (int i = 0; i < n; i++)
            {
                total = 0.0f;
                for (int j = 0; j < mask.Dim(0); j++)
                {
                    int index = i + j - range;
                    if (index < 0)
                    {
                        index = 0;
                    }
                    if (index >= n)
                    {
                        index = n - 1;
                    }
                    total += ina[index] * mask[j]; // it's symmetric
                }
                outa[i] = total;
            }
        }
예제 #23
0
        public static void weighted_sample(Intarray samples, Floatarray weights, int n)
        {
            Floatarray cs = new Floatarray();

            cs.Copy(weights);
            for (int i = 1; i < cs.Length(); i++)
            {
                cs[i] += cs[i - 1];
            }
            cs /= NarrayUtil.Max(cs);
            samples.Clear();
            for (int i = 0; i < n; i++)
            {
                float value = (float)DRandomizer.Default.drand();
                int where = Binsearch(cs, value);
                samples.Push(where);
            }
        }
예제 #24
0
        public override void Info()
        {
            bool bak = Logger.Default.verbose;

            Logger.Default.verbose = true;
            Logger.Default.WriteLine("MLP");
            PPrint();
            Logger.Default.WriteLine(String.Format("nInput {0} nHidden {1} nOutput {2}",
                                                   w1.Dim(1), w1.Dim(0), w2.Dim(0)));
            if (w1.Length() > 0 && w2.Length() > 0)
            {
                Logger.Default.WriteLine(String.Format("w1 [{0},{1}] b1 [{2},{3}]",
                                                       NarrayUtil.Min(w1), NarrayUtil.Max(w1), NarrayUtil.Min(b1), NarrayUtil.Max(b1)));
                Logger.Default.WriteLine(String.Format("w2 [{0},{1}] b2 [{2},{3}]",
                                                       NarrayUtil.Min(w2), NarrayUtil.Max(w2), NarrayUtil.Min(b2), NarrayUtil.Max(b2)));
            }
            Logger.Default.verbose = bak;
        }
예제 #25
0
파일: FstUtil.cs 프로젝트: nickun/OCRonet
        /// <summary>
        /// Copy one FST to another, preserving only lowest-cost arcs.
        /// This is useful for visualization.
        /// </summary>
        /// <param name="dst">The destination. Will be cleared before copying.</param>
        /// <param name="src">The FST to copy.</param>
        public static void fst_copy_best_arcs_only(IGenericFst dst, IGenericFst src)
        {
            dst.Clear();
            int n = src.nStates();
            for (int i = 0; i < n; i++)
                dst.NewState();
            dst.SetStart(src.GetStart());
            for(int i = 0; i < n; i++)
            {
                dst.SetAccept(i, src.GetAcceptCost(i));
                Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
                Floatarray costs = new Floatarray();
                src.Arcs(inputs, targets, outputs, costs, i);
                int inlen = inputs.Length();
                if (inlen != targets.Length())
                    throw new Exception("ASSERT: inputs.length() == targets.length()");
                if (inlen != outputs.Length())
                    throw new Exception("ASSERT: inputs.length() == outputs.length()");
                if (inlen != costs.Length())
                    throw new Exception("ASSERT: inputs.length() == costs.length()");
                Dictionary< int, int > hash = new Dictionary<int,int>();
                for(int j = 0; j < n; j++) {
                    int t = targets[j];
                    int best_so_far = -1;
                    if (hash.ContainsKey(t))
                        best_so_far = hash[t];
                    if(best_so_far == -1 || costs[j] < costs[best_so_far])
                        hash[t] = j;
                }
                Intarray keys = new Intarray();
                //hash.keys(keys);
                keys.Clear();
                foreach (int key in hash.Keys)
                {
                    keys.Push(key);
                }

                for(int k = 0; k < keys.Length(); k++) {
                    int j = hash[keys[k]];
                    dst.AddTransition(i, targets[j], outputs[j], costs[j], inputs[j]);
                }
            }
        }
예제 #26
0
        public override void Add(Floatarray v, int c)
        {
            CHECK_ARG(NarrayUtil.Min(v) >= 0.0f && NarrayUtil.Max(v) <= 1.0f, "float8: value out of range (0..1)");
            CHECK_ARG(c >= -1, "c>=-1");
            if (c >= nc)
            {
                nc = c + 1;
            }
            if (nf < 0)
            {
                nf = v.Length();
            }
            Narray <byte> newDataItem = data.Push(new Narray <byte>());

            Copy(newDataItem, v);
            classes.Push(c);
            CHECK_ARG(nc > 0, "nc>0");
            CHECK_ARG(nf > 0, "nf>0");
        }
예제 #27
0
        public static void erase_small_components(Floatarray input, float mins = 0.2f, float thresh = 0.25f)
        {
            // compute a thresholded image for component labeling
            float    threshold  = thresh * NarrayUtil.Max(input);
            Intarray components = new Intarray();

            components.MakeLike(input);
            components.Fill(0);
            for (int i = 0; i < components.Length(); i++)
            {
                components[i] = (input[i] > threshold ? 1 : 0);
            }

            // compute the number of pixels in each component
            int      n      = ImgLabels.label_components(ref components);
            Intarray totals = new Intarray(n + 1);

            totals.Fill(0);
            for (int i = 0; i < components.Length(); i++)
            {
                totals[components[i]] = totals[components[i]] + 1;
            }
            totals[0] = 0;
            int biggest = NarrayUtil.ArgMax(totals);

            // erase small components
            float     minsize    = mins * totals[biggest];
            Bytearray keep       = new Bytearray(n + 1);
            float     background = NarrayUtil.Min(input);

            for (int i = 0; i < keep.Length(); i++)
            {
                keep[i] = (byte)(totals[i] > minsize ? 1 : 0);
            }
            for (int i = 0; i < input.Length(); i++)
            {
                if (keep[components[i]] == 0)
                {
                    input[i] = background;
                }
            }
        }
예제 #28
0
        protected static void vmmul0(Floatarray result, Floatarray v, Floatarray a)
        {
            int n = a.Dim(0);
            int m = a.Dim(1);

            CHECK_ARG(n == v.Length(), "n == v.Length()");
            result.Resize(m);
            result.Fill(0f);
            for (int i = 0; i < n; i++)
            {
                float value = v.UnsafeAt(i);//v[i];
                if (value == 0f)
                {
                    continue;
                }
                for (int j = 0; j < m; j++)
                {
                    result.UnsafePut(j, result.UnsafeAt(j) + (a.UnsafeAt(i, j) * value));
                }
            }
        }
예제 #29
0
        /// <summary>
        /// Reverse the FST's arcs, adding a new start vertex (former accept).
        /// </summary>
        public static void fst_copy_reverse(IGenericFst dst, IGenericFst src, bool no_accept = false)
        {
            dst.Clear();
            int n = src.nStates();

            for (int i = 0; i <= n; i++)
            {
                dst.NewState();
            }
            if (!no_accept)
            {
                dst.SetAccept(src.GetStart());
            }
            dst.SetStart(n);
            for (int i = 0; i < n; i++)
            {
                dst.AddTransition(n, i, 0, src.GetAcceptCost(i), 0);
                Intarray   targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
                Floatarray costs = new Floatarray();
                src.Arcs(inputs, targets, outputs, costs, i);
                if (inputs.Length() != targets.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == targets.length()");
                }
                if (inputs.Length() != outputs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == outputs.length()");
                }
                if (inputs.Length() != costs.Length())
                {
                    throw new Exception("ASSERT: inputs.length() == costs.length()");
                }
                for (int j = 0; j < inputs.Length(); j++)
                {
                    dst.AddTransition(targets.At1d(j), i, outputs.At1d(j), costs.At1d(j), inputs.At1d(j));
                }
            }
        }
예제 #30
0
파일: FstUtil.cs 프로젝트: nickun/OCRonet
 /// <summary>
 /// Copy one FST to another.
 /// </summary>
 /// <param name="dst">The destination. Will be cleared before copying.</param>
 /// <param name="src">The FST to copy.</param>
 public static void fst_copy(IGenericFst dst, IGenericFst src)
 {
     dst.Clear();
     int n = src.nStates();
     for (int i = 0; i < n; i++)
         dst.NewState();
     dst.SetStart(src.GetStart());
     for (int i = 0; i < n; i++)
     {
         dst.SetAccept(i, src.GetAcceptCost(i));
         Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
         Floatarray costs = new Floatarray();
         src.Arcs(inputs, targets, outputs, costs, i);
         int inlen = inputs.Length();
         if (inlen != targets.Length())
             throw new Exception("ASSERT: inputs.length() == targets.length()");
         if (inlen != outputs.Length())
             throw new Exception("ASSERT: inputs.length() == outputs.length()");
         if (inlen != costs.Length())
             throw new Exception("ASSERT: inputs.length() == costs.length()");
         for (int j = 0; j < inputs.Length(); j++)
             dst.AddTransition(i, targets.At1d(j), outputs.At1d(j), costs.At1d(j), inputs.At1d(j));
     }
 }
예제 #31
0
        protected static void outer_add(Floatarray a, Floatarray u, Floatarray v, float eps)
        {
            int n = a.Dim(0);
            int m = a.Dim(1);

            CHECK_ARG(n == u.Length(), "n == u.Length()");
            CHECK_ARG(m == v.Length(), "m == v.Length()");
            if (count_zeros(u) >= count_zeros(v))
            {
                for (int i = 0; i < n; i++)
                {
                    if (u.UnsafeAt(i) == 0)
                    {
                        continue;
                    }
                    for (int j = 0; j < m; j++)
                    {
                        a.UnsafePut(i, j, a.UnsafeAt(i, j) + (eps * u.UnsafeAt(i) * v.UnsafeAt(j)));
                    }
                }
            }
            else
            {
                for (int j = 0; j < m; j++)
                {
                    if (v.UnsafeAt(j) == 0)
                    {
                        continue;
                    }
                    for (int i = 0; i < n; i++)
                    {
                        a.UnsafePut(i, j, a.UnsafeAt(i, j) + (eps * u.UnsafeAt(i) * v.UnsafeAt(j)));
                    }
                }
            }
        }
예제 #32
0
        /// <summary>
        /// do a single stochastic gradient descent step
        /// </summary>
        public void TrainOne(Floatarray z, Floatarray target, Floatarray x, float eta)
        {
            CHECK_ARG(target.Length() == w2.Dim(0), "target.Length() == w2.Dim(0)");
            CHECK_ARG(x.Length() == w1.Dim(1), "x.Length() == w1.Dim(1)");

            int sparse = PGeti("sparse");
            int nhidden = this.nHidden();
            int noutput = nClasses();
            Floatarray delta1 = new Floatarray(nhidden);
            Floatarray delta2 = new Floatarray(noutput);
            Floatarray y = new Floatarray(nhidden);

            mvmul0(y, w1, x);
            y += b1;
            for (int i = 0; i < nhidden; i++)
                y[i] = sigmoid(y[i]);
            if (sparse > 0) ClassifierUtil.Sparsify(y, sparse);
            mvmul0(z, w2, y);
            z += b2;
            for (int i = 0; i < noutput; i++)
                z[i] = sigmoid(z[i]);

            for (int i = 0; i < noutput; i++)
                delta2[i] = (z[i] - target[i]) * dsigmoidy(z[i]);
            vmmul0(delta1, delta2, w2);
            for (int i = 0; i < nhidden; i++)
                delta1[i] = delta1[i] * dsigmoidy(y[i]);

            outer_add(w2, delta2, y, -eta);
            for (int i = 0; i < noutput; i++)
                b2[i] -= eta * delta2[i];
            outer_add(w1, delta1, x, -eta);
            for (int i = 0; i < nhidden; i++)
                b1[i] -= eta * delta1[i];
        }
예제 #33
0
 public override void PutCosts(Floatarray costs, int page, int line, string variant = null)
 {
     using(var fs = Open(FileMode.Create, page, line, variant, "costs"))
     using (var writer = new StreamWriter(fs))
     {
         for (int i = 0; i < costs.Length(); i++)
         {
             writer.WriteLine("{0} {1}", i, costs[i]);
         }
     }
 }
예제 #34
0
 public void Copy(Floatarray v, float eps = 1e-11f)
 {
     Clear();
     int n = v.Length();
     for (int i = 0; i < n; i++)
     {
         float value = v.At1d(i);
         if (Math.Abs(value) >= eps)
         {
             _keys.Push(i);
             _values.Push(value);
         }
     }
     _len = v.Length();
     _keys.Resize(_len);
     for (int i = 0; i < _len; i++)
         _keys.Put1d(i, i);
     _values.Copy(v);
 }
예제 #35
0
파일: FstUtil.cs 프로젝트: nickun/OCRonet
 /// <summary>
 /// Reverse the FST's arcs, adding a new start vertex (former accept).
 /// </summary>
 public static void fst_copy_reverse(IGenericFst dst, IGenericFst src, bool no_accept = false)
 {
     dst.Clear();
     int n = src.nStates();
     for (int i = 0; i <= n; i++)
         dst.NewState();
     if (!no_accept)
         dst.SetAccept(src.GetStart());
     dst.SetStart(n);
     for (int i = 0; i < n; i++)
     {
         dst.AddTransition(n, i, 0, src.GetAcceptCost(i), 0);
         Intarray targets = new Intarray(), outputs = new Intarray(), inputs = new Intarray();
         Floatarray costs = new Floatarray();
         src.Arcs(inputs, targets, outputs, costs, i);
         if (inputs.Length() != targets.Length())
             throw new Exception("ASSERT: inputs.length() == targets.length()");
         if (inputs.Length() != outputs.Length())
             throw new Exception("ASSERT: inputs.length() == outputs.length()");
         if (inputs.Length() != costs.Length())
             throw new Exception("ASSERT: inputs.length() == costs.length()");
         for (int j = 0; j < inputs.Length(); j++)
             dst.AddTransition(targets.At1d(j), i, outputs.At1d(j), costs.At1d(j), inputs.At1d(j));
     }
 }
예제 #36
0
파일: IDataset.cs 프로젝트: nickun/OCRonet
 public void Input1d(Floatarray v, int i)
 {
     Input(v, i);
     v.Reshape(v.Length());
 }
예제 #37
0
파일: FstUtil.cs 프로젝트: nickun/OCRonet
        /// <summary>
        /// Randomly sample an FST, assuming any input.
        /// </summary>
        /// <param name="result">The array of output symbols, excluding epsilons.</param>
        /// <param name="fst">The FST.</param>
        /// <param name="max">The maximum length of the result.</param>
        /// <returns>total cost</returns>
        public static double fst_sample(Intarray result, IGenericFst fst, int max=1000)
        {
            double total_cost = 0;
            int current = fst.GetStart();

            for (int counter = 0; counter < max; counter++)
            {
                Intarray inputs  = new Intarray();
                Intarray outputs = new Intarray();
                Intarray targets = new Intarray();
                Floatarray costs = new Floatarray();

                fst.Arcs(inputs, targets, outputs, costs, current);

                // now we need to deal with the costs uniformly, so:
                costs.Push(fst.GetAcceptCost(current));
                int choice = sample_by_costs(costs);
                if (choice == costs.Length() - 1)
                    break;
                result.Push(outputs[choice]);
                total_cost += costs[choice];
                current = targets[choice];
            }
            return total_cost + fst.GetAcceptCost(current);
        }
예제 #38
0
 public override void Add(Floatarray v, int c)
 {
     CHECK_ARG(NarrayUtil.Min(v) >= 0.0f && NarrayUtil.Max(v) <= 1.0f, "float8: value out of range (0..1)");
     CHECK_ARG(c >= -1, "c>=-1");
     if (c >= nc) nc = c + 1;
     if (nf < 0) nf = v.Length();
     Narray<byte> newDataItem = data.Push(new Narray<byte>());
     Copy(newDataItem, v);
     classes.Push(c);
     CHECK_ARG(nc > 0, "nc>0");
     CHECK_ARG(nf > 0, "nf>0");
 }
예제 #39
0
파일: FstUtil.cs 프로젝트: nickun/OCRonet
        /// <summary>
        /// Pick an array element with probability proportional to exp(-cost).
        /// </summary>
        public static int sample_by_costs(Floatarray costs)
        {
            Doublearray p = new Doublearray();
            p.Copy(costs);
            double mincost = NarrayUtil.Min(costs);
            p -= mincost;

            for (int i = 0; i < p.Length(); i++)
                p.UnsafePut1d(i, Math.Exp(-p.UnsafeAt1d(i)));
            double sump = NarrayUtil.Sum(p);
            p /= sump;

            double choice = rnd.NextDouble();
            double s = 0;
            for (int i = 0; i < p.Length(); i++)
            {
                s += p[i];
                if (choice < s)
                    return i;
            }

            // shouldn't happen...
            return costs.Length() - 1;
        }
예제 #40
0
 public override float OutputsDense(Floatarray result, Floatarray x_raw)
 {
     CHECK_ARG(x_raw.Length() == w1.Dim(1), "x_raw.Length() == w1.Dim(1)");
     Floatarray z = new Floatarray();
     int sparse = PGeti("sparse");
     Floatarray y = new Floatarray();
     Floatarray x = new Floatarray();
     x.Copy(x_raw);
     mvmul0(y, w1, x);
     y += b1;
     for (int i = 0; i < y.Length(); i++)
         y[i] = sigmoid(y[i]);
     if (sparse > 0)
         ClassifierUtil.Sparsify(y, sparse);
     mvmul0(z, w2, y);
     z += b2;
     for (int i = 0; i < z.Length(); i++)
         z[i] = sigmoid(z[i]);
     result.Copy(z);
     //int idx = NarrayUtil.ArgMax(result);
     //float val = NarrayUtil.Max(result);
     return Convert.ToSingle(Math.Abs(NarrayUtil.Sum(z) - 1.0));
 }
예제 #41
0
 protected void normalize(Floatarray v)
 {
     float kind = PGetf("normalization");
     if (kind < 0) return;
     if (kind == 1)
     {
         double total = 0.0;
         for (int i = 0; i < v.Length(); i++) total += Math.Abs(v[i]);
         for (int i = 0; i < v.Length(); i++) v[i] = (float)(v[i] / total);
     }
     else if (kind == 2)
     {
         double total = 0.0;
         for (int i = 0; i < v.Length(); i++) total += Math.Sqrt(v[i]);
         total = Math.Sqrt(total);
         for (int i = 0; i < v.Length(); i++) v[i] = (float)(v[i] / total);
     }
     else if (kind < 999)
     {
         double total = 0.0;
         for (int i = 0; i < v.Length(); i++) total += Math.Pow(v[i], kind);
         total = Math.Pow(total, 1.0 / kind);
         for (int i = 0; i < v.Length(); i++) v[i] = (float)(v[i] / total);
     }
     else if (kind == 999)
     {
         double total = 0.0;
         for (int i = 0; i < v.Length(); i++) if (Math.Abs(v[i]) > total) total = Math.Abs(v[i]);
         for (int i = 0; i < v.Length(); i++) v[i] = (float)(v[i] / total);
     }
     else
     {
         throw new Exception("bad normalization in mlp");
     }
 }
예제 #42
0
 public void Input1d(Floatarray v, int i)
 {
     Input(v, i);
     v.Reshape(v.Length());
 }
예제 #43
0
 // reading
 public override int nStates()
 {
     return(accept_costs.Length());
 }
예제 #44
0
        protected void normalize(Floatarray v)
        {
            float kind = PGetf("normalization");

            if (kind < 0)
            {
                return;
            }
            if (kind == 1)
            {
                double total = 0.0;
                for (int i = 0; i < v.Length(); i++)
                {
                    total += Math.Abs(v[i]);
                }
                for (int i = 0; i < v.Length(); i++)
                {
                    v[i] = (float)(v[i] / total);
                }
            }
            else if (kind == 2)
            {
                double total = 0.0;
                for (int i = 0; i < v.Length(); i++)
                {
                    total += Math.Sqrt(v[i]);
                }
                total = Math.Sqrt(total);
                for (int i = 0; i < v.Length(); i++)
                {
                    v[i] = (float)(v[i] / total);
                }
            }
            else if (kind < 999)
            {
                double total = 0.0;
                for (int i = 0; i < v.Length(); i++)
                {
                    total += Math.Pow(v[i], kind);
                }
                total = Math.Pow(total, 1.0 / kind);
                for (int i = 0; i < v.Length(); i++)
                {
                    v[i] = (float)(v[i] / total);
                }
            }
            else if (kind == 999)
            {
                double total = 0.0;
                for (int i = 0; i < v.Length(); i++)
                {
                    if (Math.Abs(v[i]) > total)
                    {
                        total = Math.Abs(v[i]);
                    }
                }
                for (int i = 0; i < v.Length(); i++)
                {
                    v[i] = (float)(v[i] / total);
                }
            }
            else
            {
                throw new Exception("bad normalization in mlp");
            }
        }
예제 #45
0
 protected static void outer_add(Floatarray a, Floatarray u, Floatarray v, float eps)
 {
     int n = a.Dim(0);
     int m = a.Dim(1);
     CHECK_ARG(n == u.Length(), "n == u.Length()");
     CHECK_ARG(m == v.Length(), "m == v.Length()");
     if (count_zeros(u) >= count_zeros(v))
     {
         for (int i = 0; i < n; i++)
         {
             if (u.UnsafeAt(i) == 0) continue;
             for (int j = 0; j < m; j++)
             {
                 a.UnsafePut(i, j, a.UnsafeAt(i, j) + (eps * u.UnsafeAt(i) * v.UnsafeAt(j)));
             }
         }
     }
     else
     {
         for (int j = 0; j < m; j++)
         {
             if (v.UnsafeAt(j) == 0) continue;
             for (int i = 0; i < n; i++)
             {
                 a.UnsafePut(i, j, a.UnsafeAt(i, j) + (eps * u.UnsafeAt(i) * v.UnsafeAt(j)));
             }
         }
     }
 }
예제 #46
0
 protected static void vmmul0(Floatarray result, Floatarray v, Floatarray a)
 {
     int n = a.Dim(0);
     int m = a.Dim(1);
     CHECK_ARG(n == v.Length(), "n == v.Length()");
     result.Resize(m);
     result.Fill(0f);
     for (int i = 0; i < n; i++)
     {
         float value = v.UnsafeAt(i);//v[i];
         if (value == 0f)
             continue;
         for (int j = 0; j < m; j++)
             result.UnsafePut(j, result.UnsafeAt(j) + (a.UnsafeAt(i, j) * value));
     }
 }
예제 #47
0
파일: Dataset8.cs 프로젝트: nickun/OCRonet
 public override void Add(Floatarray v, int c)
 {
     CHECK_ARG(NarrayUtil.Min(v) > -1.2f && NarrayUtil.Max(v) < 1.2f, "float8: value out of range (-1.2..1.2)");
     CHECK_ARG(c >= -1, "c>=-1");
     if (c >= nc) nc = c + 1;
     if (nf < 0) nf = v.Length();
     RowPush(data, v);
     classes.Push(c);
 }