コード例 #1
0
    public Matrix <double>[] Gradients(Matrix <double> x, int action, double qtarget)
    {
        //Forward
        Matrix <double> x1 = NNOperations.matrixmul(W [0], x);
        Matrix <double> x2 = NNOperations.add(W [1], x1);
        Matrix <double> x3 = NNOperations.sigm(x2);
        Matrix <double> x4 = NNOperations.matrixmul(W [2], x3);
        Matrix <double> x5 = NNOperations.add(W [3], x4);
        //Loss

        Matrix <double> ind = Matrix <double> .Build.Dense(x5.RowCount, 1);

        ind [action, 0] = 1.0;
        Matrix <double> x6    = x5.PointwiseMultiply(ind);
        Matrix <double> ygold = ind.Multiply(qtarget);

        Matrix <double>[] gx6_gyg  = NNOperations.mse_b(x6, ygold, 1.0);
        Matrix <double>[] gx5_gind = NNOperations.ewisemul_b(x5, ind, gx6_gyg [0]);
        Matrix <double>[] gw3_gx4  = NNOperations.add_b(W[3], x4, gx5_gind[0]);
        Matrix <double>[] gw2_gx3  = NNOperations.matrixmul_b(W[2], x3, gw3_gx4[1]);
        Matrix <double>   gx2      = NNOperations.sigm_b(x2, x3, gw2_gx3[1]);

        Matrix <double>[] gw1_gx1 = NNOperations.add_b(W[1], x1, gx2);
        Matrix <double>[] gw0_gx  = NNOperations.matrixmul_b(W[0], x, gw1_gx1[1]);

        Matrix <double> [] gradients = { gw0_gx[0], gw1_gx1[0], gw2_gx3[0], gw3_gx4[0] };

        return(gradients);
    }
コード例 #2
0
 public Matrix <double> lossbackward(Matrix <double>[] x)
 {
     //Layer1
     Matrix <double>[] gx1_gyg = NNOperations.mse_b(x[2], x[3], 1.0);
     Matrix <double>[] gx_gind = NNOperations.ewisemul_b(x[0], x[1], gx1_gyg [0]);
     return(gx_gind [0]);
 }
コード例 #3
0
    public Matrix <double>[] Gradients(Matrix <double> x, Vector <double> ygold)
    {
        //Forward
        Matrix <double> x1 = NNOperations.matrixmul(W [0], x);
        Matrix <double> x2 = NNOperations.add(W [1], x1);
        Matrix <double> x3 = NNOperations.sigm(x2);
        Matrix <double> x4 = NNOperations.matrixmul(W [2], x3);
        Matrix <double> x5 = NNOperations.add(W [3], x4);
        //Loss
        //double loss = NNOperations.mse (x5.Column(0),ygold);
        //Backward
        Matrix <double> ym = ygold.ToColumnMatrix();

        Matrix <double>[] gx5_gym = NNOperations.mse_b(x5, ym, 1.0);
        Matrix <double>[] gw3_gx4 = NNOperations.add_b(W[3], x4, gx5_gym[0]);
        Matrix <double>[] gw2_gx3 = NNOperations.matrixmul_b(W[2], x3, gw3_gx4[1]);
        Matrix <double>   gx2     = NNOperations.sigm_b(x2, x3, gw2_gx3[1]);

        Matrix <double>[] gw1_gx1 = NNOperations.add_b(W[1], x1, gx2);
        Matrix <double>[] gw0_gx  = NNOperations.matrixmul_b(W[0], x, gw1_gx1[1]);

        Matrix <double> [] gradients = { gw0_gx[0], gw1_gx1[0], gw2_gx3[0], gw3_gx4[0] };

        return(gradients);
    }
コード例 #4
0
        void Test()
        {
            Stopwatch t = new Stopwatch();

            t.Start();

            for (int i = 0; i < TestingSet.Size; i++)
            {
                Image image = TrainingSet.Data[i];
                uint  label = TrainingSet.Labels[i];

                Vector labelVector = Vector.Create(10, 0f);
                labelVector[(int)label] = 1;

                Network.Input.Nodes = new Vector(image.NormalizedPixels);
                Network.FeedForward();

                Vector loss    = NNOperations.OutputLoss(Network.Output, labelVector, Network.Properties.LossFunction);
                float  avgLoss = 0;
                loss.ForEach(v => avgLoss += v);
                avgLoss /= loss.Length;

                Console.WriteLine(Network.Output.Nodes.ToString("Output (Index: " + i + ") (avg: " + avgLoss + ")"));
            }

            t.Stop();
            Console.WriteLine("Testing time: " + t.ElapsedMilliseconds + "ms");
        }
コード例 #5
0
 public Matrix <double>[] lastbackward(Matrix <double>[] x, int i, Matrix <double> gy)
 {
     //Layer1
     Matrix <double>[] gw1_gx1   = NNOperations.add_b(W[i + 1], x[1], gy);
     Matrix <double>[] gw0_gx    = NNOperations.matrixmul_b(W[i], x[0], gw1_gx1[1]);
     Matrix <double>[] gradients = { gw0_gx [0], gw1_gx1 [0], gw0_gx [1] };
     return(gradients);
 }
コード例 #6
0
    public Matrix <double>[] lastlayer(Matrix <double> x, int i)
    {
        //Layer1
        Matrix <double> x1 = NNOperations.matrixmul(W [i], x);
        Matrix <double> x2 = NNOperations.add(W [i + 1], x1);

        Matrix <double>[] results = { x, x1, x2 };
        return(results);
    }
コード例 #7
0
    public Matrix <double>[] fcbackward(System.Func <Matrix <double>, Matrix <double>, Matrix <double>, Matrix <double> > activation_b, Matrix <double>[] x, int i, Matrix <double> gy)
    {
        //Layer1
        Matrix <double> gx2 = activation_b(x[2], x[3], gy);

        Matrix <double>[] gw1_gx1   = NNOperations.add_b(W[i + 1], x[1], gx2);
        Matrix <double>[] gw0_gx    = NNOperations.matrixmul_b(W[i], x[0], gw1_gx1[1]);
        Matrix <double>[] gradients = { gw0_gx [0], gw1_gx1 [0], gw0_gx [1] };
        return(gradients);
    }
コード例 #8
0
    public Matrix <double>[] fclayer(System.Func <Matrix <double>, Matrix <double> > activation, Matrix <double> x, int i)
    {
        //Layer1
        Matrix <double> x1 = NNOperations.matrixmul(W [i], x);
        Matrix <double> x2 = NNOperations.add(W [i + 1], x1);
        Matrix <double> x3 = activation(x2);

        Matrix <double>[] results = { x, x1, x2, x3 };
        return(results);
    }
コード例 #9
0
    public double Loss(Matrix <double> x, int action, double qtarget)
    {
        Matrix <double> x5  = Forward(x);
        Matrix <double> ind = Matrix <double> .Build.Dense(x5.RowCount, 1);

        ind [action, 0] = 1.0;
        Matrix <double> x6 = x5.PointwiseMultiply(ind);

        ind [action, 0] = qtarget;
        return(NNOperations.mse(x6, ind));
    }
コード例 #10
0
    public Matrix <double> Forward(Matrix <double> x)
    {
        //Layer1
        Matrix <double> x1 = NNOperations.matrixmul(W [0], x);
        Matrix <double> x2 = NNOperations.add(W [1], x1);
        Matrix <double> x3 = NNOperations.sigm(x2);
        //Layer2
        Matrix <double> x4 = NNOperations.matrixmul(W [2], x3);
        Matrix <double> x5 = NNOperations.add(W [3], x4);

        return(x5);
    }
コード例 #11
0
    public Matrix <double>[] losslayer(Matrix <double> x, int action, double qtarget)
    {
        //Layer1
        Matrix <double> ind = Matrix <double> .Build.Dense(x.RowCount, 1);

        ind [action, 0] = 1.0;
        Matrix <double> x1    = x.PointwiseMultiply(ind);
        Matrix <double> ygold = ind.Multiply(qtarget);
        Matrix <double> loss  = M.Dense(1, 1, NNOperations.mse(x1, ygold));

        Matrix <double>[] results = { x, ind, x1, ygold, loss };
        return(results);
    }
コード例 #12
0
        public NNBayes(int[] numNeurons, params object[] parameters)
        {
            float l2Penalty = 0.1F;

            _preds = new TFOutput[numSamples];

            _session = new TFSession();

            _graph = _session.Graph;

            _graph.Seed = Global.Random.Next();

            _layers = new BayesLayer[numNeurons.Length - 1];

            _input = _graph.Placeholder(TFDataType.Float, new TFShape(-1, numNeurons[0]));

            _output = _graph.Placeholder(TFDataType.Float, new TFShape(-1));

            _decay = _graph.Placeholder(TFDataType.Float, new TFShape(1));

            for (int i = 0; i < numNeurons.Length - 1; i++)
            {
                _layers[i] = new BayesLayer(_graph, numNeurons[i], numNeurons[i + 1]);
            }


            TFOutput likelihood = _graph.Const(0F);


            for (int i = 0; i < numSamples; i++)
            {
                TFOutput act = _input;

                foreach (BayesLayer layer in _layers)
                {
                    // TFOutput W = layer.SampleW(_graph);
                    // TFOutput b = layer.Sampleb(_graph);
                    // TFOutput z = _graph.Add(_graph.MatMul(act, W), b);

                    TFOutput z = layer.Samplez(_graph, act);

                    if (layer == _layers.Last())
                    {
                        TFOutput pred = _graph.Reshape(z, _graph.Const(new TFShape(-1)));
                        _preds[i] = pred;
                        TFOutput sample_likelihood = NNOperations.LogOfNormal(_graph, pred, _output, _graph.Const(1F));
                        likelihood = _graph.Add(likelihood, sample_likelihood);
                    }
                    else
                    {
                        act = _graph.Relu(z);
                    }
                }
            }


            TFOutput kl_W = _graph.Const(0F);
            TFOutput kl_b = _graph.Const(0F);

            foreach (BayesLayer layer in _layers)
            {
                kl_W = _graph.Add(kl_W, _graph.ReduceSum(NNOperations.KLUnivariateNormal(_graph, layer.Mu_W, NNOperations.LogTrans(_graph, layer.Phi_W), _graph.Const(0F), _graph.Const((float)(1 / (Math.Sqrt(l2Penalty)))))));
                kl_b = _graph.Add(kl_b, _graph.ReduceSum(NNOperations.KLUnivariateNormal(_graph, layer.Mu_b, NNOperations.LogTrans(_graph, layer.Phi_b), _graph.Const(0F), _graph.Const((float)(1 / Math.Sqrt(l2Penalty))))));
            }

            TFOutput kl = _graph.Add(kl_W, kl_b);

            likelihood = _graph.Div(likelihood, _graph.Const((float)numSamples));

            _cost = _graph.ReduceMean(_graph.Sub(_graph.Mul(_decay, kl), likelihood));

            optimizer = new AdamOptimizer();

            optimizer.AddBayesLayer(_graph, _layers, _cost);

            optimizer.Apply(_graph);

            var runner = _session.GetRunner();

            foreach (BayesLayer layer in _layers)
            {
                layer.Init(runner);
            }

            optimizer.Init(runner);

            runner.Run();
        }
コード例 #13
0
    public double Loss(Matrix <double> x, Vector <double> ygold)
    {
        Matrix <double> y = Forward(x);

        return(NNOperations.mse(y, ygold.ToColumnMatrix()));
    }