神经网络模型:
#include
#include
#include
using namespace std;
double w1 = 0.1;
double w2 = 0.4;
double w3 = -0.2;
double w4 = 0.2;
double w5 = 0.2;
double w6 = -0.5;
double i1;
double i2;
double i3;
double o1;
double o2;
double o3;
double sigmoid(double x) {
return (1 / (1 + exp(-x)));
}
double TError(double x, double y, double z) {
i1 = w1 * x + w3 * y;
o1 = sigmoid(i1);
i2 = w2 * x + w4 * y;
o2 = sigmoid(i2);
i3 = w5 * o1 + w6 * o2;
o3 = sigmoid(i3);
return (o3 - z) * (o3 - z);
}
void BP(double x, double y, double z) {
double c1, c2, c3;
c3 = (z - o3) * o3 * (1 - o3);
c1 = c3 * w5 * o1 * (1 - o1);
c2 = c3 * w6 * o2 * (1 - o2);
w1 = w1 + x * c1 * 0.6;
w2 = w2 + x * c2 * 0.6;
w3 = w3 + y * c1 * 0.6;
w4 = w4 + y * c2 * 0.6;
w5 = w5 + o1 * c3 * 0.6;
w6 = w6 + o2 * c3 * 0.6;
}
double Verify(double x, double y) {
i1 = w1 * x + w3 * y;
o1 = sigmoid(i1);
i2 = w2 * x + w4 * y;
o2 = sigmoid(i2);
i3 = w5 * o1 + w6 * o2;
o3 = sigmoid(i3);
return o3;
}
int main()
{
double terror;
int t = 0;
double dataSet[20][3] = {
{5.3,3.7,0.2},
{4.8,3.0,0.3},
{5.1,3.8,0.4},
{4.4,3.0,0.2},
{5.4,3.4,0.4},
{5.0,3.4,0.4},
{5.1,3.3,0.5},
{5.1,3.7,0.4},
{5.7,3.8,0.3},
{5.4,3.9,0.4},
{5.1,3.5,0.2},
{4.9,3.0,0.2},
{4.6,3.1,0.2},
{5.4,3.9,0.4},
{5.0,3.4,0.2},
{4.4,2.9,0.2},
{5.4,3.7,0.2},
{5.8,4.0,0.2},
{5.0,3.5,0.6},
{4.9,3.1,0.1},
};
for (int i = 0; i < 20; i++) {
terror = TError(dataSet[i][0], dataSet[i][1], dataSet[i][2]);
BP(dataSet[i][0], dataSet[i][1], dataSet[i][2]);
if (terror < 0.0001 && t>0) break;
if (i == 19) {
i = 0;
t++;
}
}
double verify;
verify = Verify(5.1, 3.9);
std::cout << "实际结果是:0.2 " << "计算结果是:" << verify << endl;
verify = Verify(4.6, 3.4);
std::cout << "实际结果是:0.3 " << "计算结果是:" << verify << endl;
verify = Verify(5.4, 3.9);
std::cout << "实际结果是:0.4 " << "计算结果是:" << verify << endl;
}