I am trying to learn how to use mlpack::ann::FFN<> class. The default parameter for output layer type is mlpack::ann::NegativeLogLikelihood<> as per documentation but I want to use mlpack::ann::MeanSquaredError<> as I don't now former yet... when I use MSE I get compiler error saying that MeanSquaredError<> is not a part of mlpack::ann namespace but when I check the source code of mean_squared_error.hpp I can clearly see that it is defined in mlpack::ann namespace ( same in the documentation too ). What's happening here? When I compile using NegativeLogLikelihood the program compiles although there is an error at the runtime saying Mat::operator() index out of bounds
Here is the error :
brightprogrammer@titan:~/Desktop$ g++ test.cpp -std=c++17 -lmlpack -lboost_serialization -lboost_unit_test_framework -lboost_program_options -larmadillo -fopenmp -I/usr/include/ -I/usr/include/stb -o test && ./test ~/Datasets/IRIS.csv
In file included from /usr/include/mlpack/core.hpp:67,
from test.cpp:3:
/usr/include/mlpack/prereqs.hpp:20:17: note: #pragma message: Armadillo was included before mlpack; this can sometimes cause problems. It should only be necessary to include <mlpack/core.hpp> and not <armadillo>.
20 | #pragma message "Armadillo was included before mlpack; this can sometimes cause\
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 | problems. It should only be necessary to include <mlpack/core.hpp> and not \
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 | <armadillo>."
| ~~~~~~~~~~~~~
test.cpp: In function ‘int main(int, char**)’:
test.cpp:71:32: error: ‘MeanSquaredError’ is not a member of ‘mlpack::ann’
71 | mlpack::ann::FFN<mlpack::ann::MeanSquaredError<>, mlpack::ann::RandomInitialization> model;
| ^~~~~~~~~~~~~~~~
test.cpp:71:49: error: template argument 1 is invalid
71 | mlpack::ann::FFN<mlpack::ann::MeanSquaredError<>, mlpack::ann::RandomInitialization> model;
| ^
test.cpp:71:50: error: expected unqualified-id before ‘,’ token
71 | mlpack::ann::FFN<mlpack::ann::MeanSquaredError<>, mlpack::ann::RandomInitialization> model;
| ^
test.cpp:71:85: error: qualified-id in declaration before ‘>’ token
71 | mlpack::ann::FFN<mlpack::ann::MeanSquaredError<>, mlpack::ann::RandomInitialization> model;
| ^
test.cpp:72:2: error: ‘model’ was not declared in this scope; did you mean ‘modfl’?
72 | model.Add<mlpack::ann::Linear<>>(4, 10);
| ^~~~~
| modfl
test.cpp:72:32: error: expected primary-expression before ‘>’ token
72 | model.Add<mlpack::ann::Linear<>>(4, 10);
| ^~
test.cpp:73:35: error: expected primary-expression before ‘>’ token
73 | model.Add<mlpack::ann::ReLULayer<>>();
| ^~
test.cpp:73:38: error: expected primary-expression before ‘)’ token
73 | model.Add<mlpack::ann::ReLULayer<>>();
| ^
test.cpp:74:32: error: expected primary-expression before ‘>’ token
74 | model.Add<mlpack::ann::Linear<>>(10, 3);
| ^~
test.cpp:75:38: error: expected primary-expression before ‘>’ token
75 | model.Add<mlpack::ann::SigmoidLayer<>>();
| ^~
test.cpp:75:41: error: expected primary-expression before ‘)’ token
75 | model.Add<mlpack::ann::SigmoidLayer<>>();
| ^
brightprogrammer@titan:~/Desktop$
Here is my code :
```#include
bool check_number(std::string str) {
uint dec=0;
for (int i = 0; i < str.length(); i++){
if(std::isdigit(str[i])==false){
if(str[i]=='.' && dec<=1) dec++;
else return false;
}
}
return true;
}
std::vector
std::ifstream file(filename);
std::string line, cell;
std::vector
getline(file,line); //we don't need first line so just skip it from being evaluated
while(getline(file, line)){
std::stringstream ss(line);
while(getline(ss, cell, ',')){
if(check_number(cell)==false) parsed_vec.push_back(cell);
}
}
return parsed_vec;
}
int main(int argc, char** argv){
arma::mat trainX, trainY, rawData;
mlpack::data::Load(argv[1], rawData, false, true);
//set trainX and trainY
trainX = rawData.submat(0, 1, rawData.n_rows-2, rawData.n_cols-1);
trainY.resize(3, trainX.n_cols);
trainY.zeros();
std::vector<std::string> labels = getLabels(argv[1]);
std::vector<std::string> classes;
for(uint i=0; i<labels.size(); i++){
if(classes.size()==0) classes.push_back(labels[i]);
//get unique labels (classes)
bool hasLabel = false;
uint pos=0;
for(uint j=0; j<classes.size(); j++){
if(classes[j] == labels[i]){
hasLabel=true;
pos=j;
}
}
if(hasLabel) trainY(pos, i) = 1;
else{
classes.push_back(labels[i]);
trainY.resize(classes.size(), trainX.n_cols);
trainY.row(classes.size()-1).zeros();
trainY(classes.size()-1, i) = 1;
}
}
mlpack::ann::FFN<mlpack::ann::MeanSquaredError<>, mlpack::ann::RandomInitialization> model;
model.Add<mlpack::ann::Linear<>>(4, 10);
model.Add<mlpack::ann::ReLULayer<>>();
model.Add<mlpack::ann::Linear<>>(10, 3);
model.Add<mlpack::ann::SigmoidLayer<>>();
ens::Adam optimizer(0.005, 3, 0.9, 0.999, 1e-8, 0, 1e-8, true);
model.Train(trainX, trainY, optimizer, ens::PrintLoss(), ens::ProgressBar(), ens::EarlyStopAtMinLoss());
}```
The same problem is there for mlpack::ann::CrossEntropyError<>
I think you will have to include
#include <mlpack/methods/ann/loss_functions/mean_squared_error.hpp>
or any loss function for that matter since it's not there in layer.hpp
Thank you so much @mrityunjay-tripathi this comment can't really express how happy I am after so many failures!
@brightprogrammer Glad that it helped :)
Most helpful comment
I think you will have to include
or any loss function for that matter since it's not there in
layer.hpp