Example #1
0
        public Vol Clone()
        {
            var V = new Vol(this.sx, this.sy, this.depth, 0.0f);
            var n = this.w.Length;

            for (var i = 0; i < n; i++)
            {
                V.w[i] = this.w[i];
            }
            return(V);
        }
Example #2
0
        public Vol Forward(Vol V, bool is_training)
        {
            this.in_act = V;
            var V2 = V.CloneAndZero();
            var N  = V.w.Length;

            for (var i = 0; i < N; i++)
            {
                V2.w[i] = Util.Tanh(V.w[i]);
            }
            this.out_act = V2;
            return(this.out_act);
        }
Example #3
0
        public Vol Forward(Vol V, bool is_training)
        {
            this.in_act = V;
            var V2  = V.CloneAndZero();
            var N   = V.w.Length;
            var V2w = V2.w;
            var Vw  = V.w;

            for (var i = 0; i < N; i++)
            {
                V2w[i] = 1.0f / (1.0f + (float)Math.Exp(-Vw[i]));
            }
            this.out_act = V2;
            return(this.out_act);
        }
Example #4
0
        public Vol Forward(Vol V, bool is_training)
        {
            this.in_act = V;
            var V2  = V.Clone();
            var N   = V.w.Length;
            var V2w = V2.w;

            for (var i = 0; i < N; i++)
            {
                if (V2w[i] < 0)
                {
                    V2w[i] = 0;                            // threshold at 0
                }
            }
            this.out_act = V2;
            return(this.out_act);
        }
Example #5
0
        void Update()
        {
            for (var i = 0; i < iteration; ++i)
            {
                foreach (Vector3 v in data)
                {
                    trainer.Train(new Vol(new float[] { v.x, v.y }), v.z);
                }
            }

            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                var pos = ray.origin;
                pos  /= 10.0f;
                pos.z = 0;
                data.Add(pos);
            }

            if (Input.GetMouseButtonDown(1))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                var pos = ray.origin;
                pos  /= 10.0f;
                pos.z = 1;
                data.Add(pos);
            }

            for (var i = 0; i < pixels.GetLength(0); ++i)
            {
                for (var j = 0; j < pixels.GetLength(1); ++j)
                {
                    Vol   action = net.Forward(new Vol(new float[] { i / 10f, j / 10f }));
                    float output = action.w[0];
                    if (output > 0.5)
                    {
                        pixels [i, j].GetComponent <MeshRenderer> ().material.color = new Color(1, 0, 0);
                    }
                    else
                    {
                        pixels [i, j].GetComponent <MeshRenderer> ().material.color = new Color(0, 0, 1);
                    }
                }
            }
        }
Example #6
0
        public FullyConnLayer(int depth, int numOfOutput, float bias_pref = 0.0f)
        {
            this.out_depth    = numOfOutput;
            this.num_inputs   = 1 * 1 * depth;
            this.out_sx       = 1;
            this.out_sy       = 1;
            this.l1_decay_mul = 0.0f;
            this.l2_decay_mul = 1.0f;

            var bias = bias_pref;

            this.filters = new Vol[this.out_depth];
            for (var i = 0; i < this.out_depth; i++)
            {
                filters [i] = new Vol(1, 1, this.num_inputs);
            }
            this.biases = new Vol(1, 1, this.out_depth, bias);
        }
Example #7
0
        public Vol Forward(Vol V, bool is_training)
        {
            this.in_act = V;
            var A  = new Vol(1, 1, this.out_depth, 0.0f);
            var Vw = V.w;

            for (var i = 0; i < this.out_depth; i++)
            {
                var a  = 0.0f;
                var wi = this.filters[i].w;
                for (var d = 0; d < this.num_inputs; d++)
                {
                    a += Vw[d] * wi[d];
                }
                a     += this.biases.w[i];
                A.w[i] = a;
            }
            this.out_act = A;
            return(this.out_act);
        }
Example #8
0
        public void Train(Vol x, object y)
        {
            net.Forward(x, true);
            var cost_loss = net.Backward(y);
            //Debug.Log ("loss:" + cost_loss);

            var l2_decay_loss = 0.0f;
            var l1_decay_loss = 0.0f;

            List <ParamsAndGrads> pglist = new List <ParamsAndGrads> ();

            net.GetParamsAndGrads(pglist);

            for (var i = 0; i < pglist.Count; i++)
            {
                var pg = pglist[i];
                var p  = pg.param;
                var g  = pg.grads;

                var l2_decay_mul = pg.l2_decay_mul;
                var l1_decay_mul = pg.l1_decay_mul;
                var l2_decay     = this.l2_decay * l2_decay_mul;
                var l1_decay     = this.l1_decay * l1_decay_mul;

                var plen = p.Length;
                for (var j = 0; j < plen; j++)
                {
                    l2_decay_loss += l2_decay * p[j] * p[j] / 2;
                    l1_decay_loss += l1_decay * Math.Abs(p[j]);
                    var l1grad = l1_decay * (p[j] > 0 ? 1 : -1);
                    var l2grad = l2_decay * (p[j]);
                    var gij    = (l2grad + l1grad + g [j]);
                    //p[j] +=  - this.learning_rate * gij;
                    p[j] += -this.learning_rate * g[j];

                    // 記得要歸0
                    g[j] = 0.0f;
                }
            }
        }
Example #9
0
        public Vol Forward(Vol V, bool is_training = false)
        {
            var act = this.layers[0].Forward(V, is_training);

            for (var i = 1; i < this.layers.Count; i++)
            {
                /*
                 * for (var j = 0; j < act.w.Length; ++j) {
                 *      Debug.Log (act.w [j]);
                 * }
                 * Debug.Log ("======="+i+"========");
                 */
                act = this.layers[i].Forward(act, is_training);
            }

            /*
             * for (var j = 0; j < act.w.Length; ++j) {
             *      Debug.Log (act.w [j]);
             * }
             * Debug.Log ("======="+(this.layers.Count)+"========");
             */
            return(act);
        }
Example #10
0
 public Vol Forward(Vol V, bool is_training)
 {
     this.in_act  = V;
     this.out_act = V;
     return(this.out_act);
 }