我在Java中实现了一个简单的神经网络,用于解决XOR问题(http://www.mind.ilstu.edu/curriculum/artificial_neural_net/xor_problem_and_solution.php)。
我想知道你需要什么神经元和一个NeuronLayer等课程,所以我决定尝试只使用一个课程。
不幸的是,在一些时期之后,它达到了一个错误是两个值之一的状态:0.5或-0.5。
我尝试过限制选择起始重量但不会改变任何内容的范围。
也许,你们中的一些人可以帮助我发现错误或改善网络。 提前谢谢!
这是我的代码:
int inCount = 2;
int hidCount = 3;
int outCount = 1;
float learningRate = 0.01;
float[] inputs = new float[inCount];
float[] hidden = new float[hidCount];
float[] outputs = new float[outCount];
float[][] IHweights = new float[inCount][hidCount];
float[][] HOweights = new float[hidCount][outCount];
void setup ()
{
for (int i = 0; i < IHweights.length; i++)
{
for (int e = 0; e < IHweights[i].length; e++)
{
float newWeight = random(-1,1);
while(newWeight > -0.5 && newWeight < 0.5)
{
newWeight = random(-1,1);
}
IHweights[i][e] = newWeight;
println(IHweights[i][e]+"\n");
}
}
for (int i = 0; i < HOweights.length; i++)
{
for (int e = 0; e < HOweights[i].length; e++)
{
float newWeight = random(-1,1);
while(newWeight > -0.5 && newWeight < 0.5)
{
newWeight = random(-1,1);
}
HOweights[i][e] = newWeight;
}
}
}
void draw ()
{
float[] inData = {round(random(1)),round(random(1))};
println(inData[0]+" "+inData[1]);
float[] expResult = {(int) inData[0]^(int) inData[1]};
println(" -> "+expResult[0]);
feedForward(inData,expResult);
}
public float sigmoid (float x)
{
if (x>10)
{
return 1;
}
if (x<-10)
{
return 0;
}
return 1/(1+exp(-x));
}
public void feedForward (float[] input, float[] expOut)
{
inputs = input;
for (int i = 0; i < hidCount; i++)
{
float var = 0;
for (int e = 0; e < inCount; e++)
{
var += inputs[e] * IHweights[e][i];
}
hidden[i] = sigmoid(var);
}
for (int i = 0; i < outCount; i++)
{
float var = 0;
for (int e = 0; e < hidCount; e++)
{
var += hidden[e] * HOweights[e][i];
}
outputs[i] = sigmoid(var);
}
float[] error = new float[outCount];
float[] deltaOut = new float[outCount];
for (int i = 0; i < outCount; i++)
{
error[i] = expOut[i] - outputs[i];
deltaOut[i] = outputs[i] * (1-outputs[i]) * error[i];
}
float[] deltaHid = new float[hidCount];
for (int i = 0; i < hidCount; i++)
{
deltaHid[i] = hidden[i] * (1-hidden[i]);
for (int e = 0; e < outCount; e++)
{
deltaHid[i] += HOweights[i][e] * outputs[e];
}
}
for (int i = 0; i < inCount; i++)
{
for (int e = 0; e < hidCount; e++)
{
IHweights[i][e] += inputs[i] * deltaHid[e] * learningRate;
}
}
for (int i = 0; i < hidCount; i++)
{
for (int e = 0; e < outCount; e++)
{
HOweights[i][e] += hidden[i] * deltaOut[e] * learningRate;
}
}
println(error[0]);
}