Я визуализирую Perceptron с Backpropagation для задания по информатике, но по какой-то причине с данными обучения XOR он всегда обучается выводить значения, близкие к нулю для любых входов, и я не могу найти проблему, вот код: https://gitlab.com/OmegaRogue/neuralnetworkunity Редактировать: соответствующий код: DisplayNn.cs
private void LearnStep()
{
_synapses.ForEach(synapse => synapse.Learn());
_neurons.ForEach(neuron => neuron.Learn());
error = CalculateError();
iterations++;
GraphManager.Graph.Plot("Error",
error,
Color.green);
}
private float CalculateError()
{
return _outLayer.Sum(a => Math.Abs(a.error));
}
Neuron.cs
public void Calculate()
{
if (!_initialized)
return;
switch (type)
{
case ENeuronType.Hidden:
case ENeuronType.Output:
var result =
NeuralMaths.Transfer[(int) nn.transferFunction](NeuralMaths.Input
[
(int) nn
.inputFunction](inputSynapses
.Select(synapse =>
synapse
.Data)));
value = result.value;
derivative = result.derivative;
break;
case ENeuronType.Input:
value = input;
break;
default:
throw new ArgumentOutOfRangeException();
}
switch (type)
{
case ENeuronType.Output:
{
error = NeuralMaths.Error[(int)nn.errorFunction](value, nn.CurrentData.output[0]);
delta = error * derivative;
break;
}
case ENeuronType.Hidden:
{
var sum = outputSynapses.Sum(synapse => synapse.output.delta * synapse.weight);
delta = derivative * sum;
break;
}
case ENeuronType.Input:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
Synapse.cs
private void Start()
{
var numberOfInputConnections = output.inputSynapses.Count;
var coefficient = 1/ math.sqrt(numberOfInputConnections);
coefficient = coefficient == 0 ? 1 : coefficient;
weight = coefficient * Random.Range(-0.5f, 0.5f);
}
private void Update()
{
if(!initialized)
return;
var position = input.transform.position;
angle = Vector2.SignedAngle(input.Right -position, output.Left -position);
transform.position = Vector3.Lerp(input.Right, output.Left, .5f);
transform.rotation = Quaternion.Euler(0, 0, angle);
graphicTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Vector2.Distance(input.Right, output.Left));
weightDelta = -nn.learningRate * output.delta * input.value;
weightLabel.text = $"{weight}";
synapseGraphic.color = weight < 0 ? Color.red * math.abs(weight):Color.green * math.abs(weight);
}
public void Learn()
{
weight += weightDelta;
}
NeuralMaths.cs
#region Error
private static float SquaredError(float predictedOutput, float targetOutput) =>
math.pow(predictedOutput - targetOutput, 2);
private static float AbsoluteError(float predictedOutput, float targetOutput) =>
math.abs(predictedOutput - targetOutput);
#endregion