Java 简单的神经网络设置
2022-09-01 23:52:46
我决定尝试一些涉及Java神经网络的简单概念,并且在改编我在论坛上发现的一些无用的代码时,我已经能够为典型的初学者的XOR模拟创建一个非常简单的模型:
public class MainApp {
public static void main (String [] args) {
Neuron xor = new Neuron(0.5f);
Neuron left = new Neuron(1.5f);
Neuron right = new Neuron(0.5f);
left.setWeight(-1.0f);
right.setWeight(1.0f);
xor.connect(left, right);
for (String val : args) {
Neuron op = new Neuron(0.0f);
op.setWeight(Boolean.parseBoolean(val));
left.connect(op);
right.connect(op);
}
xor.fire();
System.out.println("Result: " + xor.isFired());
}
}
public class Neuron {
private ArrayList inputs;
private float weight;
private float threshhold;
private boolean fired;
public Neuron (float t) {
threshhold = t;
fired = false;
inputs = new ArrayList();
}
public void connect (Neuron ... ns) {
for (Neuron n : ns) inputs.add(n);
}
public void setWeight (float newWeight) {
weight = newWeight;
}
public void setWeight (boolean newWeight) {
weight = newWeight ? 1.0f : 0.0f;
}
public float getWeight () {
return weight;
}
public float fire () {
if (inputs.size() > 0) {
float totalWeight = 0.0f;
for (Neuron n : inputs) {
n.fire();
totalWeight += (n.isFired()) ? n.getWeight() : 0.0f;
}
fired = totalWeight > threshhold;
return totalWeight;
}
else if (weight != 0.0f) {
fired = weight > threshhold;
return weight;
}
else {
return 0.0f;
}
}
public boolean isFired () {
return fired;
}
}
在我的主课程中,我在对Jeff Heaton的图表进行建模时创建了简单的模拟:
但是,我想确保神经元类的实现是正确的。我已经测试了所有可能的输入([true true],[true false],[false true],[false true],[false false]),并且它们都通过了我的手动验证。此外,由于该程序接受输入作为参数,因此它似乎还通过对输入的手动验证,例如[true false false],[true true false]等。
但从概念上讲,这种实现是否正确?或者,在我开始进一步开发和研究此主题之前,我该如何改进它?
谢谢!