Ejemplo n.º 1
0
        public override float OutputsDense(Floatarray result, Floatarray x_raw)
        {
            CHECK_ARG(x_raw.Length() == w1.Dim(1), "x_raw.Length() == w1.Dim(1)");
            Floatarray z      = new Floatarray();
            int        sparse = PGeti("sparse");
            Floatarray y      = new Floatarray();
            Floatarray x      = new Floatarray();

            x.Copy(x_raw);
            mvmul0(y, w1, x);
            y += b1;
            for (int i = 0; i < y.Length(); i++)
            {
                y[i] = sigmoid(y[i]);
            }
            if (sparse > 0)
            {
                ClassifierUtil.Sparsify(y, sparse);
            }
            mvmul0(z, w2, y);
            z += b2;
            for (int i = 0; i < z.Length(); i++)
            {
                z[i] = sigmoid(z[i]);
            }
            result.Copy(z);
            //int idx = NarrayUtil.ArgMax(result);
            //float val = NarrayUtil.Max(result);
            return(Convert.ToSingle(Math.Abs(NarrayUtil.Sum(z) - 1.0)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// do a single stochastic gradient descent step
        /// </summary>
        public void TrainOne(Floatarray z, Floatarray target, Floatarray x, float eta)
        {
            CHECK_ARG(target.Length() == w2.Dim(0), "target.Length() == w2.Dim(0)");
            CHECK_ARG(x.Length() == w1.Dim(1), "x.Length() == w1.Dim(1)");

            int        sparse  = PGeti("sparse");
            int        nhidden = this.nHidden();
            int        noutput = nClasses();
            Floatarray delta1  = new Floatarray(nhidden);
            Floatarray delta2  = new Floatarray(noutput);
            Floatarray y       = new Floatarray(nhidden);

            mvmul0(y, w1, x);
            y += b1;
            for (int i = 0; i < nhidden; i++)
            {
                y[i] = sigmoid(y[i]);
            }
            if (sparse > 0)
            {
                ClassifierUtil.Sparsify(y, sparse);
            }
            mvmul0(z, w2, y);
            z += b2;
            for (int i = 0; i < noutput; i++)
            {
                z[i] = sigmoid(z[i]);
            }

            for (int i = 0; i < noutput; i++)
            {
                delta2[i] = (z[i] - target[i]) * dsigmoidy(z[i]);
            }
            vmmul0(delta1, delta2, w2);
            for (int i = 0; i < nhidden; i++)
            {
                delta1[i] = delta1[i] * dsigmoidy(y[i]);
            }

            outer_add(w2, delta2, y, -eta);
            for (int i = 0; i < noutput; i++)
            {
                b2[i] -= eta * delta2[i];
            }
            outer_add(w1, delta1, x, -eta);
            for (int i = 0; i < nhidden; i++)
            {
                b1[i] -= eta * delta1[i];
            }
        }