36 #ifndef SHARK_ALGORITHMS_TRAINERS_LOGISTICREGRESSION_H 37 #define SHARK_ALGORITHMS_TRAINERS_LOGISTICREGRESSION_H 63 template <
class InputVectorType = RealVector>
88 {
return "LogisticRegression"; }
125 return {m_lambda1,m_lambda2};
141 void train(ModelType& model, DatasetType
const& dataset){
142 optimize(model, dataset);
146 void train(ModelType& model, WeightedDatasetType
const& dataset){
147 optimize(model, dataset);
152 template<
class DatasetT>
153 void optimize(ModelType& model, DatasetT
const& dataset){
156 if(numOutputs == 2) numOutputs = 1;
157 auto& innerModel = model.decisionFunction();
158 innerModel.setStructure(
inputDimension(dataset),numOutputs, m_bias);
159 std::size_t dim = innerModel.numberOfParameters();
160 innerModel.setParameterVector(RealVector(dim,0.0));
171 RealVector mask(dim,1.0);
172 subrange(mask,dim - numOutputs, dim).clear();
182 optimizer.
init(error);
184 while(norm_inf(optimizer.
derivative()) > m_accuracy){
185 optimizer.
step(error);
188 if(norm_sqr(optimizer.
solution().
point - lastPoint) == 0)
break;
191 model.setParameterVector(lastPoint);
199 L1Reformulation
function(&error, m_lambda1, dim - m_bias * numOutputs);
202 optimizer.init(
function);
203 RealVector lastPoint = optimizer.solution().point;
207 auto const& derivative = optimizer.derivative();
208 for(std::size_t i = 0; i != lastPoint.size(); ++i){
209 if(lastPoint(i) < 1.e-13 && -derivative(i) > m_accuracy){
212 }
else if(lastPoint(i) > 1.e-13 && std::abs(derivative(i)) > m_accuracy){
222 optimizer.step(
function);
225 if(norm_sqr(optimizer.solution().point - lastPoint) == 0)
break;
226 noalias(lastPoint) = optimizer.solution().point;
229 std::size_t n = dim - m_bias * numOutputs;
231 RealVector param = (subrange(lastPoint,0,n) - subrange(lastPoint,n, 2 * n)) | subrange(lastPoint,2*n,lastPoint.size());
232 model.setParameterVector(param);
244 : mep_error(error), m_lambda1(lambda1), m_regularizedParams(regularizedParams){
245 m_features |= CAN_PROPOSE_STARTING_POINT;
246 m_features |= HAS_FIRST_DERIVATIVE;
248 std::size_t dim = numberOfVariables();
249 double unconstrained = 1e100;
250 RealVector lower(dim,0.0);
251 subrange(lower, 2 * m_regularizedParams,dim) = blas::repeat(-unconstrained,dim - 2 * m_regularizedParams);
252 RealVector upper(dim,unconstrained);
253 m_handler.setBounds(lower,upper);
254 announceConstraintHandler(&m_handler);
257 SearchPointType proposeStartingPoint()
const {
258 return RealVector(numberOfVariables(),0.0);
261 std::size_t numberOfVariables()
const{
262 return mep_error->numberOfVariables() + m_regularizedParams;
265 double eval(RealVector
const& input)
const{
266 std::size_t dim = input.size();
267 std::size_t n = m_regularizedParams;
268 RealVector params = (subrange(input,0,n) - subrange(input,n, 2 * n)) | subrange(input,2*n,dim);
269 return mep_error->eval(params) + m_lambda1 * sum(subrange(input,0,2*n));
271 ResultType evalDerivative(
const SearchPointType & input, FirstOrderDerivative & derivative )
const{
272 std::size_t dim = input.size();
273 std::size_t n = m_regularizedParams;
274 RealVector params = (subrange(input,0,n) - subrange(input,n, 2 * n)) | subrange(input,2*n,dim);
275 FirstOrderDerivative paramDerivative;
276 double error = mep_error->
evalDerivative(params, paramDerivative);
277 derivative.resize(numberOfVariables());
278 noalias(subrange(derivative,0,n)) = m_lambda1 + subrange(paramDerivative,0,n);
279 noalias(subrange(derivative,n,2 * n)) = m_lambda1 - subrange(paramDerivative,0,n);
280 noalias(subrange(derivative,2 * n,dim)) = subrange(paramDerivative,n,dim - n);
281 return error + m_lambda1 * sum(subrange(input,0,2*n));
288 std::size_t m_regularizedParams;