IVector _Derivative(IVector th, float lambda)
 {
     using (var p0 = _feature.Multiply(th))
         using (var p1 = p0.Column(0))
             using (var p = p1.Sigmoid())
                 using (var e0 = p.Subtract(_target))
                     using (var e = e0.ToRowMatrix())
                         using (var e2 = e.Multiply(_feature)) {
                             e2.Multiply(1f / _feature.RowCount);
                             var ret = e2.Row(0);
                             if (lambda != 0f)
                             {
                                 var reg = new float[th.Count];
                                 using (var thi = th.AsIndexable()) {
                                     var term = lambda / _feature.RowCount;
                                     for (var i = 1; i < th.Count; i++)
                                     {
                                         reg[i] = thi[i] * term;
                                     }
                                     using (var regVector = _lap.CreateVector(reg))
                                         ret.Add(regVector);
                                 }
                             }
                             return(ret);
                         }
 }
        public float ComputeCost(IVector theta, float lambda)
        {
            var regularisationCost = theta.AsIndexable().Values.Skip(1).Sum(v => v * v * lambda);
            var h    = _feature.Multiply(theta);
            var diff = _target.Subtract(h.Column(0));

            diff.Add(regularisationCost);
            return(diff.PointwiseMultiply(diff).AsIndexable().Values.Sum() / (2 * _feature.RowCount));
        }
Beispiel #3
0
        I4DTensor _FormIntoTensor(ILinearAlgebraProvider lap, IVector vector, I4DTensor tensor)
        {
            var indexableVector = vector.AsIndexable();
            var tensorList      = new List <I3DTensor>();

            for (var i = 0; i < tensor.Count; i++)
            {
                var matrixList = new List <IMatrix>();
                for (var j = 0; j < tensor.Depth; j++)
                {
                    matrixList.Add(lap.CreateMatrix(tensor.RowCount, tensor.ColumnCount, indexableVector[j]));
                }
                tensorList.Add(lap.Create3DTensor(matrixList));
            }
            return(lap.Create4DTensor(tensorList));
        }
 public float ComputeCost(IVector th, float lambda)
 {
     using (var h0 = _feature.Multiply(th))
         using (var h1 = h0.Column(0))
             using (var h = h1.Sigmoid())
                 using (var hLog = h.Log())
                     using (var t = _target.Clone()) {
                         var a = _target.DotProduct(hLog);
                         t.Multiply(-1f);
                         t.Add(1f);
                         h.Multiply(-1f);
                         h.Add(1f);
                         var b   = t.DotProduct(hLog);
                         var ret = -(a + b) / _feature.RowCount;
                         if (lambda != 0f)
                         {
                             ret += th.AsIndexable().Values.Skip(1).Select(v => v * v).Sum() * lambda / (2 * _feature.RowCount);
                         }
                         return(ret);
                     }
 }
Beispiel #5
0
        public void PointwiseDivideColumns(IVector vector)
        {
            var v2 = vector.AsIndexable();

            _matrix.MapIndexedInplace((x, y, v) => v /= v2[y]);
        }