最近我开始使用 mlpack,我按照本教程构建了我的第一个神经网络。我生成训练和测试数据只是为了让事情顺利进行。
到目前为止,一切正常:
//LOADING DATA
mat trainData;
data::Load("training_data.csv", trainData);
mat testData;
data::Load("testing_data.csv", testData);
//Split data into target\label and the features
mat trainTarget = trainData.row(trainData.n_rows - 1);
trainData.shed_row(trainData.n_rows - 1);
mat testTarget = testData.row(testData.n_rows - 1);
testData.shed_row(testData.n_rows - 1);
//BUILD MODEL
FFN<> model;
model.Add<Linear<> >(trainData.n_rows, 8);
model.Add<SigmoidLayer<> >();
model.Add<Linear<> >(8, 2);
model.Add<Softmax<> >();
我添加训练线的那一刻:
model.Train(trainData, trainTarget);
它不会编译并返回奇怪的错误:
我上传了培训和测试文件,所以你可以自己尝试一下,如果我有问题,请告诉我。
完整代码:
#include "mlpack/core.hpp"
#include "mlpack/methods/ann/layer/layer.hpp"
#include "mlpack/methods/ann/ffn.hpp"
using namespace std;
using namespace arma;
using namespace mlpack;
using namespace mlpack::ann;
int main()
{
//LOADING DATA
mat trainData;
data::Load("training_data.csv", trainData);
mat testData;
data::Load("testing_data.csv", testData);
//Split data into target\label and the features
mat trainTarget = trainData.row(trainData.n_rows - 1);
trainData.shed_row(trainData.n_rows - 1);
mat testTarget = testData.row(testData.n_rows - 1);
testData.shed_row(testData.n_rows - 1);
//BUILD MODEL
FFN<> model;
model.Add<Linear<> >(trainData.n_rows, 8);
model.Add<SigmoidLayer<> >();
model.Add<Linear<> >(8, 2);
model.Add<Softmax<> >();
model.Train(trainData, trainTarget);
mat targetprediction;
model.Predict(testData, targetprediction);
uword predictedTarget = targetprediction.index_max() + 1;
cout << "Test Target: " << testTarget << " Target Prediction: " << predictedTarget << endl;
return 0;
}