我正在编码感知器,但它没有产生正确的输出。我从文件中获取输入,显然我的错误不应该为零。最接近的错误结果应该是5或6.所以我不确定问题出在哪里。 (我将偏差设为-1,这就是为什么我在捕获目标数组后将x [i] [2]设置为-1。)
示例来自文件的输入:(最后一个数字是目标,所以我在T数组中捕获它后将最后一个数字更改为-1)。文件中有100个这样的输入。每行3列作为示例
7.316 5.871 1
class Program {
public static int percept(double[] W, double[] X) {
double total = 0;
for (int i = 0; i < X.length; i++) {
total += W[i] * X[i];
//System.out.println("total: "+total);
}
if (total > 0) {
return 1;
} else {
return 0;
}
}
public static double[] PerceptLearn(double[][] X, double[] T, double[] W, double n) {
double[] yOutput = new double[X.length];
double error = 0;
double sum=0;
int count = 0;
while (count < 3) {
for (int k = 0; k < T.length; k++) { //t.length = 100
int y = percept(W, X[k]);
for (int i = 0; i < 3; i++) {
W[i] = W[i] + (n * (T[k] - y) * X[k][i]);
}
yOutput[k] = y;
}
error = errorFunction(T, yOutput);
System.out.println("Error: " + error);
count++;
}
return W;
}
public static double errorFunction(double[] T, double[] yOut) {
double sum =0;
for (int i = 0; i < T.length; i++) {
sum = (int) Math.abs(T[i] - yOut[i]);
}
return sum;
}
public static void main(String[] args) {
Scanner scan; // NEW FILE (PATH)
File file = new File("C:\\Users\\Asus\\Documents\\NetBeansProjects\\perceptron\\test\\H1dataset1.txt");
try {
scan = new Scanner(file);
double x;
double[][] X = new double[100][3];
double[] T = new double[100];
double[] W = new double[100];
double[] B = new double[100];
double n = 0.25;
//int max_iter = 100;
while (scan.hasNext()) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 3; j++) {
x = scan.nextDouble();
X[i][j] = x;
//System.out.println(X[i][j]);
}
}
for (int i = 0; i < 3; i++) {
double randomNum = ThreadLocalRandom.current().nextDouble(-1, 2);
W[i] = randomNum;
System.out.println("weight: " + W[i]);
}
for (int i = 0; i < 100; i++) {
T[i] = X[i][2];
//System.out.println("target: " + T[i]);
}
//Setting bias as -1 at X[i][2] for all 100 inputs which i get from the file.
for (int i = 0; i < 100; i++) {
X[i][2] = -1;
System.out.println(X[i][2]);
}
double[] output = PerceptLearn(X, T, W, n);
for (int i = 0; i < output.length; i++) {
//System.out.println("output: "+output[i]);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
}
}
我得到的输出是: 重量:0.12368274236843613 重量:1.1523570912710452 重量:0.6621886132628083 错误:0.0 错误:0.0 错误:0.0 我没有找到任何有用的教程来解决这个问题。