Java 为什么这段代码运行不正常
这是我的代码,我给它输入如下,我删除了用户给出实际输出值的步骤,因为它是在代码中计算的,但仍然发生同样的事情 “1”表示第一次输入,然后 第一次输入重量为“0.5” “0”表示第二个输入 “0.6”表示其重量 “0”表示所需的输出,然后 “0.002”表示定义值,然后 学习率为“0.23”Java 为什么这段代码运行不正常,java,exponentiation,Java,Exponentiation,这是我的代码,我给它输入如下,我删除了用户给出实际输出值的步骤,因为它是在代码中计算的,但仍然发生同样的事情 “1”表示第一次输入,然后 第一次输入重量为“0.5” “0”表示第二个输入 “0.6”表示其重量 “0”表示所需的输出,然后 “0.002”表示定义值,然后 学习率为“0.23” package Neural4copy; import java.util.Scanner; import java.lang.Math; public class Demo { // in
package Neural4copy;
import java.util.Scanner;
import java.lang.Math;
public class Demo {
// inputs are declared
private int x[][]=new int[1][2];
//weights are declared
private double w[][]=new double[1][2];
private double temp;
private double z[]=new double[1];
private double desiredOutput;
private double actualOutput;
private double error;
private double definedValue=0.004;
private double weightChange[][]=new double[1][2];
private double learningRate;
private double epilision=0.000000001;
private static double zTotal;
private Scanner user_input= new Scanner(System.in);
public void getData(){
for(int j=0;j<x.length;j++){
for(int i=0;i<x[0].length;i++){
System.out.println("Enter "+j+"th neuron "+i+"th input values :");
x[j][i]=user_input.nextInt();
System.out.println("Enter "+j+"th neuron "+i+" weight of the input value :");
w[j][i]=user_input.nextDouble();
}
}
System.out.println("Enter the desired Output :");
desiredOutput=user_input.nextDouble();
System.out.println("Enter the defined value for checking the condition");
definedValue=user_input.nextDouble();
System.out.println("Enter the learning rate");
learningRate=user_input.nextDouble();
calculate();
}
public void calculate(){
for(int j=0;j<x.length;j++){
for(int i=0; i<w[0].length; i++){
temp=0;
temp=x[j][i]*w[j][i];
z[j]+=temp;
}
zTotal+=z[j];
System.out.println("temp value :"+zTotal);
}
System.out.println("ztotal "+zTotal);
//double negZ= -zTotal;
double temp2=1+Math.exp(-zTotal);
actualOutput=1/temp2;
//actualOutput=1/(1+Math.exp(-zTot));
System.out.println("actualOutput "+actualOutput);
if(Math.abs(actualOutput-desiredOutput)>epilision){
calculateError();
}
else{
printWeights();
}
}
public void calculateError(){
System.out.println("desired out put :"+desiredOutput+" actual out put :"+actualOutput);
error=0.5*((desiredOutput-actualOutput)*(desiredOutput-actualOutput));
System.out.println("error "+error+"\n\n");
//error=0.003;
checkingCondition();
}
public void checkingCondition(){
if(error<definedValue){
printWeights();
}
else{
balanceWeights();
}
}
public void balanceWeights(){
//System.out.println("new Weights are");
for(int j=0;j<w.length;j++){
for(int i=0;i<w[0].length;i++){
weightChange[j][i]=(-learningRate)*(desiredOutput-actualOutput)*actualOutput*(1-actualOutput)*x[j][i];
System.out.println("weight change "+weightChange[j][i]);
w[j][i]+=weightChange[j][i];
//System.out.print(w[i]+"\t");
}
}
calculate();
}
public void printWeights(){
System.out.print("The balanced weights are: ");
for(int j=0;j<x.length;j++){
for(int i=0;i<x[0].length;i++){
System.out.println("Neuron "+j+"Weight of input"+i+"=\t"+w[j][i]);
}
}
}
}
zTotal:348355.7408125164
为什么它从一个高值开始查看异常消息,您将看到堆栈(您调用调用方法的方法)。。。基本堆栈溢出异常
计算>计算误差>检查条件>平衡重量>计算
堆栈的大小有限,所以您使用迭代实现,但我认为当neuron值为0或never时,此代码不会收敛,如果没有注释,我真的不理解此算法。我看到了您得到的这些值,但它们不在输出的开始,它们更靠后。见: 在输出的顶部,我看到:
temp value :0.5
ztotal 0.5
actualOutput 0.6224593312018546
desired out put :0.0 actual out put :0.6224593312018546
error 0.19372780950013005
这可能取决于输出的显示方式。某些环境仅显示最后几千行,顶部将被截断。请在这一混乱处加上标点符号并大写。
temp value :0.5
ztotal 0.5
actualOutput 0.6224593312018546
desired out put :0.0 actual out put :0.6224593312018546
error 0.19372780950013005