32 #ifndef SHARK_OBJECTIVEFUNCTIONS_NEGATIVE_LOG_LIKELIHOOD_H 33 #define SHARK_OBJECTIVEFUNCTIONS_NEGATIVE_LOG_LIKELIHOOD_H 39 #include <boost/range/algorithm_ext/iota.hpp> 40 #include <boost/range/algorithm/random_shuffle.hpp> 61 DatasetType
const& data,
63 ):mep_model(model),m_data(data){
71 {
return "NegativeLogLikelihood"; }
87 double minProb = 1e-100;
89 RealMatrix predictions = (*mep_model)(m_data.
batch(i));
91 double logLikelihoodOfSamples = sum(log(max(predictions,minProb)));
93 error += logLikelihoodOfSamples;
106 derivative.resize(input.size());
114 std::size_t batchesPerThread = numBatches/numThreads;
115 std::size_t leftOver = numBatches - batchesPerThread*numThreads;
117 double minProb = 1e-100;
121 std::size_t start = t*batchesPerThread+std::min(t,leftOver);
122 std::size_t end = (t+1)*batchesPerThread+std::min(t+1,leftOver);
126 double threadError = 0;
127 boost::shared_ptr<State> state = mep_model->
createState();
128 RealVector batchDerivative;
129 RealMatrix predictions;
130 for(std::size_t i = start; i != end; ++i){
131 mep_model->
eval(m_data.
batch(i),predictions,*state);
133 threadError += sum(log(max(predictions,minProb)));
134 RealMatrix coeffs(predictions.size1(),predictions.size2(),0.0);
136 for(std::size_t j = 0; j != predictions.size1(); ++j){
137 for(std::size_t k = 0; k != predictions.size2(); ++k){
138 if(predictions(j,k) >= minProb){
139 coeffs(j,k) = 1.0/predictions(j,k);
144 m_data.
batch(i),predictions, coeffs,*state,batchDerivative
146 threadDerivative += batchDerivative;
151 error += threadError;
152 noalias(derivative) += threadDerivative;
156 error /= numElements;
157 derivative /= numElements;