Demo entry 6662853

machine_controller

   

Submitted by anonymous on Nov 28, 2017 at 05:49
Language: C#. Code size: 6.4 kB.

public class TransferedSVM
    {
        public double u { get; set; } = 0.5;
        public double C { get; set; } = 0.6;
        private int m { get; set; }
        private int T { get; set; } = 2;

        private List<List<double>> OldX { get; set; }
        private List<List<double>> NewX { get; set; }

        private List<List<double>> a { get; set; }

        public void Train(List<List<double>> old_x, List<double> old_y, List<List<double>> new_x, List<double> new_y)
        {
            OldX = old_x;
            NewX = new_x;

            a = new List<List<double>>();
            m = old_x.Count;


            for (int i = 0; i < m; i++)
            {
                a.Add(new List<double> { 0, 0 });
            }


            int max_iter = 40;

            Random r = new Random();

            int iter = 0;
            int alpha_change = 0;
            while (iter < max_iter)
            {
                alpha_change = 0;

                for (int i = 0; i < 2*m; i++)
                {
                    int ii = i % m;
                    int ti = i / m;

                    List<double> xi = null;
                    if (ti == 0)
                    {
                        xi = OldX[ii];
                    }
                    else
                    {
                        xi = NewX[ii];
                    }

                    var pre_Li = F_s(ti, xi);

                    double? labeli = null;

                    if (ti == 0)
                    {
                        labeli = old_y[ii];
                    }
                    else
                    {
                        labeli = new_y[ii];
                    }

                    var Ei = pre_Li - labeli;

                    if ((labeli * Ei < -0.001 && a[ii][ti] < C) || (labeli * Ei > 0.001 && a[ii][ti] > 0))
                    {
                        var ij = r.Next(m);
                        var tj = r.Next(2);

                        if (ij == ii && tj == ti)
                        {
                            var temp = true;
                            while (temp)
                            {
                                ij = r.Next(m);
                                tj = r.Next(2);

                                if (ij != ii || tj != ti)
                                {
                                    temp = false;
                                }
                            }
                        }

                        List<double> xj = null;
                        if (tj == 0)
                        {
                            xj = OldX[ij];
                        }
                        else
                        {
                            xj = NewX[ij];
                        }

                        double? labelj = null;

                        if (tj == 0)
                        {
                            labelj = old_y[ij];
                        }
                        else
                        {
                            labelj = new_y[ij];
                        }

                        var pre_Lj = F_s(tj, xj);

                        var Ej = pre_Lj - labelj;

                        double? L = null;
                        double? H = null;

                        if (labeli != labelj)
                        {
                            L = Math.Max(0, a[ii][ti] - a[ij][tj]);
                            H = Math.Min(C, C + a[ii][ti] - a[ij][tj]);
                        }
                        else
                        {
                            L = Math.Max(0, a[ii][ti] + a[ij][tj] - C);
                            H = Math.Min(C, a[ii][ti] + a[ij][tj]);
                        }

                        if (L == H)
                        {
                            continue;
                        }

                        var eta = 2 * K(ti, tj, xi, xj) - K(ti, ti, xi, xi) - K(tj, tj, xj, xj);

                        var alphasI_old = a[ii][ti];
                        var alphasJ_old = a[ij][tj];

                        a[ij][tj] = (double)(a[ij][tj] - labelj * (Ei - Ej) / eta);

                        if (a[ij][tj] > H)
                        {
                            a[ij][tj] = (double)H;
                        }
                        else if (a[ij][tj] < L)
                        {
                            a[ij][tj] = (double)L;
                        }

                        if (Math.Abs(a[ij][tj] - alphasJ_old) < 1e-4)
                        {
                            continue;
                        }

                        a[ii][ti] = a[ii][ti] + a[ii][ti] * a[ij][tj] * (alphasJ_old - a[ij][tj]);

                        //更新b,忽略

                        alpha_change++;
                        Debug.WriteLine($"alpha_changed: " +alpha_change);
                    }
                }

                if (alpha_change == 0)
                {
                    iter = iter + 1;
                }
                else
                {
                    iter = 0;
                }
            }
        }

        private double K(int s, int t, IList<double> x, IList<double> z)
        {
            return (1 / u + d(s, t)) * x.Dot(z);
        }

        private double d(int s,int t)
        {
            return s == t ? 1 : 0;
        }

        public double F_s(int t,List<double> x)
        {
            double sum = 0;
            for (int i = 0; i < m; i++)
            {
                for (int s = 0; s < 2; s++)
                {
                    var a_ = a[i][s];
                    List<double> x_is = null;
                    if (s==0)
                    {
                        x_is = OldX[i];
                    }
                    else if(s==1)
                    {
                        x_is = NewX[i];
                    }

                    var k_ = K(s, t, x_is, x);
                    sum += a_ * k_;
                }
            }

            return sum;
        }

        public double Predict(int T, List<double> X)
        {
            return F_s(T, X);
        }

    }

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).