I understand … in that case I will attach my code to improve comprehension, my intention is to apply to the function computeGradient (which is a gradient made by hand) of the following expression:
∇wNLL(y|X,w) =Σxi(µi −yi) = X^T(µ−y)
Were: µ is wx^T
T : trasnpose
a gradient with autodiff reverse-mode in order to optimize it since the one that programs is a bit slow.
I read the paragraph of the functional gradient of the paper but I can not understand how to use it with my code
I would appreciate any help, thank you very much
my code (CPU_logistic_regressionhpp.txt (424 Bytes)
logistic_regression.txt (3.5 KB)
logistic_regressionhpp.txt (1.4 KB)
I add the other files)
VectorXd CPU_LogisticRegression::train(int n_iter,double alpha,double tol){
VectorXd log_likelihood=VectorXd::Zero(n_iter);
for(int i=0;i<n_iter;i++){
tools.printProgBar(i, n_iter);
this->preCompute();
log_likelihood(i)=-this->logPosterior();
VectorXd gradient=this->computeGradient();
this->weights-=alphagradient;
if(this->with_bias) this->bias-=alphathis->grad_bias;
}
cout << endl;
return log_likelihood;
}
void CPU_LogisticRegression::preCompute(){
this->eta = (*this->X_train * this->weights);
if(this->with_bias) this->eta.noalias()=(this->eta.array()+this->bias).matrix();
this->phi = this->sigmoid(this->eta);
}
////////this part I need to perform the autodif
VectorXd CPU_LogisticRegression::computeGradient(){
VectorXd E_d=this->phi-*this->Y_train;
VectorXd E_w=this->weights/(this->lambda);
VectorXd grad=VectorXd::Zero(this->dim);
for(int d=0;ddim;d++){
grad[d]=this->X_train->col(d).cwiseProduct(E_d).sum()+E_w[d];
}
if(this->with_bias) this->grad_bias= (E_d.sum()+this->bias/this->lambda);
return grad;
}
VectorXd CPU_LogisticRegression::predict(MatrixXd &_X_test,bool prob, bool data_processing){
if (data_processing){
if (this->normalization) tools.testNormalization(_X_test,this->featureMax,this->featureMin);
if(this->standardization) tools.testStandardization(_X_test,this->featureMean,this->featureStd);
}
VectorXd eta_test = (_X_test)*this->weights;
if(this->with_bias) eta_test.noalias()=(eta_test.array()+this->bias).matrix();
VectorXd phi_test=this->sigmoid(eta_test);
if(!prob){
phi_test.noalias() = phi_test.unaryExpr([](double elem){
return (elem > 0.5) ? 1.0 : 0.0;
});
}
return phi_test;
}