gpt4 book ai didi

java - ANN 感知器或门训练无法正常工作

转载 作者:行者123 更新时间:2023-11-30 07:02:53 26 4
gpt4 key购买 nike

所以我正在尝试用感知器来训练或门。问题是它不起作用。错误的是 0 0 ->desired = 0,actual = 1。并且该值不会改变。

此外,当我输入要测试的权重时,它们似乎也根本不起作用,但这可能是我的 testOut 函数出了问题。

public class Temp {

double[][] data = {{0.d, 0.d}, {0.d, 1.d}, {1.d, 0.d}, {1.d, 1.d}};
double[] outputs = {0.d, 1.d, 1.d, 1.d};
double[][] weights = {
{ThreadLocalRandom.current().nextDouble(-.5, .5),
ThreadLocalRandom.current().nextDouble(-.5, .5)},
{ThreadLocalRandom.current().nextDouble(-.5, .5),
ThreadLocalRandom.current().nextDouble(-.5, .5)},
{ThreadLocalRandom.current().nextDouble(-.5, .5),
ThreadLocalRandom.current().nextDouble(-.5, .5)},
{ThreadLocalRandom.current().nextDouble(-.5, .5),
ThreadLocalRandom.current().nextDouble(-.5, .5)}
};

public double[][] train(int maxEpoch, double threshhold) {
for (int i = 0; i < maxEpoch; i++) {
System.out.println("EPOCH " + i);
double sum = 0.0d;
double actualOutput = 0.0d;
double[] ep = new double[outputs.length];
for (int j = 0; j < data.length; j++) {
for (int k = 0; k < data[j].length; k++) {
sum += data[j][k] * weights[j][k];
}
actualOutput = step(sum - threshhold);
ep[j] = outputs[j] - actualOutput;
for (int k = 0; k < data[j].length; k++) {
weights[j][k] = weights[j][k] + .1 * data[j][k] * ep[j];
}
System.out.println("output " + j + " " + actualOutput + " " + outputs[j] +" - " + ep[j]);
}
}
return weights;

}

public void testOut(double[][] data, double[][] weights, double threshhold){
double sum = 0;
double[] actualOutput = new double[data.length];
for (int j = 0; j < data.length; j++) {
for (int k = 0; k < data[j].length; k++) {
sum += data[j][k] * weights[j][k];
}
actualOutput[j] = step(sum - threshhold);
}
System.out.println(Arrays.toString(actualOutput));
}

public static void main(String[] args) {
Temp t = new Temp();
double[][] weights = t.train(200, 0);
t.testOut(t.data, weights, .5);
}
}

感谢任何帮助。

编辑:step(sum - Threshold) 是阶跃函数。

public static int step(double x) {
return x >= 0.d ? 1 : 0;
}

最佳答案

所以我有点解决了这个问题。由于阈值的原因,阶跃函数返回 0。我把它改成了 .2d,现在工作正常了。

关于java - ANN 感知器或门训练无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40583540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com