这是一篇基于本文的神经网络:https://stevenmiller888.github.io/mind-how-to-build-a-neural-network/
我试图尽可能地遵循一切,但它不起作用。训练有点作品,但不是很好。此外,任何时候我输入0,0,无论我如何训练它,它都会以0响应。
继承人的意见:
0,0 0
1,0 1
0,1 1
1,1 1
下面是代码:
#include <iostream>
#include <time.h>
using namespace std;
double Sigmoid(double y)
{
return y / sqrt(1 + pow(y, 2));
}
class Neuron
{
public:
double output;
double input1wt;
double input2wt;
double input1;
double input2;
double outputwt;
void Init()
{
// srand(time(NULL));
input1wt = rand() / (RAND_MAX + 1.);
input2wt = rand() / (RAND_MAX + 1.);
outputwt = rand() / (RAND_MAX + 1.);
}
double Solve()
{
if (input1 != -1 && input2 != -1)
{
return Sigmoid(input1*input1wt + input2*input2wt);
}
else return -1;
}
};
bool Train(Neuron n1, Neuron n2, Neuron n3, double x, double y, double targetResult, int iterations)
{
double deltaOutputSum;
double deltaHiddenSum[3];
n1.input1 = x;
n1.input2 = y;
n2.input1 = x;
n2.input2 = y;
n3.input1 = x;
n3.input2 = y;
for (int c = 0; c < iterations; c++)
{
deltaOutputSum = Sigmoid(n1.output * n1.outputwt + n2.output * n2.outputwt + n3.output * n3.outputwt) * (1 - Sigmoid(n1.output * n1.outputwt + n2.output * n2.outputwt + n3.output * n3.outputwt)) * (targetResult - Sigmoid(n1.output * n1.outputwt + n2.output * n2.outputwt + n3.output * n3.outputwt));
deltaHiddenSum[0] = deltaOutputSum * n1.outputwt * (Sigmoid(n1.input1 * n1.input1wt + n1.input2 * n1.input2wt) * (1 - Sigmoid(n1.input1 * n1.input1wt + n1.input2 * n1.input2wt)));
deltaHiddenSum[1] = deltaOutputSum * n2.outputwt * (Sigmoid(n2.input1 * n2.input1wt + n2.input2 * n2.input2wt) * (1 - Sigmoid(n2.input1 * n2.input1wt + n2.input2 * n2.input2wt)));
deltaHiddenSum[2] = deltaOutputSum * n3.outputwt * (Sigmoid(n3.input1 * n3.input1wt + n3.input2 * n3.input2wt) * (1 - Sigmoid(n3.input1 * n3.input1wt + n3.input2 * n3.input2wt)));
n1.outputwt += n1.outputwt * deltaOutputSum;
n2.outputwt += n2.outputwt * deltaOutputSum;
n3.outputwt += n3.outputwt * deltaOutputSum;
n1.input1wt += deltaHiddenSum[0];
n1.input2wt += deltaHiddenSum[1];
n2.input1wt += deltaHiddenSum[2];
n2.input2wt += deltaHiddenSum[0];
n3.input1wt += deltaHiddenSum[1];
n3.input2wt += deltaHiddenSum[2];
n1.output = n1.Solve();
n2.output = n2.Solve();
n3.output = n3.Solve();
}
return true;
}
int main()
{
srand(time(NULL));
int targetResult = 0;
int a;
int b;
int d;
int c;
Neuron n1 = Neuron();
Neuron n2 = Neuron();
Neuron n3 = Neuron();
n1.Init();
n2.Init();
n3.Init();
for (int c = 0; c < 1000000; c++)
{
Train(n1, n2, n3, 1, 1, 0, 1);
Train(n1, n2, n3, 1, 0, 1, 1);
Train(n1, n2, n3, 0, 0, 0, 1);
Train(n1, n2, n3, 0, 1, 1, 1);
}
cout << "Done\n";
while (2)
{
cin >> a;
cin >> b;
//cin >> d;
n1.input1 = a;
n1.input2 = b;
n2.input1 = a;
n2.input2 = b;
n3.input1 = a;
n3.input2 = b;
//Train(n1, n2, n3, a, b, d, 1000);
n1.output = n3.Solve();
n2.output = n3.Solve();
n3.output = n3.Solve();
cout << "\n" << Sigmoid(n1.output * n1.outputwt + n2.output * n2.outputwt + n3.output * n3.outputwt) << "\n";
}
return 1;
}
感谢您阅读本文,如果可以的话,这确实困扰着我。