converting Matlab Neural Network into C++ Neural Network - c++

I created a Neural Network in Matlab with newff, for handwritten Digits recognition.
I just trained it to recognize only 0 & 1 values from images.
with 3 Layers, Input Layer has 9 Neurons, Hidden Layer has 5 Neurons, and output Layer 1 Neuron,and there is 9 inputs.
my out puts are 0.1 & 0.2 ,and all Layers outputs function are "tansig".
I test it in Matlab and Network works Fine. now I want to create this network in c++ , I Wrote the Code and I copied all the Weights and Biasses(total 146 weights).
but when I put the same input data to Network the output value is not correct.
can anyone of you guys guide me?
this is my networks code:
here's my networks code...
public class Neuron
{
public Neuron()
{ }
public Neuron(int SumOfInputs)
{
m_SumOfInputs = SumOfInputs;
}
public double act(double[] Input, double[] weight, double bias)
{
double tmp = bias;
for (int i = 0; i < m_SumOfInputs; i++)
tmp += (Input[i] * weight[i]);
m_output = 1.0 / (1.0 + Math.Exp(-tmp));
return m_output;
}
public double m_output;
private int m_SumOfInputs;
};
public class Net
{
public Net()
{
int i;
//net1 , net2
//initializing inputLayer Neurons
for (i = 0; i < 9; i++)
InputLayer[i] = new Neuron(9);
//initializing HiddenLayer Neurons
for (i = 0; i < 5; i++)
HiddenLayer[i] = new Neuron(9);
//initializing OutputLayer
OutputLayer = new Neuron(5);
}
public double Calculate(double[] inputs)
{
double[] ILay_Outputs = new double[9];
double[] HLay_Outputs = new double[5];
//inputLayer acting
ILay_Outputs[0] = InputLayer[0].act(inputs, IW1, Ib[0]);
ILay_Outputs[1] = InputLayer[1].act(inputs, IW2, Ib[1]);
ILay_Outputs[2] = InputLayer[2].act(inputs, IW3, Ib[2]);
ILay_Outputs[3] = InputLayer[3].act(inputs, IW4, Ib[3]);
ILay_Outputs[4] = InputLayer[4].act(inputs, IW5, Ib[4]);
ILay_Outputs[5] = InputLayer[5].act(inputs, IW6, Ib[5]);
ILay_Outputs[6] = InputLayer[6].act(inputs, IW7, Ib[6]);
ILay_Outputs[7] = InputLayer[7].act(inputs, IW8, Ib[7]);
ILay_Outputs[8] = InputLayer[8].act(inputs, IW9, Ib[8]);
//HiddenLayer acting
HLay_Outputs[0] = HiddenLayer[0].act(ILay_Outputs, HW1, Hb[0]);
HLay_Outputs[1] = HiddenLayer[1].act(ILay_Outputs, HW2, Hb[1]);
HLay_Outputs[2] = HiddenLayer[2].act(ILay_Outputs, HW3, Hb[2]);
HLay_Outputs[3] = HiddenLayer[3].act(ILay_Outputs, HW4, Hb[3]);
HLay_Outputs[4] = HiddenLayer[4].act(ILay_Outputs, HW5, Hb[4]);
//OutputLayer acting
OutputLayer.act(HLay_Outputs, OW, Ob);
return OutputLayer.m_output;
}
//variables
Neuron[] InputLayer = new Neuron[9];
Neuron[] HiddenLayer = new Neuron[5];
Neuron OutputLayer;
//net2 tansig tansig tansig
double[] IW1 = { 0.726312035124743, 1.01034015912570, 0.507178716484559, -0.254689455765290, 0.475299816659036, 0.0336358919735363, -0.715890843015230, 0.466632424349648, 0.565406467159982 };
double[] IW2 = { 0.866482591050076, -0.672473224929341, 0.915599891389326, 0.310163265280920, -0.373812653648686, -0.0859927887021936, 0.0100063635393257, 0.816638798257382, -0.540771172965867 };
double[] IW3 = { 0.138868216294952, 1.93121321568871, -0.564704445249800, 0.834275586326333, 3.08348295981989, 0.899715248285303, -0.661916798988641, 6.00562393127300, 6.11939776912678 };
double[] IW4 = { 0.578089791487308, 0.885170493965113, -0.992514702569606, 0.415980526304333, -0.706140252063166, 0.442017877881589, -0.449053823645690, -0.0894051386719344, -0.348622179369911 };
double[] IW5 = { -0.407756482945129, 0.0786764402198765, 0.972408690276837, -0.959955597431701, -0.977769442966978, 1.52121267506016, 0.503296357838885, -3.31593633455649, -3.47834004737816 };
double[] IW6 = { -1.17474983226852, 0.870140308892922, 1.50545637070446, 0.369712493398677, -0.569857993006262, -0.732502911495791, -0.668984976457441, -1.48023312055586, -0.893472571240467 };
double[] IW7 = { -0.860518592120001, -1.48432158859269, 0.957060799463945, -0.680797771869510, -0.270752283410268, -0.218766920514208, 0.168091770241510, -2.50326075864844, -0.800988078966455 };
double[] IW8 = { 0.436492138260917, 0.280081066366966, 0.484813099857825, -0.310693876078844, 1.60359045377467, 1.57343220231689, -1.21552190886612, 2.03276547165735, 1.27245062411707 };
double[] IW9 = { 1.66853306274827, -1.59142022586958, 0.862315766588855, 0.676048095028997, -2.22623540036057, -1.48036066273542, -0.0386781503608105, -5.18214728910353, -5.21258509200432 };
double[] HW1 = { 0.577543862468449, 0.452264642610010, -0.869014797322399, 0.122435296258077, 0.507631314535324, 0.0386430216115630, -0.398222802253669, -0.614601040619812, 1.43324133164016 };
double[] HW2 = { 0.163344332215885, 0.434728230081814, -3.04877964757120, -0.118300732191499, -2.63220585865390, 0.443163977179405, -2.11883915836372, 2.07955461474729, -3.94441429060856 };
double[] HW3 = { -0.156103043064606, -0.482049683802527, 1.24788068138172, -1.05731056687422, -0.615321348655331, 0.214815967784408, 0.375762477817552, -0.728649292060764, -0.212151944122515 };
double[] HW4 = { 1.78276088127139, 1.15086535250306, 1.25967219208841, -0.446026243031773, -3.94742837475153, -1.33311929047378, -2.09356929069216, 0.0736879745054291, 1.51472991137144 };
double[] HW5 = { 0.744372844550077, 0.400815326319268, -4.94686055701529, 0.444773365537176, 2.65351865321717, 1.87143709824455, 1.74346707204902, -3.28220218001754, 5.78321274609173 };
double[] OW = { -1.09112204235009, -7.13508015318964, -1.02533926874837, 3.80439015418632, -4.16711367340349 };
double[] Ib = {-1.77988445077976,
-1.37323967952292,
-0.547465218997906,
0.331535304175263,
-0.0167810612906040,
0.734128501831859,
-0.543321122358485,
-1.13525462762255,
1.82870615182942};
double[] Hb = {1.68321697741393,
-0.862080862212137,
-0.536310792063381,
-0.772019935790668,
1.51470472867250};
double Ob = -0.156343477742835;
};
thanks.
Arta.

You mention in your description that you want to use the Tansig activation function, but in your code you have the implementation for the Logsig activation function. Tansig approximation would be:
2/(1+Math.Exp(-2*tmp))-1
I am also not sure how you get the weights for the input layer, are these perhaps the weights for the hidden layer. Matlab does not generate weights for the input layers since the inputs are directly connected to the hidden layer. Where net.IW are the weights for the first (hidden) layer, the weights for the subsequent layers (including output) are given by net.LW.
Besides the above I don't see obvious bugs/errors in your code, maybe try a simpler network first and train it to do the old and wise XOR relationship.
Lastly I would like to mention if you are writing this code for a micro-controller it's easier to do it in C and without objects. Your code will be smaller and faster. A step by step example is given here.

I found the problem guys.
in matlab, before inputs goes to the network, they all goes to a function names (applyminmax) in a .m file names (mapminmax.m), and then this function outputs are the network inputs.
after the simulation on network is done, the outputs goes to a function names (reverse) in the same .m file.
and this function outputs is the final output of the Neural Network.
thanks for all your helps.
Arta.

Related

How do i perform operations on a structures field?

I im currently having an issue with when performing an operation with my structure.
On form load, I am loading a comma delimited file with 6 indexes then using a 2d array i display the structured list's field to a grid view cell.
In the last for loop, perform the operation using my total field?
{
public partial class Form1 : Form
{
public struct Costomers
{
public string firstN;
public string lastN;
public string address;
public string item;
public double price;
public int quantity;
public double total;
}
public Form1()
{
InitializeComponent();
}
List<Costomers> consumers = new List<Costomers>();
private void Form1_Load(object sender, EventArgs e)
{
var inputfile = File.ReadAllLines("customers.txt");
for (int i = 0; i < inputfile.Length; i++)
{
var SplitArr = inputfile[i].Split(',');
// next declare a new instace of costomers
Costomers ConsumerInfo = new Costomers();
// the ConsumerInfo object now contains all of the fields i declared in the structer.
// next i want to access the Objects fields and assign them
ConsumerInfo.firstN = SplitArr[0];
ConsumerInfo.lastN = SplitArr[1];
ConsumerInfo.address = SplitArr[2];
ConsumerInfo.item = SplitArr[3];
ConsumerInfo.price = Convert.ToDouble(SplitArr[4]);
ConsumerInfo.quantity = Convert.ToInt32(SplitArr[5]);
//ConsumerInfo.total = consumers[i].price * consumers[i].quantity;
consumers.Add(ConsumerInfo);
}
// Next i need to disply each field in the grid view
// i will resue my code from section F to do this.
for (int i = 0; i < consumers.Count; i++)
{
dgInfo.Rows.Add();
dgInfo[0, i].Value = consumers[i].firstN;
dgInfo[1, i].Value = consumers[i].lastN;
dgInfo[2, i].Value = consumers[i].address;
dgInfo[3, i].Value = consumers[i].item;
dgInfo[4, i].Value = consumers[i].price;
dgInfo[5, i].Value = consumers[i].quantity;
}
// next in a seperat for loop i need too mutiply the price by the quantity
for (int i = 0; i < consumers.Count; i++)
{
Costomers totalPrice = new Costomers();
totalPrice.total = consumers[i].price * consumers[i].quantity;
dgInfo[6,i].Value = totalPrice.total;
}
}
}
}```
Second, because the file i load does not contain a index for total; how to i display the total in its own column?
Text file as shown below:
The, Batman, 123 Gotham Drive, bat belt, 193.82,17
The, Joker, 12432 Joker Way, Bat Spray, 19.99, 1022
Cat, Women, 8787 Meow St., Kibbles, 9.99, 4700
The, Penguin, 17 Waddel Ave., pointy cigarettes, 24.99, 51700
sidekick, Robin, 123 Gotham Drive, junior bat belt, 67.80, 10000
Adam, West, 1782 Hollywood Dr., hasbeen kit, 10305018.18, 1

MRPT Graph Slam Minimal Example

I am trying to come up with a "minimal" way of running a graph slam application using MRPT. The sensor data (LaserScan / Odometry) will be provided by a custom middleware similiar to ROS. After reading docs and source codes (both for the MRPT and the ROS bridge) extensively, I came up with the following snippet:
std::string config_file = "../../../laser_odometry.ini";
std::string rawlog_fname = "";
std::string fname_GT = "";
auto node_reg = mrpt::graphslam::deciders::CICPCriteriaNRD<mrpt::graphs::CNetworkOfPoses2DInf>{};
auto edge_reg = mrpt::graphslam::deciders::CICPCriteriaERD<mrpt::graphs::CNetworkOfPoses2DInf>{};
auto optimizer = mrpt::graphslam::optimizers::CLevMarqGSO<mrpt::graphs::CNetworkOfPoses2DInf>{};
auto win3d = mrpt::gui::CDisplayWindow3D{"Slam", 800, 600};
auto win_observer = mrpt::graphslam::CWindowObserver{};
auto win_manager = mrpt::graphslam::CWindowManager{&win3d, &win_observer};
auto engine = mrpt::graphslam::CGraphSlamEngine<mrpt::graphs::CNetworkOfPoses2DInf>{
config_file, rawlog_fname, fname_GT, &win_manager, &node_reg, &edge_reg, &optimizer};
for (size_t measurement_count = 0;;) {
// grab laser scan from the network, then fill it (hardcoded values for now), e.g:
auto scan_ptr = mrpt::obs::CObservation2DRangeScan::Create();
scan_ptr->timestamp = std::chrono::system_clock::now().time_since_epoch().count();
scan_ptr->rightToLeft = true;
scan_ptr->sensorLabel = "";
scan_ptr->aperture = 3.14; // rad (max-min)
scan_ptr->maxRange = 3.0; // m
scan_ptr->sensorPose = mrpt::poses::CPose3D{};
scan_ptr->resizeScan(30);
for (int i = 0; i < 30; ++i) {
scan_ptr->setScanRange(i, 0.5);
scan_ptr->setScanRangeValidity(i, true);
}
{ // Send LaserScan measurement to the slam engine
auto obs_ptr = std::dynamic_pointer_cast<mrpt::obs::CObservation>(scan_ptr);
engine.execGraphSlamStep(obs_ptr, measurement_count);
++measurement_count;
}
// grab odometry from the network, then fill it (hardcoded values for now), e.g:
auto odometry_ptr = mrpt::obs::CObservationOdometry::Create();
odometry_ptr->timestamp = std::chrono::system_clock::now().time_since_epoch().count();
odometry_ptr->hasVelocities = false;
odometry_ptr->odometry.x(0);
odometry_ptr->odometry.y(0);
odometry_ptr->odometry.phi(0);
{ // Send Odometry measurement to the slam engine
auto obs_ptr = std::dynamic_pointer_cast<mrpt::obs::CObservation>(odometry_ptr);
engine.execGraphSlamStep(obs_ptr, measurement_count);
++measurement_count;
}
// Get pose estimation from the engine
auto pose = engine.getCurrentRobotPosEstimation();
}
Am I in the right direction here? Did I miss something?
Hmm, at a first look the script seems fine, you are providing odometry and the laser scan in two different steps and in Observation form.
Minor note
auto node_reg = mrpt::graphslam::deciders::CICPCriteriaNRD{};
If you want to run with Odometry + laser scans use CFixedIntervalsNRD instead. It's much better tested and actually makes use of those measurements.
There is no minimal graphslam-engine example at present in MRPT but here's here's the main method for running graph-slam with datasets:
https://github.com/MRPT/mrpt/blob/26ee0f2d3a9366c50faa5f78d0388476ae886808/libs/graphslam/include/mrpt/graphslam/apps_related/CGraphSlamHandler_impl.h#L395
template <class GRAPH_T>
void CGraphSlamHandler<GRAPH_T>::execute()
{
using namespace mrpt::obs;
ASSERTDEB_(m_engine);
// Variables initialization
mrpt::io::CFileGZInputStream rawlog_stream(m_rawlog_fname);
CActionCollection::Ptr action;
CSensoryFrame::Ptr observations;
CObservation::Ptr observation;
size_t curr_rawlog_entry;
auto arch = mrpt::serialization::archiveFrom(rawlog_stream);
// Read the dataset and pass the measurements to CGraphSlamEngine
bool cont_exec = true;
while (CRawlog::getActionObservationPairOrObservation(
arch, action, observations, observation, curr_rawlog_entry) &&
cont_exec)
{
// actual call to the graphSLAM execution method
// Exit if user pressed C-c
cont_exec = m_engine->_execGraphSlamStep(
action, observations, observation, curr_rawlog_entry);
}
m_logger->logFmt(mrpt::system::LVL_WARN, "Finished graphslam execution.");
}
You basically grab the data and then continuously feed them to CGraphSlamEngine via either execGraphSlamStep or _execGraphSlamStep methods.
Here's also the relevant snippet for processing measurements in the corresponding ROS wrapper that operates with measurements from ROS topics:
https://github.com/mrpt-ros-pkg/mrpt_slam/blob/8b32136e2a381b1759eb12458b4adba65e2335da/mrpt_graphslam_2d/include/mrpt_graphslam_2d/CGraphSlamHandler_ROS_impl.h#L719
template<class GRAPH_T>
void CGraphSlamHandler_ROS<GRAPH_T>::processObservation(
mrpt::obs::CObservation::Ptr& observ) {
this->_process(observ);
}
template<class GRAPH_T>
void CGraphSlamHandler_ROS<GRAPH_T>::_process(
mrpt::obs::CObservation::Ptr& observ) {
using namespace mrpt::utils;
if (!this->m_engine->isPaused()) {
this->m_engine->execGraphSlamStep(observ, m_measurement_cnt);
m_measurement_cnt++;
}
}

Unknown reason for "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" exception

I am making a Unity game where the player is collecting data about aliens.
Therefor the player points on the alien and uses something like a camera.
Camera --> shoots Ray --> Ray returns all needed data attached to the script on the alien-gameobject
void ShootRay()
{
RaycastHit hitInfo; // stores information about hitted object
if (Physics.Raycast (transform.position, transform.forward, out hitInfo, maxRaycastRange, 1 << LayerMask.NameToLayer("creature"))) // out hitInfo = Unity puts information in the variable hitInfo
{
// UI alerts and collecting dna
if (hitInfo.distance <= photoRaycastRange)
{
distanceInfo.text = "scanning_genome";
if (hitInfo.collider.gameObject.GetComponent<EnemyAI> ().dna_collected == false) {
if (dna_percent_0_to_1 < 1)
{
calming_dna_scan_circle = false;
distanceInfo.text = "scanning_genome";
dna_percent_0_to_1 += Time.deltaTime * dna_scanSpeed;
dna_collect_circle.fillAmount = dna_percent_0_to_1;
}
else if (dna_percent_0_to_1 >= 1)
{
// adding info of creature to database
if (hitInfo.collider.gameObject.GetComponent<EnemyAI> ().raceIndex == 1)
{
if (!raceOneWasAdded)
{
BestiariumData.scannedSpecies.Add (hitInfo.collider.gameObject);
raceOneWasAdded = true;
}
BestiariumData.dnaBar_1 += 0.25f;
The mentioned database is simply a class called BestiariumData with:
public static List<GameObject> scannedSpecies = new List<GameObject> ();
public static List<float> savedDNAFillRates = new List<float> ();
public static float dnaBar_1 = 0;
public static float dnaBar_2 = 0;
public static float dnaBar_3 = 0;
public static float dnaBar_4 = 0;
public static float dnaBar_5 = 0;
public static float dnaBar_6 = 0;
public static float dnaBar_7 = 0;
public static float dnaBar_8 = 0;
}
I'm having a menu where the player can check which aliens he/she already has collected data. The name of the alien is displayed (Monster One, ...) and a progress bar for how many alien individuals the player has scanned.
THE PROBLEM:
if I try to assign the NAME of the status bar if throws the ArgumentOutOfRangeException: Argument is out of range. Parameter name: index exception. I am doing this by setting a bool in another script to true.
public List<GameObject> monsterButtons = new List<GameObject>();
public static bool nameButtons = false;
// Update is called once per frame
void LateUpdate ()
{
if (nameButtons)
{
for (int buttonIndex = monsterButtons.Count; buttonIndex > 0; buttonIndex--)
{
monsterButtons [buttonIndex].GetComponentInChildren<Text> ().text = BestiariumData.scannedSpecies [buttonIndex].name;
}
}
}
Thank you for your help.
Button index gives the count of your list. So say your list contains 10 items, count will be 10.
However a list's index starts at 0, not 1.
So when you try to access monsterButtons [buttonIndex] for the first time, you are calling index 10, which means item 11. This does not exist so throws your error.
To fix, add "-1" to your index asigning:
for (int buttonIndex = monsterButtons.Count -1; buttonIndex >= 0; buttonIndex--)
{
monsterButtons [buttonIndex].GetComponentInChildren<Text> ().text = BestiariumData.scannedSpecies [buttonIndex].name;
}

How to train in Matlab a model, save it to disk, and load in C++ program?

I am using libsvm version 3.16. I have done some training in Matlab, and created a model. Now I would like to save this model to disk and load this model in my C++ program. So far I have found the following alternatives:
This answer explains how to save a model from C++, which is based on this website. Not exactly what I need, but could be adapted. (This requires development time).
I could find the best training parameters (kernel,C) in Matlab and re-train everything in C++. (Will require doing the training in C++ each time I change a parameter. It's not scalable).
Thus, both of these options are not satisfactory,
Does anyone have an idea?
My solution was to retrain in C++ because I couldn't find a nice way to directly save the model. Here's my code. You'll need to adapt it and clean it up a bit. The biggest change you'll have to make it not hard coding the svm_parameter values like I did. You'll also have to replace FilePath with std::string. I'm copying, pasting and making small edits here in SO so the formatting won't e perfect:
Used like this:
auto targetsPath = FilePath("targets.txt");
auto observationsPath = FilePath("observations.txt");
auto targetsMat = MatlabMatrixFileReader::Read(targetsPath, ',');
auto observationsMat = MatlabMatrixFileReader::Read(observationsPath, ',');
auto v = MiscVector::ConvertVecOfVecToVec(targetsMat);
auto model = SupportVectorRegressionModel{ observationsMat, v };
std::vector<double> observation{ { // 32 feature observation
0.883575729725847,0.919446119013878,0.95359403450317,
0.968233630936732,0.91891307107125,0.887897763183844,
0.937588566544751,0.920582702918882,0.888864454119387,
0.890066735260163,0.87911085669864,0.903745573664995,
0.861069296586979,0.838606194934074,0.856376230548304,
0.863011311537075,0.807688936997926,0.740434984165146,
0.738498042748759,0.736410940165691,0.697228384912424,
0.608527698289016,0.632994967880269,0.66935784966765,
0.647761430696238,0.745961037635717,0.560761134660957,
0.545498063585615,0.590854855113663,0.486827902942118,
0.187128866890822,- 0.0746523069562551
} };
double prediction = model.Predict(observation);
miscvector.h
static vector<double> ConvertVecOfVecToVec(const vector<vector<double>> &mat)
{
vector<double> targetsVec;
targetsVec.reserve(mat.size());
for (size_t i = 0; i < mat.size(); i++)
{
targetsVec.push_back(mat[i][0]);
}
return targetsVec;
}
libsvmtargetobjectconvertor.h
#pragma once
#include "machinelearning.h"
struct svm_node;
class LibSvmTargetObservationConvertor
{
public:
svm_node ** LibSvmTargetObservationConvertor::ConvertObservations(const vector<MlObservation> &observations, size_t numFeatures) const
{
svm_node **svmObservations = (svm_node **)malloc(sizeof(svm_node *) * observations.size());
for (size_t rowI = 0; rowI < observations.size(); rowI++)
{
svm_node *row = (svm_node *)malloc(sizeof(svm_node) * numFeatures);
for (size_t colI = 0; colI < numFeatures; colI++)
{
row[colI].index = colI;
row[colI].value = observations[rowI][colI];
}
row[numFeatures].index = -1; // apparently needed
svmObservations[rowI] = row;
}
return svmObservations;
}
svm_node* LibSvmTargetObservationConvertor::ConvertMatToSvmNode(const MlObservation &observation) const
{
size_t numFeatures = observation.size();
svm_node *obsNode = (svm_node *)malloc(sizeof(svm_node) * numFeatures);
for (size_t rowI = 0; rowI < numFeatures; rowI++)
{
obsNode[rowI].index = rowI;
obsNode[rowI].value = observation[rowI];
}
obsNode[numFeatures].index = -1; // apparently needed
return obsNode;
}
};
machinelearning.h
#pragma once
#include <vector>
using std::vector;
using MlObservation = vector<double>;
using MlTarget = double;
//machinelearningmodel.h
#pragma once
#include <vector>
#include "machinelearning.h"
class MachineLearningModel
{
public:
virtual ~MachineLearningModel() {}
virtual double Predict(const MlObservation &observation) const = 0;
};
matlabmatrixfilereader.h
#pragma once
#include <vector>
using std::vector;
class FilePath;
// Matrix created with command:
// dlmwrite('my_matrix.txt', somematrix, 'delimiter', ',', 'precision', 15);
// In these files, each row is a matrix row. Commas separate elements on a row.
// There is no space at the end of a row. There is a blank line at the bottom of the file.
// File format:
// 0.4,0.7,0.8
// 0.9,0.3,0.5
// etc.
static class MatlabMatrixFileReader
{
public:
static vector<vector<double>> Read(const FilePath &asciiFilePath, char delimiter)
{
vector<vector<double>> values;
vector<double> valueline;
std::ifstream fin(asciiFilePath.Path());
string item, line;
while (getline(fin, line))
{
std::istringstream in(line);
while (getline(in, item, delimiter))
{
valueline.push_back(atof(item.c_str()));
}
values.push_back(valueline);
valueline.clear();
}
fin.close();
return values;
}
};
supportvectorregressionmodel.h
#pragma once
#include <vector>
using std::vector;
#include "machinelearningmodel.h"
#include "svm.h" // libsvm
class FilePath;
class SupportVectorRegressionModel : public MachineLearningModel
{
public:
SupportVectorRegressionModel::~SupportVectorRegressionModel()
{
svm_free_model_content(model_);
svm_destroy_param(&param_);
svm_free_and_destroy_model(&model_);
}
SupportVectorRegressionModel::SupportVectorRegressionModel(const vector<MlObservation>& observations, const vector<MlTarget>& targets)
{
// assumes all observations have same number of features
size_t numFeatures = observations[0].size();
//setup targets
//auto v = ConvertVecOfVecToVec(targetsMat);
double *targetsPtr = const_cast<double *>(&targets[0]); // why aren't the targets const?
LibSvmTargetObservationConvertor conv;
svm_node **observationsPtr = conv.ConvertObservations(observations, numFeatures);
// setup observations
//svm_node **observations = BuildObservations(observationsMat, numFeatures);
// setup problem
svm_problem problem;
problem.l = targets.size();
problem.y = targetsPtr;
problem.x = observationsPtr;
// specific to out training sets
// TODO: This is hard coded.
// Bust out these values for use in constructor
param_.C = 0.4; // cost
param_.svm_type = 4; // SVR
param_.kernel_type = 2; // radial
param_.nu = 0.6; // SVR nu
// These values are the defaults used in the Matlab version
// as found in svm_model_matlab.c
param_.gamma = 1.0 / (double)numFeatures;
param_.coef0 = 0;
param_.cache_size = 100; // in MB
param_.shrinking = 1;
param_.probability = 0;
param_.degree = 3;
param_.eps = 1e-3;
param_.p = 0.1;
param_.shrinking = 1;
param_.probability = 0;
param_.nr_weight = 0;
param_.weight_label = NULL;
param_.weight = NULL;
// suppress command line output
svm_set_print_string_function([](auto c) {});
model_ = svm_train(&problem, &param_);
}
double SupportVectorRegressionModel::Predict(const vector<double>& observation) const
{
LibSvmTargetObservationConvertor conv;
svm_node *obsNode = conv.ConvertMatToSvmNode(observation);
double prediction = svm_predict(model_, obsNode);
return prediction;
}
SupportVectorRegressionModel::SupportVectorRegressionModel(const FilePath & modelFile)
{
model_ = svm_load_model(modelFile.Path().c_str());
}
private:
svm_model *model_;
svm_parameter param_;
};
Option 1 is actually pretty reasonable. If you save the model in libsvm's C format through matlab, then it is straightforward to work with the model in C/C++ using functions provided by libsvm. Trying to work with matlab-formatted data in C++ will probably be much more difficult.
The main function in "svm-predict.c" (located in the root directory of the libsvm package) probably has most of what you need:
if((model=svm_load_model(argv[i+1]))==0)
{
fprintf(stderr,"can't open model file %s\n",argv[i+1]);
exit(1);
}
To predict a label for example x using the model, you can run
int predict_label = svm_predict(model,x);
The trickiest part of this will be to transfer your data into the libsvm format (unless your data is in the libsvm text file format, in which case you can just use the predict function in "svm-predict.c").
A libsvm vector, x, is an array of struct svm_node that represents a sparse array of data. Each svm_node has an index and a value, and the vector must be terminated by an index that is set to -1. For instance, to encode the vector [0,1,0,5], you could do the following:
struct svm_node *x = (struct svm_node *) malloc(3*sizeof(struct svm_node));
x[0].index=2; //NOTE: libsvm indices start at 1
x[0].value=1.0;
x[1].index=4;
x[1].value=5.0;
x[2].index=-1;
For SVM types other than the classifier (C_SVC), look at the predict function in "svm-predict.c".

Code requires too much memory

I'm porting some code to another structure:
class EnvironObject
{
protected:
vector<float> mX, mY, mXSpeed, mYSpeed;
int mMaxObjects;
public:
virtual void init(int maxObjects);
virtual void setLimit(int limit);
virtual int getLimit();
virtual void update(float arg) = 0;
};
void EnvironObject::setLimit(int limit)
{
mMaxObjects = limit;
mX.resize(limit, 0); mY.resize(limit, 0);
mXSpeed.resize(limit, 0); mY.resize(limit, 0);
}
int EnvironObject::getLimit()
{
return mMaxObjects;
}
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
fill(mX.begin(), mX.end(), 0);
fill(mY.begin(), mY.end(), 0);
fill(mXSpeed.begin(), mXSpeed.end(), 0);
fill(mYSpeed.begin(), mYSpeed.end(), 0);
/*mX.reserve(mMaxObjects * 1.5); mY.reserve(mMaxObjects * 1.5);
mXSpeed.reserve(mMaxObjects * 1.5); mYSpeed.reserve(mMaxObjects * 1.5);*/
mMaxObjects = maxObjects;
}
This is some basic class, now it's usage:
class Rain : public EnvironObject
{
public:
Rain(int maxDrops = 150);
void update(float windPower);
};
Rain::Rain(int maxDrops)
{
srand(time(NULL));
IEnvironObject::init(maxDrops);
}
void Rain::update(float windPower)
{
for (int i=0; i < mMaxObjects; i++)
{
mX[i] += mXSpeed[i];
mY[i] += mYSpeed[i];
mXSpeed[i] += windPower;
mYSpeed[i] += G;
// Drawing
}
}
The objects Rain creates with default constructor (so, each array is 150 elements size) and then I'm calling setLimit(50).
The problem is that code fails almost each running with exception:
terminate called after throwing an instance of 'std::bad_alloc'
And sometimes it segfaults at line:
mY[i] += mYSpeed[i];
I can't image what could it be, because the code is old and it worked. The new one is only base class.
And when I'm looking at RAM usage when starting app, I see almost +600 mb!
Look again at that function of yours:
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
// ^
// ...
mMaxObjects = maxObjects;
}
You're using a not yet initialized variable.
A big problem with your class is that you are doing what's called two-phase construction. Your class EnvironObject has a compiler-supplied default constructor that creates an object with random values for all POD types (mMaxObjects). Users then need to call the init() method to really initialize the object. But that's what constructors are there for!
void EnvironObject::EnvironObject(int maxObjects)
: mMaxObjects(maxObjects)
, mX(maxObjects), mY(maxObjects), mXSpeed(maxObjects), mYSpeed(maxObjects)
{
/* these aren't necessary, std::vector automatically does this
fill(mX.begin(), mX.end(), 0);
fill(mY.begin(), mY.end(), 0);
fill(mXSpeed.begin(), mXSpeed.end(), 0);
fill(mYSpeed.begin(), mYSpeed.end(), 0);
*/
}
Derived classes can then use this constructor:
Rain::Rain(int maxDrops)
: EnvironObject(maxDrops)
{
srand(time(NULL));
}
Regarding this crash in the subscription mY[i] += mYSpeed[i]:
This might happen when you are calling this function through a pointer that's pointing to nowhere.
You're using mMaxObjects in init() before initializing it. So it has a random value.
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects); // you mean maxObjects here
I think you want to replace
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
with
void EnvironObject::init(int maxObjects)
{
mX = mY = mXSpeed = mYSpeed = std::vector<float>(maxObjects);
Notice the replacement of mMaxObject to maxObjects in the vector creation.
One comment, though it won't likely fix your memory error, is that since the fields mX, mY, mXSpeed, and mYSpeed seem related and the vectors are all the same size, you should consider merging them into one structure with four members, and having a single vector containing several of those structure instances.