building a multilayer perceptron

closed account (E093605o)
Hello,

I want to implement a simple multilayer perceptron for digit recognition. I have started to read the c++ primer 5th edition book 2 days ago so I am not that familiar with c++. Below you see my code for the MLP.h file. I want to set the length of the weights and biases vector in the constructor to be equal to the int provided. Can somebody tell me why my approach is wrong and maybe what I need to understand to do it right? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MLP
#define MLP
#include <vector>
#include <eigen3/Eigen/Core>
using namespace std;

class MLP{
    private:
    vector<Eigen::Matrix<double,9,9>> weights;
    vector<Eigen::Matrix<double,9,1>> biases;
    double output;
    double squaredError(vector<double> output, vector<double> target);
    public:
    void feedforward(vector<double> x);
    void backprop(vector<double> x, double y);
    MLP(const int layers) : weights(layers,Eigen::Matrix<double,9,9>){}
    
};
#endif 
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MLPdefs                        // <==== don't call it the same name as the class
#define MLPdefs                        // <==== 
#include <vector>
#include <Eigen/Core>                  // <==== depends on your setup (mine is different from yours)
using namespace std;

class MLP
{
private:
   vector<Eigen::Matrix<double,9,9>> weights;
   vector<Eigen::Matrix<double,9,1>> biases;
   double output;
   double squaredError(vector<double> output, vector<double> target);
public:
   void feedforward(vector<double> x);
   void backprop(vector<double> x, double y);
   MLP( int layers ) : weights(layers,Eigen::Matrix<double,9,9>()){}          // <==== note the extra ()
};
#endif  

closed account (E093605o)
thanks for your answer, unfortunately this does not work. Any ideas why?
did you by chance get an error message?
closed account (E093605o)
sure, sorry I should have posted it. It says "this declaration has no storage class or type specifier" for MLP in line 17 and "a nonstatic member reference must be relative to a specific object" for weights in line 17.
Well I compiled it using g++ with the line
g++ -I /eigen-3.4.0/ -Wall -pedantic -Wextra -c test.cpp
and it didn't throw up any errors. I also tested using the -std=c++11 option, and it still worked with even that much older standard.

You obviously have to adjust the include directory in the compile command, and possibly the #include statement for Eigen in the code itself, to suit your own Eigen setup. Other than that, I have no idea how you got the errors that you cite.

Are you sure that you tried to compile precisely that code and didn't change it in any way?
Last edited on
closed account (E093605o)
yes I just copy pasted the code. However, I installed eigen via this guide https://installati.one/ubuntu/22.04/libeigen3-dev/ might it be the case that I need to change something additionally to merely installing it? I used the first option - apt package manager.
Sorry, but I can see absolutely no way of getting that error message with anything like my code.
closed account (E093605o)
ok thanks for your efforts!
@dvdlly,
I am well aware that the code you provided is a header file, but I would ask you to copy my code exactly into a .cpp file and try to compile it as-is. (The only thing that might need changing in your set-up is the #include path to the Eigen code - nothing else.)

I have seen error messages like those you cite in other contexts - but I see no way of them applying to the constructor of your class as you state here.

FWIW I tested my code on Windows, not linux, but I doubt that that would affect the issue. Perhaps somebody else may be in a better position to test.
Topic archived. No new replies allowed.