Registered Member
|
I am a newer with eigen, and I implement a logistic regression model with eigen. It works but I don't know whether it is implemented in a efficient way or is there any operation in my implementation is low efficient and could get some improvement.
|
Moderator
|
In all function, use const reference to pass vectors and matrices, e.g.:
void train(const MatrixXd &train_datas, double lr) In train, no need to introduce tmp: w += lr * (train_datas.leftCols(this->w.rows()) * dw.asDiagonal()).colwise().mean(); In predict: .array() > 0.5 -> .eval() > 0.5 ; array() is redundant here, and better evaluate the subexpression to fully benefits from vectorization (bench to be sure this really improve perf.) In linear_separable_dataset_generator: datas.rightCols(1) -> datas.col(datas.cols()-1) so that Eigen knowns this is a vector. |
Registered Member
|
Thanks a lot! I think using const reference will improve the performance a lot bucause it doesn't have to do the assignments , but after I tried several times with -O0 shuting down the optimizer, I found this way does not improve the performance. Isn't it weird? |
Registered Member
|
As for what you said about predict function: .array() > 0.5 -> .eval() > 0.5 ; This seems cannot work bucause there is no > operation for a matrix like this.
|
Moderator
|
1 - Benchmarking without compiler optimizations is meaningless. You need at lest to enabled -O2 or -O3.
2 - Yes, Matrix>Scalar is illegal, but in your case, the expression "(1 / (1 + ((-inputs*this->w).array() - b).exp()))" is already an array. In doubt, you can still do .array().eval(). |
Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot]