Beispiel #1
0
        void nextStep()
        {
            // compute jacobian
            if (_retry == 0)
            {
                _jac = _jac_func(_inputs, _weights);
            }
            // compute hessian approximation with tykhonov damping coefficient
            var jacT = new NRealMatrix(_jac.Rows, _jac.Columns);

            jacT.SetArray(_jac.ToArray());
            jacT.Transpose();
            var dampedHessian = new NRealMatrix(_jac.Columns, _jac.Columns);

            dampedHessian = jacT * _jac;
            for (int idxRow = 0; idxRow < dampedHessian.Rows; idxRow++)
            {
                dampedHessian.SetAt(idxRow, idxRow, new NDouble(dampedHessian[idxRow, idxRow] * (1.0 + _lambda) + 1e-10));
            }
            var adj = new NRealMatrix(dampedHessian.Rows, 1);
            var y   = new NRealMatrix(dampedHessian.Rows, 1);

            y = jacT * _error;
            // solve dampedHessian * adj = y
            LapackLib.Instance.SolveSle(dampedHessian, y, adj);
            var nextWeights = new NRealMatrix(1, _weights.Columns);

            for (int idxWeight = 0; idxWeight < nextWeights.Columns; idxWeight++)
            {
                nextWeights.SetAt(0, idxWeight, new NDouble(_weights[0, idxWeight] - adj[idxWeight, 0]));
            }
            // compute errors
            var error      = calcError(nextWeights);
            var totalError = calcTotalError(error);

            if (totalError > _totalError)
            {
                // revert step and increase damping factor
                if (_retry < 100)
                {
                    _lambda *= 11.0;
                    _retry++;
                }
                else
                {
                    updateWeights();
                    throw new StallException();
                }
            }
            else
            {
                // accept step and decrease damping factor
                _lambda /= 9.0;
                _weights.SetArray(nextWeights.ToArray());
                _error      = error;
                _totalError = totalError;
                _retry      = 0;
            }
        }
Beispiel #2
0
 void nextStep()
 {
     // compute jacobian
     if (_retry == 0)
         _jac = _jac_func(_inputs, _weights);
     // compute hessian approximation with tykhonov damping coefficient
     var jacT = new NRealMatrix(_jac.Rows, _jac.Columns);
     jacT.SetArray(_jac.ToArray());
     jacT.Transpose();
     var dampedHessian = new NRealMatrix(_jac.Columns, _jac.Columns);
     dampedHessian = jacT * _jac;
     for (int idxRow = 0; idxRow < dampedHessian.Rows; idxRow++)
         dampedHessian.SetAt(idxRow, idxRow, new NDouble(dampedHessian[idxRow, idxRow] * (1.0 + _lambda) + 1e-10));
     var adj = new NRealMatrix(dampedHessian.Rows, 1);
     var y = new NRealMatrix(dampedHessian.Rows, 1);
     y = jacT * _error;
     // solve dampedHessian * adj = y
     LapackLib.Instance.SolveSle(dampedHessian, y, adj);
     var nextWeights = new NRealMatrix(1, _weights.Columns);
     for (int idxWeight = 0; idxWeight < nextWeights.Columns; idxWeight++)
         nextWeights.SetAt(0, idxWeight, new NDouble(_weights[0, idxWeight] - adj[idxWeight, 0]));
     // compute errors
     var error = calcError(nextWeights);
     var totalError = calcTotalError(error);
     if (totalError > _totalError)
     {
         // revert step and increase damping factor
         if (_retry < 100)
         {
             _lambda *= 11.0;
             _retry++;
         }
         else
         {
             updateWeights();
             throw new StallException();
         }
     }
     else
     {
         // accept step and decrease damping factor
         _lambda /= 9.0;
         _weights.SetArray(nextWeights.ToArray());
         _error = error;
         _totalError = totalError;
         _retry = 0;
     }
 }