Compute the KL-divergence of a set of marginals
| 212 | |
| 213 | // Compute the KL-divergence of a set of marginals |
| 214 | double DenseCRF::klDivergence( const MatrixXf & Q ) const { |
| 215 | double kl = 0; |
| 216 | // Add the entropy term |
| 217 | for( int i=0; i<Q.cols(); i++ ) |
| 218 | for( int l=0; l<Q.rows(); l++ ) |
| 219 | kl += Q(l,i)*log(std::max( Q(l,i), 1e-20f) ); |
| 220 | // Add the unary term |
| 221 | if( unary_ ) { |
| 222 | MatrixXf unary = unary_->get(); |
| 223 | for( int i=0; i<Q.cols(); i++ ) |
| 224 | for( int l=0; l<Q.rows(); l++ ) |
| 225 | kl += unary(l,i)*Q(l,i); |
| 226 | } |
| 227 | |
| 228 | // Add all pairwise terms |
| 229 | MatrixXf tmp; |
| 230 | for( unsigned int k=0; k<pairwise_.size(); k++ ) { |
| 231 | pairwise_[k]->apply( tmp, Q ); |
| 232 | kl += (Q.array()*tmp.array()).sum(); |
| 233 | } |
| 234 | return kl; |
| 235 | } |
| 236 | |
| 237 | // Gradient computations |
| 238 | double DenseCRF::gradient( int n_iterations, const ObjectiveFunction & objective, VectorXf * unary_grad, VectorXf * lbl_cmp_grad, VectorXf * kernel_grad) const { |