Пример #1
0
    public static Pixbuf Points(ManagedNN network, NeuralNetworkOptions opts, double threshold, ManagedArray x, int width, int height, int f1 = 0, int f2 = 0)
    {
        var pixbuf = Common.Pixbuf(width, height, new Color(255, 255, 255));

        Points(pixbuf, network, opts, threshold, x, width, height, f1, f2);

        return(pixbuf);
    }
    public static string Serialize(ManagedNN network)
    {
        var model = Convert(network);

        string output = JsonConvert.SerializeObject(model);

        return(output);
    }
Пример #3
0
    public static void Points(Pixbuf pixbuf, ManagedNN network, NeuralNetworkOptions opts, double threshold, ManagedArray x, int width, int height, int f1 = 0, int f2 = 0)
    {
        var m = Rows(x);

        minx = Double.MaxValue;
        maxx = Double.MinValue;

        miny = Double.MaxValue;
        maxy = Double.MinValue;

        f1 = f1 >= 0 && f1 < Cols(x) ? f1 : 0;
        f2 = f2 >= 0 && f2 < Cols(x) ? f2 : 0;

        for (var j = 0; j < m; j++)
        {
            minx = Math.Min(x[f1, j], minx);
            maxx = Math.Max(x[f1, j], maxx);

            miny = Math.Min(x[f2, j], miny);
            maxy = Math.Max(x[f2, j], maxy);
        }

        deltax = (maxx - minx) / width;
        deltay = (maxy - miny) / height;

        minx = minx - 8 * deltax;
        maxx = maxx + 8 * deltax;
        miny = miny - 8 * deltay;
        maxy = maxy + 8 * deltay;

        deltax = (maxx - minx) / width;
        deltay = (maxy - miny) / height;

        var colors = Common.Palette2();

        colors.Shuffle();

        var PlotOptions = opts;

        PlotOptions.Items = Rows(x);

        var classification = network.Classify(x, PlotOptions, threshold);

        Points(pixbuf, x, classification, colors, f1, f2);

        // Plot bounding box
        var cw     = pixbuf.Width - 1;
        var ch     = pixbuf.Height;
        var border = new Color(128, 128, 128);

        Common.Line(pixbuf, 0, 1, cw, 1, border);
        Common.Line(pixbuf, cw, 1, cw, ch, border);
        Common.Line(pixbuf, 0, ch, cw, ch, border);
        Common.Line(pixbuf, 0, 1, 0, ch, border);

        ManagedOps.Free(classification);
    }
    public static ManagedNNJSON Convert(ManagedNN network)
    {
        var model = new ManagedNNJSON()
        {
            Wji = Convert2D(network.Wji),
            Wkj = Convert2D(network.Wkj)
        };

        model.Normalization.Add(network.Min);
        model.Normalization.Add(network.Max);

        return(model);
    }
    public static ManagedNN Deserialize(string json)
    {
        var model = JsonConvert.DeserializeObject <ManagedNNJSON>(json);

        var network = new ManagedNN();

        if (model.Wji != null && model.Wkj != null)
        {
            network.Wji = Set(model.Wji);
            network.Wkj = Set(model.Wkj);
        }

        if (model.Normalization != null && model.Normalization.Count > 1)
        {
            network.Min = model.Normalization[0];
            network.Max = model.Normalization[1];
        }

        return(network);
    }
Пример #6
0
    public static Pixbuf Contour(ManagedNN network, NeuralNetworkOptions opts, double threshold, ManagedArray x, int width, int height, int f1 = 0, int f2 = 0)
    {
        InitializeContour(11, width, height);

        var m = Rows(x);

        var xplot = new double[width];
        var yplot = new double[height];
        var data  = new double[height, width];

        minx = Double.MaxValue;
        maxx = Double.MinValue;

        miny = Double.MaxValue;
        maxy = Double.MinValue;

        f1 = f1 >= 0 && f1 < Cols(x) ? f1 : 0;
        f2 = f2 >= 0 && f2 < Cols(x) ? f2 : 0;

        for (var j = 0; j < m; j++)
        {
            minx = Math.Min(x[f1, j], minx);
            maxx = Math.Max(x[f1, j], maxx);

            miny = Math.Min(x[f2, j], miny);
            maxy = Math.Max(x[f2, j], maxy);
        }

        deltax = (maxx - minx) / width;
        deltay = (maxy - miny) / height;

        minx = minx - 8 * deltax;
        maxx = maxx + 8 * deltax;
        miny = miny - 8 * deltay;
        maxy = maxy + 8 * deltay;

        deltax = (maxx - minx) / width;
        deltay = (maxy - miny) / height;

        // For predict
        for (var i = 0; i < width; i++)
        {
            xplot[i] = minx + i * deltax;
        }

        for (var i = 0; i < height; i++)
        {
            yplot[i] = miny + i * deltay;
        }

        var xx = new ManagedArray(2, height);

        for (var i = 0; i < width; i++)
        {
            for (var j = 0; j < height; j++)
            {
                xx[f1, j] = xplot[i];
                xx[f2, j] = yplot[j];
            }

            var p = network.Predict(xx, opts);

            for (var j = 0; j < height; j++)
            {
                data[i, j] = p[j];
            }

            ManagedOps.Free(p);
        }

        var z = new double[] { 0.6, 0.8, 1 };

        Conrec.Contour(data, xplot, yplot, z, ContourLine);

        Points(ContourGraph, network, opts, threshold, x, width, height, f1, f2);

        ManagedOps.Free(xx);

        var border = new Color(128, 128, 128);

        // Plot bounding box
        var cw = ContourGraph.Width - 1;
        var ch = ContourGraph.Height;

        Common.Line(ContourGraph, 0, 1, cw, 1, border);
        Common.Line(ContourGraph, cw, 1, cw, ch, border);
        Common.Line(ContourGraph, 0, ch, cw, ch, border);
        Common.Line(ContourGraph, 0, 1, 0, ch, border);

        return(ContourGraph);
    }