# Demo entry 5874888

qqqqqq

Submitted by sassssss on Aug 25, 2016 at 20:06
Language: ANTLR With CPP Target. Code size: 3.4 kB.

```#include <iostream>
#include <fstream>
#include <vector>
#include <cassert>
using namespace std;

{
public:
void create_simulate_data(double x_from=0, double x_to=1.0, int N=100)
{
xs.resize(N);
ys.resize(N);
for (int i = 0; i < N; ++i)
{
xs[i] = x_from + (x_to - x_from) / N*i;
ys[i] = function(xs[i]);
}
}
protected:
// you need overload this function!!!!
// since we only have one cell, we need make sure function() always returns data between (0,1), and 单调
virtual double function(double x)
{
return x*x*x*x;
}
public:
vector<double>    xs;  // remember xs[0] always equals 1
vector<double>    ys;
};
{

public:
{
// 懒得写单个训练版本了，直接批处理！
double new_mse = 0; // use this to detect whether we can quit！
do
{
// we will use batch training method
for (int j = 0; j < 2; ++j)
{
double sigma_xEx = 0;
double x = 0;

for (int i = 0; i < data.xs.size(); ++i)
{
if (j == 0)
x = 1;
else
x = data.xs[i];

sigma_xEx += (data.ys[i] - output(data.xs[i]))*x;
}

ws[j] += beta*((sigma_xEx) / data.xs.size());  //调整权值！！！delta法则，用的是批训练的方法
}

new_mse = MSE(data);
cout << "w[0]=" << ws[0] << "，      w[1]=" << ws[1] << ",   error="<<new_mse<<endl;
} while (new_mse > 0.00038);

}
{
// TBD
}
virtual void init(double beta1, int w_size, int* p_ws = NULL)
{
ws.clear();
ws.resize(w_size);
beta = beta1;
if (p_ws)
{
for (int i = 0; i < w_size; ++i)
{
ws[i] = p_ws[i];
}
return;
}
else
{
for (int i = 0; i < w_size; ++i)
{
ws[i] = 0.5;
}
return;

}

}
public:
// general version
double output(vector<double> & xs)
{
assert(xs.size() == ws.size());
double u = 0;
for (int i = 0; i < xs.size(); ++i)
{
u += xs[i] * ws[i];
}
return stimulate_function(u);
}
// special version for y=f(x),
double output(double x)
{
vector<double> xs = {1,x};
return output(xs);
}
protected:
virtual double stimulate_function(double u)
{
double ret= 1 / (1 + exp(-u));
return ret;
}
double MSE(madnet_training_data& data) // mean square error
{
int size = data.ys.size();
double sum_error = 0;
for (int i = 0; i < size; ++i)
{
double error = data.ys[i] - output(data.xs[i]);
sum_error += (error * error);
}
return sum_error / (2*size);
}
protected:
vector<double>  ws;       // u = 1*w[0]+x[1]*w[1]+....x[n]*w[n]
double    beta ;     // training speed, let it be 0.5 firstly
};

int main()
{
data.create_simulate_data(0, 1, 100);

cell.init(0.5,2,NULL);
cell.training(data);
//============================================================================================
// now print the result, using csv file
os << "x, expected data, data computed using madnet" << endl;
for (int i = 0; i < data.xs.size(); ++i)
{
double expected = data.ys[i];
double actual = cell.output(data.xs[i]);
os <<data.xs[i]<<","<< expected << "," << actual << endl;
}
os.close();
}
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.