所以我试图训练一个基本上是感知器的OR门。问题是它不起作用。错误的是0 0 - >期望= 0,实际= 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);
}
}
任何帮助表示赞赏。
编辑:步骤(sum - threshold)是步进功能。
public static int step(double x) {
return x >= 0.d ? 1 : 0;
}
答案 0 :(得分:1)
所以我有点解决了这个问题。由于阈值的值,步长函数返回0。我做了.2d而现在它工作正常。