我只是想了解一下神经网络的训练是如何工作的。我使用C#
作为首选语言,以及Accord.NET
库。
我的问题是:
有时训练错误以26%开始,然后您可以等待30分钟,使其降至25%。另一次 - 它从3%开始,在半秒内降至0.1%。或者它可以以90%的错误率开始,基本上不会下降..
以下是一些输出示例:
10000次迭代(10秒)后25%的错误。如果我继续 - 即使在30分钟后也会是25%..
100次迭代(0.5秒)后出现1.6%的错误
在相同的旧100次迭代中接近0%错误。最接近完美的结果,我想看到每一次运行。不是随意的场合!
由于这种不稳定性,我不了解如何创建一个功能齐全的神经网络,所以我可以训练它并使用它带来一些有意义的结果。我做错了什么?
以下是代码:
Console.WriteLine("Starting..");
// initialize input and output values
double[][] input =
{
new double[] {0, 0}, new double[] {0, 1},
new double[] {1, 0}, new double[] {1, 1}
};
double[][] output =
{
new double[] {0}, new double[] {1},
new double[] {1}, new double[] {0}
};
// create neural network
ActivationNetwork network = new ActivationNetwork(
new SigmoidFunction(2),
2, // two inputs in the network
2, // two neurons in the first layer
1); // one neuron in the second layer
// create teacher
var teacher = new ResilientBackpropagationLearning(network);
// loop
int ii = 1;
double error = 1.0;
while (ii < 100)
{
// teach the network
error = teacher.RunEpoch(input, output);
Console.WriteLine($"Iteration={ii}. Error={error}");
ii++;
}
// Checks if the network has learned
for (int i = 0; i < input.Length; i++)
{
double[] answer = network.Compute(input[i]);
double answer_short = answer[0];
double ans_shortened = answer_short < 0.1 ? 0 : Math.Round(answer_short, 2);
double expected = output[i][0];
// actual should be equal to expected
Console.WriteLine($"Expected is {expected}. Actual is {ans_shortened}");
}
Console.WriteLine("Finished!");
Console.ReadKey();