Improving the efficiency of c++ code calling Eigen to do the linear operation of the matrix
$begingroup$
I am a rookie of coding and I have written a c++ procedure which calls the Eigen library to do the linear operation of the matrix. Please help me to improve the efficiency of the loop.
#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include <time.h>
using namespace std;
int main()
{
// Weight coefficient matrix
Eigen::MatrixXd wi_1,wi_2,wi_3,wi_4;
wi_1.resize(100,2);
wi_2.resize(100,100);
wi_3.resize(100,100);
wi_4.resize(5,100);
wi_1.setOnes();
wi_2.setOnes();
wi_3.setOnes();
wi_4.setOnes();
// Bias vector
Eigen::VectorXd bias_1,bias_2,bias_3,bias_4,Y;
bias_1.resize(100);
bias_2.resize(100);
bias_3.resize(100);
bias_4.resize(5);
bias_1.setOnes();
bias_2.setOnes();
bias_3.setOnes();
bias_4.setOnes();
Eigen::Matrix<double,5,1> y_mean;
Eigen::Matrix<double,5,1> y_scale;
Eigen::Matrix<double,2,1> x_mean;
Eigen::Matrix<double,2,1> x_scale;
y_mean.setOnes();
y_scale.setOnes();
y_mean.setOnes();
x_scale.setOnes();
int n = 0;
int layer;
clock_t start,finish;
double totaltime;
start=clock();
while (n<10000)
{
Y.resize(2);
layer = 0;
Y << 0.185, 0.285;//inputx[1], x[0];
Y = (Y.array() - x_mean.array()) / x_scale.array();
//ANN forward
while (layer < 4)
{
layer++;
switch (layer) {
case 1:{
Y = wi_1 * Y + bias_1;
// Info << "ANN forward layer1" << endl;
break;
}
case 2:{
Y = wi_2 * Y + bias_2;
// Info << "ANN forward layer2" << endl;
break;
}
case 3:{
Y = wi_3 * Y + bias_3;
// Info << "ANN forward layer3" << endl;
break;
}
case 4:{
Y = wi_4 * Y + bias_4;
// Info << "ANN forward layer4" << endl;
break;
}
default:{
cout<<"error"<<endl;
break;
}
}
//Relu activation function
if (layer < 4)
{
for (int i = 0; i < Y.size(); i++)
{
Y(i) = ((Y(i) > 0) ? Y(i) : 0);
}
}
}
//inverse standardization
Y = Y.array() * y_scale.array() + y_mean.array();
n++;
}
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC*1000;
cout<<"n Running time is "<<totaltime<<"ms!"<<endl;
}
c++ eigen
New contributor
$endgroup$
add a comment |
$begingroup$
I am a rookie of coding and I have written a c++ procedure which calls the Eigen library to do the linear operation of the matrix. Please help me to improve the efficiency of the loop.
#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include <time.h>
using namespace std;
int main()
{
// Weight coefficient matrix
Eigen::MatrixXd wi_1,wi_2,wi_3,wi_4;
wi_1.resize(100,2);
wi_2.resize(100,100);
wi_3.resize(100,100);
wi_4.resize(5,100);
wi_1.setOnes();
wi_2.setOnes();
wi_3.setOnes();
wi_4.setOnes();
// Bias vector
Eigen::VectorXd bias_1,bias_2,bias_3,bias_4,Y;
bias_1.resize(100);
bias_2.resize(100);
bias_3.resize(100);
bias_4.resize(5);
bias_1.setOnes();
bias_2.setOnes();
bias_3.setOnes();
bias_4.setOnes();
Eigen::Matrix<double,5,1> y_mean;
Eigen::Matrix<double,5,1> y_scale;
Eigen::Matrix<double,2,1> x_mean;
Eigen::Matrix<double,2,1> x_scale;
y_mean.setOnes();
y_scale.setOnes();
y_mean.setOnes();
x_scale.setOnes();
int n = 0;
int layer;
clock_t start,finish;
double totaltime;
start=clock();
while (n<10000)
{
Y.resize(2);
layer = 0;
Y << 0.185, 0.285;//inputx[1], x[0];
Y = (Y.array() - x_mean.array()) / x_scale.array();
//ANN forward
while (layer < 4)
{
layer++;
switch (layer) {
case 1:{
Y = wi_1 * Y + bias_1;
// Info << "ANN forward layer1" << endl;
break;
}
case 2:{
Y = wi_2 * Y + bias_2;
// Info << "ANN forward layer2" << endl;
break;
}
case 3:{
Y = wi_3 * Y + bias_3;
// Info << "ANN forward layer3" << endl;
break;
}
case 4:{
Y = wi_4 * Y + bias_4;
// Info << "ANN forward layer4" << endl;
break;
}
default:{
cout<<"error"<<endl;
break;
}
}
//Relu activation function
if (layer < 4)
{
for (int i = 0; i < Y.size(); i++)
{
Y(i) = ((Y(i) > 0) ? Y(i) : 0);
}
}
}
//inverse standardization
Y = Y.array() * y_scale.array() + y_mean.array();
n++;
}
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC*1000;
cout<<"n Running time is "<<totaltime<<"ms!"<<endl;
}
c++ eigen
New contributor
$endgroup$
add a comment |
$begingroup$
I am a rookie of coding and I have written a c++ procedure which calls the Eigen library to do the linear operation of the matrix. Please help me to improve the efficiency of the loop.
#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include <time.h>
using namespace std;
int main()
{
// Weight coefficient matrix
Eigen::MatrixXd wi_1,wi_2,wi_3,wi_4;
wi_1.resize(100,2);
wi_2.resize(100,100);
wi_3.resize(100,100);
wi_4.resize(5,100);
wi_1.setOnes();
wi_2.setOnes();
wi_3.setOnes();
wi_4.setOnes();
// Bias vector
Eigen::VectorXd bias_1,bias_2,bias_3,bias_4,Y;
bias_1.resize(100);
bias_2.resize(100);
bias_3.resize(100);
bias_4.resize(5);
bias_1.setOnes();
bias_2.setOnes();
bias_3.setOnes();
bias_4.setOnes();
Eigen::Matrix<double,5,1> y_mean;
Eigen::Matrix<double,5,1> y_scale;
Eigen::Matrix<double,2,1> x_mean;
Eigen::Matrix<double,2,1> x_scale;
y_mean.setOnes();
y_scale.setOnes();
y_mean.setOnes();
x_scale.setOnes();
int n = 0;
int layer;
clock_t start,finish;
double totaltime;
start=clock();
while (n<10000)
{
Y.resize(2);
layer = 0;
Y << 0.185, 0.285;//inputx[1], x[0];
Y = (Y.array() - x_mean.array()) / x_scale.array();
//ANN forward
while (layer < 4)
{
layer++;
switch (layer) {
case 1:{
Y = wi_1 * Y + bias_1;
// Info << "ANN forward layer1" << endl;
break;
}
case 2:{
Y = wi_2 * Y + bias_2;
// Info << "ANN forward layer2" << endl;
break;
}
case 3:{
Y = wi_3 * Y + bias_3;
// Info << "ANN forward layer3" << endl;
break;
}
case 4:{
Y = wi_4 * Y + bias_4;
// Info << "ANN forward layer4" << endl;
break;
}
default:{
cout<<"error"<<endl;
break;
}
}
//Relu activation function
if (layer < 4)
{
for (int i = 0; i < Y.size(); i++)
{
Y(i) = ((Y(i) > 0) ? Y(i) : 0);
}
}
}
//inverse standardization
Y = Y.array() * y_scale.array() + y_mean.array();
n++;
}
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC*1000;
cout<<"n Running time is "<<totaltime<<"ms!"<<endl;
}
c++ eigen
New contributor
$endgroup$
I am a rookie of coding and I have written a c++ procedure which calls the Eigen library to do the linear operation of the matrix. Please help me to improve the efficiency of the loop.
#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include <time.h>
using namespace std;
int main()
{
// Weight coefficient matrix
Eigen::MatrixXd wi_1,wi_2,wi_3,wi_4;
wi_1.resize(100,2);
wi_2.resize(100,100);
wi_3.resize(100,100);
wi_4.resize(5,100);
wi_1.setOnes();
wi_2.setOnes();
wi_3.setOnes();
wi_4.setOnes();
// Bias vector
Eigen::VectorXd bias_1,bias_2,bias_3,bias_4,Y;
bias_1.resize(100);
bias_2.resize(100);
bias_3.resize(100);
bias_4.resize(5);
bias_1.setOnes();
bias_2.setOnes();
bias_3.setOnes();
bias_4.setOnes();
Eigen::Matrix<double,5,1> y_mean;
Eigen::Matrix<double,5,1> y_scale;
Eigen::Matrix<double,2,1> x_mean;
Eigen::Matrix<double,2,1> x_scale;
y_mean.setOnes();
y_scale.setOnes();
y_mean.setOnes();
x_scale.setOnes();
int n = 0;
int layer;
clock_t start,finish;
double totaltime;
start=clock();
while (n<10000)
{
Y.resize(2);
layer = 0;
Y << 0.185, 0.285;//inputx[1], x[0];
Y = (Y.array() - x_mean.array()) / x_scale.array();
//ANN forward
while (layer < 4)
{
layer++;
switch (layer) {
case 1:{
Y = wi_1 * Y + bias_1;
// Info << "ANN forward layer1" << endl;
break;
}
case 2:{
Y = wi_2 * Y + bias_2;
// Info << "ANN forward layer2" << endl;
break;
}
case 3:{
Y = wi_3 * Y + bias_3;
// Info << "ANN forward layer3" << endl;
break;
}
case 4:{
Y = wi_4 * Y + bias_4;
// Info << "ANN forward layer4" << endl;
break;
}
default:{
cout<<"error"<<endl;
break;
}
}
//Relu activation function
if (layer < 4)
{
for (int i = 0; i < Y.size(); i++)
{
Y(i) = ((Y(i) > 0) ? Y(i) : 0);
}
}
}
//inverse standardization
Y = Y.array() * y_scale.array() + y_mean.array();
n++;
}
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC*1000;
cout<<"n Running time is "<<totaltime<<"ms!"<<endl;
}
c++ eigen
c++ eigen
New contributor
New contributor
New contributor
asked 2 mins ago
CharryzzzCharryzzz
1
1
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Charryzzz is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211652%2fimproving-the-efficiency-of-c-code-calling-eigen-to-do-the-linear-operation-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Charryzzz is a new contributor. Be nice, and check out our Code of Conduct.
Charryzzz is a new contributor. Be nice, and check out our Code of Conduct.
Charryzzz is a new contributor. Be nice, and check out our Code of Conduct.
Charryzzz is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211652%2fimproving-the-efficiency-of-c-code-calling-eigen-to-do-the-linear-operation-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown