// Gradient computations double DenseCRF::gradient( int n_iterations, const ObjectiveFunction & objective, VectorXf * unary_grad, VectorXf * lbl_cmp_grad, VectorXf * kernel_grad) const { // Run inference std::vector< MatrixXf > Q(n_iterations+1); MatrixXf tmp1, unary( M_, N_ ), tmp2; unary.fill(0); if( unary_ ) unary = unary_->get(); expAndNormalize( Q[0], -unary ); for( int it=0; it<n_iterations; it++ ) { tmp1 = -unary; for( unsigned int k=0; k<pairwise_.size(); k++ ) { pairwise_[k]->apply( tmp2, Q[it] ); tmp1 -= tmp2; } expAndNormalize( Q[it+1], tmp1 ); } // Compute the objective value MatrixXf b( M_, N_ ); double r = objective.evaluate( b, Q[n_iterations] ); sumAndNormalize( b, b, Q[n_iterations] ); // Compute the gradient if(unary_grad && unary_) *unary_grad = unary_->gradient( b ); if( lbl_cmp_grad ) *lbl_cmp_grad = 0*labelCompatibilityParameters(); if( kernel_grad ) *kernel_grad = 0*kernelParameters(); for( int it=n_iterations-1; it>=0; it-- ) { // Do the inverse message passing tmp1.fill(0); int ip = 0, ik = 0; // Add up all pairwise potentials for( unsigned int k=0; k<pairwise_.size(); k++ ) { // Compute the pairwise gradient expression if( lbl_cmp_grad ) { VectorXf pg = pairwise_[k]->gradient( b, Q[it] ); lbl_cmp_grad->segment( ip, pg.rows() ) += pg; ip += pg.rows(); } // Compute the kernel gradient expression if( kernel_grad ) { VectorXf pg = pairwise_[k]->kernelGradient( b, Q[it] ); kernel_grad->segment( ik, pg.rows() ) += pg; ik += pg.rows(); } // Compute the new b pairwise_[k]->applyTranspose( tmp2, b ); tmp1 += tmp2; } sumAndNormalize( b, tmp1.array()*Q[it].array(), Q[it] ); // Add the gradient if(unary_grad && unary_) *unary_grad += unary_->gradient( b ); } return r; }
void testSaxpyAndSquarenormEvenOdd(const LatticeExtents lE, const hmc_complex cN) { SaxpyAndSquarenormEvenOddTestParameters parametersForThisTest{lE, cN}; hardware::HardwareParametersMockup hardwareParameters(parametersForThisTest.ns, parametersForThisTest.nt, true); hardware::code::OpenClKernelParametersMockupForMergedSpinorKernels kernelParameters(parametersForThisTest.ns, parametersForThisTest.nt); ParameterCollection parameterCollection{hardwareParameters, kernelParameters}; SaxpyAndSquarenormEvenOddTester(parameterCollection, parametersForThisTest); }