Esempio n. 1
0
 public void add <Type>(Type n)
 {
     if (n is NNMatrix)
     {
         for (int i = 0; i < this.rows; i++)
         {
             for (int j = 0; j < this.cols; j++)
             {
                 NNMatrix mat = (NNMatrix)(object)n;
                 this.data[i][j] += mat.data[i][j];
             }
         }
     }
     else
     {
         for (int i = 0; i < this.rows; i++)
         {
             for (int j = 0; j < this.cols; j++)
             {
                 float val = (float)(object)n;
                 this.data[i][j] += val;
             }
         }
     }
 }
Esempio n. 2
0
    public static NNMatrix multiply(NNMatrix a, NNMatrix b)
    {
        if (a.cols != b.rows)
        {
            Debug.Log("Columns of A must match rows of B.");
            return(null);
        }

        NNMatrix result = new NNMatrix(a.rows, b.cols);

        for (int i = 0; i < result.rows; i++)
        {
            for (int j = 0; j < result.cols; j++)
            {
                float sum = 0f;
                for (int k = 0; k < a.cols; k++)
                {
                    sum += a.data[i][k] * b.data[k][j];
                }

                result.data[i][j] = sum;
            }
        }

        return(result);
    }
Esempio n. 3
0
    public void train(List <float> input_list, List <float> target_list)
    {
        NNMatrix inputs = NNMatrix.fromList(input_list);

        NNMatrix hidden = NNMatrix.multiply(this.weights_ih, inputs);

        hidden.add(this.bias_h);
        // activate function
        hidden.map(ActivateFunction.sigmoid);

        NNMatrix outputs = NNMatrix.multiply(this.weights_ho, hidden);

        outputs.add(this.bias_o);
        outputs.map(ActivateFunction.sigmoid);

        NNMatrix targets = NNMatrix.fromList(target_list);

        // calculate the error
        // ERROR = TARGETS(答え) - OUTPUTS(実際の出力)
        NNMatrix output_errors = NNMatrix.subtract(targets, outputs);


        //var gradients = outputs * (1-outputs);
        // calculate gradient
        NNMatrix gradients = NNMatrix.map(outputs, ActivateFunction.dsigmoid);

        gradients.multiply(output_errors);
        gradients.multiply(this.learning_rate);

        // calculate deltas
        NNMatrix hidden_T          = NNMatrix.transpose(hidden);
        NNMatrix weights_ho_deltas = NNMatrix.multiply(gradients, hidden_T);

        // Adjust the weights by deltas
        this.weights_ho.add(weights_ho_deltas);

        // Adjust bias by its delta (which is just the gradients)
        this.bias_o.add(gradients);

        // calculate the hidden layer errors
        NNMatrix who_t         = NNMatrix.transpose(this.weights_ho);
        NNMatrix hidden_errors = NNMatrix.multiply(who_t, output_errors);



        // calculate hidden gradients
        NNMatrix hidden_gradient = NNMatrix.map(hidden, ActivateFunction.dsigmoid);

        hidden_gradient.multiply(hidden_errors);
        hidden_gradient.multiply(this.learning_rate);

        // calculate input->hidden deltas
        NNMatrix input_T          = NNMatrix.transpose(inputs);
        NNMatrix weight_ih_deltas = NNMatrix.multiply(hidden_gradient, input_T);

        this.weights_ih.add(weight_ih_deltas);

        // Adjust bias by its delta (which is just the hiddent gradients)
        this.bias_h.add(hidden_gradient);
    }
Esempio n. 4
0
 private void Init(NNMatrix m)
 {
     for (int i = 1; i <= m.NbRows; i++)
     {
         for (int j = 1; j <= m.NbCols; j++)
         {
             m[i - 1, j - 1] = i * 10 + j;
         }
     }
 }
Esempio n. 5
0
    public static NNMatrix fromList(List <float> list)
    {
        NNMatrix m = new NNMatrix(list.Count, 1);

        for (int i = 0; i < list.Count; i++)
        {
            m.data[i][0] = list[i];
        }
        return(m);
    }
Esempio n. 6
0
        public void MatrixConstructors()
        {
            var m1 = new NNMatrix(2, 3);
            Assert.AreEqual(m1.NbRows, 2);
            Assert.AreEqual(m1.NbCols, 3);
            Init(m1);

            var m2 = new NNMatrix(data);
            Assert.AreEqual(m2.NbRows, 2);
            Assert.AreEqual(m2.NbCols, 3);
            Assert.AreEqual(m1.ToString(), m2.ToString());
        }
Esempio n. 7
0
    public static NNMatrix transpose(NNMatrix matrix)
    {
        NNMatrix result = new NNMatrix(matrix.cols, matrix.rows);

        for (int i = 0; i < matrix.rows; i++)
        {
            for (int j = 0; j < matrix.cols; j++)
            {
                result.data[j][i] = matrix.data[i][j];
            }
        }
        return(result);
    }
Esempio n. 8
0
    public static NNMatrix subtract(NNMatrix a, NNMatrix b)
    {
        NNMatrix result = new NNMatrix(a.rows, a.cols);

        for (int i = 0; i < result.rows; i++)
        {
            for (int j = 0; j < result.cols; j++)
            {
                result.data[i][j] = a.data[i][j] - b.data[i][j];
            }
        }
        return(result);
    }
Esempio n. 9
0
        public void MatrixCalc()
        {
            //  W*I+B  test calculattin Input*Weights + Biases
            int nbInput = 6;
            int nbOutput = 3;
            NNArray I = NNArray.Random(nbInput);
            NNMatrix W = NNMatrix.Random(nbInput, nbOutput);
            NNArray B = NNArray.Random(nbOutput);

            var tmp = W * I;
            var O = tmp + B;

            Assert.AreEqual(O.Length, nbOutput);
        }
Esempio n. 10
0
    public static NNMatrix map(NNMatrix matrix, System.Func <float, float> func)
    {
        NNMatrix result = new NNMatrix(matrix.rows, matrix.cols);

        for (int i = 0; i < matrix.rows; i++)
        {
            for (int j = 0; j < matrix.cols; j++)
            {
                float val = matrix.data[i][j];
                result.data[i][j] = func(val);
            }
        }

        return(result);
    }
Esempio n. 11
0
    public NeuralNetwork(int _input_nodes, int _hidden_nodes, int _output_nodes)
    {
        this.input_nodes  = _input_nodes;
        this.hidden_nodes = _hidden_nodes;
        this.output_nodes = _output_nodes;

        // weight between input and hidden
        this.weights_ih = new NNMatrix(this.hidden_nodes, this.input_nodes);
        this.weights_ih.randomize();

        // weight between hidden and output
        this.weights_ho = new NNMatrix(this.output_nodes, this.hidden_nodes);
        this.weights_ho.randomize();

        this.bias_h = new NNMatrix(this.hidden_nodes, 1);
        this.bias_o = new NNMatrix(this.output_nodes, 1);
    }
Esempio n. 12
0
        public void MatrixMult()
        {
            NNMatrix M1 = NNMatrix.Random(2, 2); // 3 row * 4 Columns
            NNMatrix M2 = NNMatrix.Identity(2, 2);

            var M3 = M1 * M2;

            Assert.IsTrue(M1.Equals(M3));

            M1 = NNMatrix.Random(3, 4); // 3 row * 4 Columns
            M2 = NNMatrix.Random(4, 2);

            M3 = M1 * M2;

            Assert.AreEqual(M3.NbRows, 3);
            Assert.AreEqual(M3.NbCols, 2);
        }
Esempio n. 13
0
    public List <float> feedforward(List <float> input_list)
    {
        NNMatrix inputs = NNMatrix.fromList(input_list);

        NNMatrix hidden = NNMatrix.multiply(this.weights_ih, inputs);

        hidden.add(this.bias_h);

        // activate function
        hidden.map(ActivateFunction.sigmoid);

        NNMatrix output = NNMatrix.multiply(this.weights_ho, hidden);

        output.add(this.bias_o);
        output.map(ActivateFunction.sigmoid);

        return(output.toList());
    }
Esempio n. 14
0
    public NeuralNetwork copy()
    {
        int _input_nodes  = this.input_nodes;
        int _hidden_nodes = this.hidden_nodes;
        int _output_nodes = this.output_nodes;

        NNMatrix wih = this.weights_ih;
        NNMatrix who = this.weights_ho;

        float lr = this.learning_rate;

        NNMatrix bh = this.bias_h;
        NNMatrix oh = this.bias_o;

        NeuralNetwork nn = new NeuralNetwork(_input_nodes, _hidden_nodes, _output_nodes);

        nn.weights_ih    = wih;
        nn.weights_ho    = who;
        nn.learning_rate = lr;
        nn.bias_h        = bh;
        nn.bias_o        = oh;
        return(nn);
    }
Esempio n. 15
0
        public void MatrixPerformance()
        {
            //  W*I+B  test calculattin Input*Weights + Biases
            int nbInput = 300;
            int nbOutput = 200;

            NNArray I = NNArray.Random(nbInput);
            NNMatrix W = NNMatrix.Random(nbInput, nbOutput);
            NNArray B = NNArray.Random(nbOutput);

            int count = 200;
            DateTime start;
            DateTime end;
            double duration;

            // Not optimized
            start = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                NNArray res = W * I + B;
            }
            end = DateTime.Now;
            duration = (end - start).TotalMilliseconds / 1000;
            Console.WriteLine("Duration Non Optimized: "+duration);

            // Optimized
            NNArray O = NNArray.Random(nbOutput);
            start = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                W.Multiply(I,O);
                O.Add(B,O);
            }
            end = DateTime.Now;
            duration = (end - start).TotalMilliseconds / 1000;
            Console.WriteLine("Duration Optimized: " + duration);
        }
Esempio n. 16
0
        public void MatrixRowCol()
        {
            var m1 = new NNMatrix(data);
            string s1 = m1.ToString();

            var m2 = new NNMatrix(2, 3);
            Init(m2);
            Assert.AreEqual(m1.ToString(), m2.ToString());

            NNMatrix M = new NNMatrix(2, 3); // 2 row * 3 Columns
            M[0, 0] = 11;
            M[0, 1] = 12;
            M[0, 2] = 13;
            M[1, 0] = 11;
            M[1, 1] = 12;
            M[1, 2] = 13;

            var s = M.ToString();

            Assert.AreEqual(2, m1.NbRows);
            Assert.AreEqual(2, M.NbRows);
            Assert.AreEqual(3, m1.NbCols);
            Assert.AreEqual(3, M.NbCols);
        }
Esempio n. 17
0
    /// <summary>
    /// 计算DOP值,返回可见卫星数
    /// </summary>
    /// <param name="tle"></param>
    /// <param name="time">要计算的时间</param>
    /// <param name="B">测站的,单位:度</param>
    /// <param name="L"></param>
    /// <param name="H">单位:米</param>
    /// <param name="cor_limit">截止高度角,度</param>
    /// <param name="GDOP"></param>
    /// <param name="PDOP"></param>
    /// <param name="HDOP"></param>
    /// <param name="VDOP"></param>
    /// <param name="TDOP"></param>
    /// <returns></returns>
    public static int CalcDops(Tle[] tles, DateTime time, double B, double L, double H, double cor_limit, ref double GDOP, ref double PDOP, ref double HDOP, ref double VDOP, ref double TDOP)
    {
        double x2 = 0; double y2 = 0; double z2 = 0; //测站坐标

        BLToXYZ(B / 180 * Math.PI, L / 180 * Math.PI, H, ref x2, ref y2, ref z2);
        ArrayList temp         = new ArrayList();
        Site      siteEquator2 = new Site(B, L, H);
        Tle       tle;
        Orbit     orbit;
        Eci       eci;
        CoordGeo  cg;
        int       sum = 0;

        for (int i = 0; i < tles.Length; i++)
        {
            tle   = tles[i];
            orbit = new Orbit(tle);
            eci   = orbit.GetPosition(time);
            double x = eci.Position.X * 1000;
            double y = eci.Position.Y * 1000;
            double z = eci.Position.Z * 1000; //化成米
            cg = eci.ToGeo();
            CoordTopo topoLook = siteEquator2.GetLookAngle(eci);
            topoLook.Elevation = Globals.Rad2Deg(topoLook.Elevation);
            topoLook.Azimuth   = Globals.Rad2Deg(topoLook.Azimuth); //化成度
            if (topoLook.Elevation > cor_limit)
            {
                sum++;
                double d_x = x - x2;
                double d_y = y - y2;
                double d_z = z - z2;
                double r2  = Math.Sqrt(d_x * d_x + d_y * d_y + d_z * d_z);
                temp.Add(d_x / r2);
                temp.Add(d_y / r2);
                temp.Add(d_z / r2);
            }
        }
        NNMatrix Q   = new NNMatrix(sum, 4);
        NNMatrix Q_x = new NNMatrix(4, 4);

        for (int j = 0; j < temp.Count; j++)
        {
            if ((j + 1) % 3 == 1)
            {
                Q.Matrix[j / 3, 0] = (double)temp[j];
            }
            else if ((j + 1) % 3 == 2)
            {
                Q.Matrix[j / 3, 1] = (double)temp[j];
            }
            else if ((j + 1) % 3 == 0)
            {
                Q.Matrix[j / 3, 2] = (double)temp[j];
            }
            Q.Matrix[j / 3, 3] = 1;
        }
        Q_x  = NNMatrix.Invers(NNMatrix.Transpos(Q) * Q);
        GDOP = Math.Sqrt(Q_x.Matrix[0, 0] + Q_x.Matrix[1, 1] + Q_x.Matrix[2, 2] + Q_x.Matrix[3, 3]);
        PDOP = Math.Sqrt(Q_x.Matrix[0, 0] + Q_x.Matrix[1, 1] + Q_x.Matrix[2, 2]);
        HDOP = Math.Sqrt(Q_x.Matrix[0, 0] + Q_x.Matrix[1, 1]);
        VDOP = Math.Sqrt(Q_x.Matrix[2, 2]);
        TDOP = Math.Sqrt(Q_x.Matrix[3, 3]);
        return(sum);
    }
Esempio n. 18
0
 public void mutate()
 {
     this.weights_ih = NNMatrix.map(this.weights_ih, NNUtils.mutate);
     this.weights_ho = NNMatrix.map(this.weights_ho, NNUtils.mutate);
 }