B-P algorithm to function y = 0.4 m in (2 * pi * x) 0.5
#include
#include
#include
#define M 8 // Hidden node number
#define n 20 // Learn the sample
#define pi 3.14159
//
Static float x [n]; // x input
Static float y [n]; // y output
Static float t [n]; // t teacher
Static float h [m] [n]; // Record the output of hidden unit
Static float b [n]; // Output error
Static float b1 [m] [n]; // Record hidden unit error
Float W1 [M] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}; // Enter the right to the first hidden layer
Float W2 [M] = {0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}; // Right of the first hidden layer and output layer
Float Q [M] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}; // Threshold of the first hidden layer
Float P = 0.3; // Threshold of Output Layer
FLOAT R = 0.2; //
INT n = 0; // Record the number of training
//
Float F (Float U)
{
RETURN 1 / (1 Exp (-U));
}
///
// Assignment function
Void Fuzhi (Void)
{
INT K;
For (k = 0; k x [k] = (float) k / 20; // input value
For (k = 0; k {
T [k] = 0.4 * sin (2 * pi * x [k]) 0.5; // calculate the value of the teacher
Cout << "x [" <}
}
/ / Calculate the hidden unit output, output unit
Void Jisuan (Void)
{
INT I, J;
Static float a [n]; // store intermediate results
For (i = 0; I for (j = 0; J H [i] [j] = f (W1 [i] * x [j] -q [i]); // calculate hidden unit output
For (j = 0; j {
a [j] = 0; // pay attention to the initial value
For (i = 0; i a [j] = w2 [i] * h [i] [j];
y [j] = f (a [j] -p);
}
///
//Calculation error
Void wucha (void)
{
INT I, J;
For (j = 0; j b [j] = y [j] * (1-y [j]) * (t [j] -y [j]); // output
For (i = 0; I for (j = 0; J B1 [i] [j] = b [j] * w2 [i] * h [i] [j] * (1-h [i] [j] ); // hidden unit
}
//
/ 权 权 修
Void Xiugai (Void)
{
Float C, D; // Temporary variable
INT I, J;
For (i = 0; i {
C = 0;
For (j = 0; J C = B [J] * h [i] [j];
W2 [i] = r * C;
}
C = 0;
For (j = 0; J C = B [J] * (- 1);
P = C * R;
For (i = 0; i {
C = 0;
D = 0;
For (j = 0; j {
C = B1 [i] [j] * x [j];
D = B1 [I] [J] * (- 1);
}
W1 [i] = r * C;
Q [i] = r * D;
}
}
/ / Total error of the output
Float Shuchu (Void)
{
FLOAT E = 0;
For (int J = 0; J E = (T [J] -Y [J]) * (T [J] -Y [J]);
Return E;
///
// master function
void main ()
{
Char S;
Float EZ;
Fuzhi ();
DO { N;
Jisuan ();
WUCHA ();
Xiugai ();
EZ = shuchu (); cout <} while (eZ / 2> 0.01);
// for (int J = 0; j // cout << "y ["
}
//