コード例 #1
0
ファイル: LSTM.-RNN.cs プロジェクト: feitianyiren/WeaveNet
        public dynamic backward(dynamic grid)
        {
            var dh       = h_next_mul.backwardY(grid);
            var d_tanh_c = cell_tl.Backward(dh);
            //var dc_prev=c_next_mul.backwardY(d_tanh_c);


            var d_input_gate  = mulin_gate_mul.backward(d_tanh_c);
            var d_forget_gate = c_next_mul.backward(d_tanh_c);
            var d_cell_memory = mulin_gate_mul.backwardY(d_tanh_c);

            var d_output_gate = h_next_mul.backward(grid);// d_tanh_c
            var d_ai          = input_gate_s.Backward(d_input_gate);
            var d_af          = forget_gate_s.Backward(d_forget_gate);
            var d_ao          = output_gate_s.Backward(d_output_gate);
            var d_ac          = cell_memory_tl.Backward(d_cell_memory);

            var temp  = Matrix.cat(d_ai, d_af, 1);
            var temp2 = Matrix.cat(d_ac, d_ao, 1);
            var da    = Matrix.cat(temp, temp2, 1);

            // var daT=Matrix.T(da);
            ihweight = convLayerih.backweight(da);
            hhweight = convLayerhh.backweight(da);
            return(convLayerih.backward(da));
        }
コード例 #2
0
ファイル: GRU.cs プロジェクト: feitianyiren/WeaveNet
        public dynamic backward(dynamic grid)
        {
            var dy = convLayerhq.backward(grid);

            hqW = convLayerhq.backweight(grid);
            var DH   = dy;
            var DZHT = ZHTL.backward(dy);

            var dH_tilda = ZHTL.backwardY(dy);
            //var Dz1 = ZHTL.backward(DH);

            var Dz = ZHL.backward(DZHT);

            var DRHS = THL.Backward(dH_tilda);
            var DR   = RHM.backward(DRHS);//Add 因为加法的梯度等于本身 所以DRHS=DRH

            var DHHS = RHM.backwardY(DRHS);
            var DRS  = RSL.Backward(DR);
            var DZS  = ZSL.Backward(Dz);
            var temp = Matrix.cat(DZS, DRS, 1);
            var da   = Matrix.cat(temp, DHHS, 1);

            ihweight = convLayerih.backweight(da);
            hhweight = convLayerhh.backweight(da);
            return(convLayerih.backward(da));
        }
コード例 #3
0
        /// <summary>
        /// BP网络测试
        /// </summary>
        static void BP()
        {
            float[][] x  = JsonConvert.DeserializeObject <float[][]>(util.getstr("D:\\bpx.json")); //训练数据
            float[][] y  = JsonConvert.DeserializeObject <float[][]>(util.getstr("D:\\bpy.json")); //训练标签
            float[][] w1 = JsonConvert.DeserializeObject <float[][]>(util.getstr("D:\\bpw.json"));


            ConvLayer cl1 = new ConvLayer(13, 5, true);

            cl1.weights = w1;
            SigmodLayer sl  = new SigmodLayer();
            float       lr  = 0.5f;
            ConvLayer   cl2 = new ConvLayer(5, 1, true);
            //SigmodLayer s2 = new SigmodLayer();
            int i = 0, a = 0;

            while (a < 5000)
            {
                //i = 0;
                //while (i < 100)
                //{
                //    float[][] xx2 = new float[1][];
                //    xx2[0] = new float[x[0].GetLength(0)];

                //    for (var f = 0; f < x[0].GetLength(0); f++)
                //    {

                //        xx2[0][f] = x[i][f];
                //    }
                dynamic ff = cl1.Forward(x);
                ff = sl.Forward(ff);
                ff = cl2.Forward(ff);
                // dynamic ff22 = s2.forward(ff);
                //计算误差
                MSELoss mloss = new MSELoss();
                //float[][] yy2= new float[1][];
                //yy2[0] = y[i];
                var loss = mloss.Forward(ff, y);

                Console.WriteLine("误差:" + loss);

                dynamic grid = mloss.Backward();

                //反传播w2
                //  dynamic grid2 =s2.backward(grid);
                dynamic w22 = cl2.backweight(grid);

                //反传播W1
                dynamic grid1 = cl2.backward(grid);
                grid1 = sl.Backward(grid1);
                dynamic w11 = cl1.backweight(grid1);


                cl2.weights   = Matrix.MatrixSub(cl2.weights, Matrix.multiply(w22.grid, lr));
                cl2.basicData = Matrix.MatrixSub(cl2.basicData, Matrix.multiply(w22.basic, lr));

                cl1.weights   = Matrix.MatrixSub(cl1.weights, Matrix.multiply(w11.grid, lr));
                cl1.basicData = Matrix.MatrixSub(cl1.basicData, Matrix.multiply(w11.basic, lr));
                i++;
                // }
                a++;
            }

            //测试网络
            float[][] xx = new float[1][];
            xx[0] = new float[x[0].GetLength(0)];
            var aa = 3;

            for (var f = 0; f < x[0].GetLength(0); f++)
            {
                xx[0][f] = x[aa][f];
            }
            dynamic ff2 = cl1.Forward(xx);

            ff2 = sl.Forward(ff2);
            ff2 = cl2.Forward(ff2);

            util.prirt(ff2);
            util.prirt(y[aa]);
        }