Пример #1
0
        public static MnistModel ReadImage(string path, bool normalize, bool trainAsEncoder)
        {
            var bitmap = new Bitmap(path);

            var fileName = path.Substring(path.LastIndexOfAny(FileSeparators) + 1);
            var label    = int.Parse(fileName[0].ToString());

            var values = new DenseVector(bitmap.Height * bitmap.Width);

            for (var i = 0; i < bitmap.Height; i++)
            {
                for (var j = 0; j < bitmap.Width; j++)
                {
                    {
                        var color = bitmap.GetPixel(j, i);

                        var gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);

                        values[j + i * bitmap.Width] = 1 - gray / 255.0;
                    }
                }
            }

            if (normalize)
            {
                var max = values.Maximum();
                max /= 2;
                values.MapInplace(v => v - max);
            }

            //values.CoerceZero(0.005);
            //values.MapInplace(v => v > 0.995 ? 1 : v);

            Vector solution;

            if (trainAsEncoder)
            {
                solution = values;
            }
            else
            {
                solution = new DenseVector(10)
                {
                    [label] = 1.0
                };
            }

            var image = new MnistModel
            {
                Width            = bitmap.Width,
                Height           = bitmap.Height,
                Values           = values,
                FileName         = fileName,
                Label            = label,
                ExpectedSolution = solution
            };

            return(image);
        }
Пример #2
0
        public static DenseMatrix ToClassMatrix(this DenseVector x, int?length = null)
        {
            int len = length.HasValue ? length.Value : (int)x.Maximum() + 1;
            var res = DenseMatrix.Create(x.Count, len, (_, __) => 0);

            for (int i = 0; i < x.Count; i++)
            {
                res[i, (int)x[i]] = 1.0;
            }
            return(res);
        }
Пример #3
0
        public SelectorResult Compute(float Percent, int maxrank)
        {
            if (Percent > 100 || Percent < 0)
            {
                throw new ArgumentException("");
            }

            int countA      = (int)(Percent * this._x.ColumnCount / 100);
            var GroupAIndex = new List <int>();
            var GroupBIndex = new List <int>();



            if (this._x == null)
            {
                return(null);
            }



            var resid = this._x * this._x.Transpose();
            var j     = this._x.ColumnCount;
            var p2    = Math.Round(j * Percent / 100);
            int k     = 0;

            this._x = (Matrix)this._x.Transpose();
            var i    = maxrank;
            var p    = new DenseMatrix(i, this._x.ColumnCount);
            var told = new DenseMatrix(j, i);
            var t    = new DenseMatrix(j, i, 1);
            var w    = new DenseMatrix(i, this._x.ColumnCount);
            var l    = new DenseVector(i);

            for (int h = 0; h < i; h++)
            {
                double sumt = 0;
                for (int ddd = 0; ddd < j; ddd++)
                {
                    sumt += Math.Abs(told[ddd, h] - t[ddd, h]);
                }

                while ((sumt > 1E-15 && k < 100))
                {
                    told.SetColumn(h, t.Column(h));
                    k++;
                    var wt = t.Column(h) * this._x / t.Column(h).DotProduct(t.Column(h));//更新更改
                    wt = wt / Math.Sqrt(wt.DotProduct(wt));
                    w.SetRow(h, wt);
                    t.SetColumn(h, this._x * w.Row(h) / w.Row(h).DotProduct(w.Row(h)));
                }
                l[h] = k;
                k    = 0;

                var pt = t.Column(h) * this._x / t.Column(h).DotProduct(t.Column(h));

                pt = pt / Math.Sqrt(pt.DotProduct(pt));
                var tempt = Math.Sqrt(pt.DotProduct(pt));
                p.SetRow(h, pt);
                t.SetColumn(h, t.Column(h) / tempt);
                w.SetRow(h, w.Row(h) / tempt);
                this._x = (Matrix)(this._x - t.Column(h).ToColumnMatrix() * p.Row(h).ToRowMatrix());
            }
            var G = t.Transpose();


            var dist = new EuclidianDistance();
            var MM   = Matlab.PDist((Matrix)G, dist, VectorType.Column);

            var M = Matlab.Triu(MM);

            double[] maxVal;
            int[]    maxIdx;
            Matlab.Max(M, out maxVal, out maxIdx);

            var    ttt  = new DenseVector(maxVal);
            int    idx  = ttt.MaximumIndex();
            double temp = ttt.Maximum();


            GroupAIndex.AddRange(new int[] { idx, maxIdx[idx] });


            for (int tr = 2; tr < p2; tr++)
            {
                var tm = new DenseMatrix(GroupAIndex.Count, MM.ColumnCount);
                for (int kkk = 0; kkk < GroupAIndex.Count; kkk++)
                {
                    tm.SetRow(kkk, MM.Column(GroupAIndex[kkk]));
                    foreach (var r in GroupAIndex)
                    {
                        tm[kkk, r] = double.NaN;
                    }
                }
                int[]    midx;
                double[] minval;
                Matlab.Min(tm, out minval, out midx);
                var minttt = new DenseVector(minval);
                temp = minttt.Maximum();
                idx  = minttt.MaximumIndex();
                GroupAIndex.Add(idx);
            }
            GroupAIndex.Sort();
            for (int r = 0; r < j; r++)
            {
                if (!GroupAIndex.Contains(r))
                {
                    GroupBIndex.Add(r);
                }
            }

            return(new SelectorResult()
            {
                GroupAIndex = GroupAIndex.ToArray(), GroupBIndex = GroupBIndex.ToArray()
            });
        }