Calling Eigen library to do the linear operation of the matrix












0












$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;
}









share|improve this question









New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 2




    $begingroup$
    Welcome to Code Review. Please tell us more about the task that this code performs. (MathJax is available for mathematical notation.) See How to Ask.
    $endgroup$
    – 200_success
    3 hours ago










  • $begingroup$
    Also, the current title is very generic. Unless your code also is very generic, please change your title to be more specific. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    $endgroup$
    – Zeta
    58 mins ago
















0












$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;
}









share|improve this question









New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 2




    $begingroup$
    Welcome to Code Review. Please tell us more about the task that this code performs. (MathJax is available for mathematical notation.) See How to Ask.
    $endgroup$
    – 200_success
    3 hours ago










  • $begingroup$
    Also, the current title is very generic. Unless your code also is very generic, please change your title to be more specific. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    $endgroup$
    – Zeta
    58 mins ago














0












0








0





$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;
}









share|improve this question









New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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






share|improve this question









New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 24 mins ago









Jamal

30.3k11116226




30.3k11116226






New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









CharryzzzCharryzzz

1




1




New contributor




Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Charryzzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    $begingroup$
    Welcome to Code Review. Please tell us more about the task that this code performs. (MathJax is available for mathematical notation.) See How to Ask.
    $endgroup$
    – 200_success
    3 hours ago










  • $begingroup$
    Also, the current title is very generic. Unless your code also is very generic, please change your title to be more specific. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    $endgroup$
    – Zeta
    58 mins ago














  • 2




    $begingroup$
    Welcome to Code Review. Please tell us more about the task that this code performs. (MathJax is available for mathematical notation.) See How to Ask.
    $endgroup$
    – 200_success
    3 hours ago










  • $begingroup$
    Also, the current title is very generic. Unless your code also is very generic, please change your title to be more specific. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    $endgroup$
    – Zeta
    58 mins ago








2




2




$begingroup$
Welcome to Code Review. Please tell us more about the task that this code performs. (MathJax is available for mathematical notation.) See How to Ask.
$endgroup$
– 200_success
3 hours ago




$begingroup$
Welcome to Code Review. Please tell us more about the task that this code performs. (MathJax is available for mathematical notation.) See How to Ask.
$endgroup$
– 200_success
3 hours ago












$begingroup$
Also, the current title is very generic. Unless your code also is very generic, please change your title to be more specific. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
$endgroup$
– Zeta
58 mins ago




$begingroup$
Also, the current title is very generic. Unless your code also is very generic, please change your title to be more specific. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
$endgroup$
– Zeta
58 mins ago










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.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211652%2fcalling-eigen-library-to-do-the-linear-operation-of-the-matrix%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.










draft saved

draft discarded


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211652%2fcalling-eigen-library-to-do-the-linear-operation-of-the-matrix%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python