Example #1
0
        private Tensor ApplyRegularizer(TVar loss)
        {
            foreach (var l in Layers)
            {
                foreach (var p in l.Params)
                {
                    if (p.Value.HaveRegularizer)
                    {
                        loss += p.Value.ApplyRegularizer();
                    }
                }
            }

            return(loss.Evaluate());
        }
Example #2
0
        public static TVar Softmax(this TVar x)
        {
            long[]       shape = x.Evaluate().Shape;
            List <float> data  = new List <float>();

            for (long i = 0; i < shape[0]; i++)
            {
                var s_x = x.Select(0, i);
                var exp = s_x.Exp();
                var sum = exp.SumAll();
                var s_t = (exp / sum.ToScalar()).View(1, shape[1]).Evaluate();
                data.AddRange(s_t.ToArray().Cast <float>());
            }

            return(TVar.FromArray(data.ToArray(), Global.Device).View(shape));
        }