📄 bp.java
字号:
package com.lee.test.BP;
public class BP {
private static final double learnRate = 0.9 ;
private static final int inputLayerTotalNodes = 3 ;
private static final int hiddenLayerTotalNodes = 2 ;
private static final int outputLayerTotalNodes = 1 ;
private static final double weightNumber = 0.5 ;
private static final double theatNumber = 0.5 ;
public static void main(String[] args) {
// TODO Auto-generated method stub
//初始化输入元素,期望输出
double[] inputElements = {1,0,1};
double[] expectOutputElements = {1};
//隐层的权值与阈值,误差,隐层的输出即输出层的输入
//在这里为了节省储存空间,采用一维数组进行存储,存储顺序为00,01,10,11...如此类推
double[] hiddenLayerWeights = {0.2,-0.3,0.4,0.1,-0.5,0.2} ;
double[] hiddenLayerTheats = {-0.4,0.2} ;
double[] hiddenLayerErrors = new double[hiddenLayerTotalNodes ];
double[] hiddenSumForInput = new double[hiddenLayerTotalNodes] ;
//输出层的权值与阈值,误差,输出层的输出,即输入层的输入
double[] outputLayerWeights = {-0.3,-0.2} ;
double[] outputLayerTheats = {0.1} ;
double[] outputLayerErrors = new double[outputLayerTotalNodes ];
double[] outputLayerBackErrors = new double[hiddenLayerTotalNodes];
double[] outputSumForInput = new double[hiddenLayerTotalNodes] ;
//用来存储输入输出
double[] hiddenLayerInput = new double[hiddenLayerTotalNodes] ;
double[] hiddenLayerOutput = new double[hiddenLayerTotalNodes] ;
double[] outputLayerInput = new double[outputLayerTotalNodes] ;
double[] outputLayerOutput = new double[outputLayerTotalNodes] ;
double[] outputBackErrorsSumForHidden = new double[hiddenLayerTotalNodes];
//计算隐层输入输出,为了编程思路连贯,这里以及后续都默认i在j前边,比如,i表示输入层,则j表示隐层。
//int loop = 0 ;
//while( loop < 10000){
for(int j = 0 ;j < hiddenLayerTotalNodes ; j++){
for(int i = 0 ; i < inputLayerTotalNodes ;i++){
//输入
hiddenLayerInput[j] += hiddenLayerWeights[i*hiddenLayerTotalNodes+j]*inputElements[i];
hiddenSumForInput[j] += hiddenLayerInput[j];
}
hiddenSumForInput[j] += hiddenLayerTheats[j];
hiddenLayerOutput[j] = 1.0 / (1 + Math.exp(-hiddenSumForInput[j]));
}
//计算输出层输入输出
for(int j = 0 ;j < outputLayerTotalNodes ; j++){
for(int i = 0 ; i < hiddenLayerTotalNodes ;i++){
//输入
outputLayerInput[j] += outputLayerWeights[i*outputLayerTotalNodes+j]*hiddenLayerOutput[i];
outputSumForInput[j] += outputLayerInput[j];
}
outputSumForInput[j] += outputLayerTheats[j];
//输出
outputLayerOutput[j] = 1.0 / (1 + Math.exp(-outputSumForInput[j]));
//输出层误差
outputLayerErrors[j] = outputLayerOutput[j]*( 1-outputLayerOutput[j] )*(expectOutputElements[j] - outputLayerOutput[j]);
//修改阈值
outputLayerTheats[j] = outputLayerTheats[j] + learnRate *outputLayerErrors[j];
}
for(int i = 0 ; i < hiddenLayerTotalNodes ;i++){
for(int j = 0 ;j < outputLayerTotalNodes ; j++){
outputLayerBackErrors[i] += outputLayerErrors[j]*outputLayerWeights[i*outputLayerTotalNodes+j];
outputBackErrorsSumForHidden[i] += outputLayerBackErrors[i];
}
//隐层误差
hiddenLayerErrors[i] = hiddenLayerOutput[i]*(1-hiddenLayerOutput[i])*outputBackErrorsSumForHidden[i];
//修改阈值
hiddenLayerTheats[i] = hiddenLayerTheats[i] + learnRate*hiddenLayerErrors[i];
}
//修改输出层权值
for(int j = 0 ;j < outputLayerTotalNodes ; j++){
for(int i =0 ; i < hiddenLayerTotalNodes ;i++ ){
outputLayerWeights[i*outputLayerTotalNodes+j] += learnRate * outputLayerErrors[j] *hiddenLayerOutput[i] ;
}
}
//修改隐层权值
for(int j = 0 ;j < hiddenLayerTotalNodes ; j++){
for(int i =0 ; i < inputLayerTotalNodes ;i++ ){
hiddenLayerWeights[i*hiddenLayerTotalNodes+j] += learnRate * hiddenLayerErrors[j] *inputElements[i] ;
}
}
//loop++;
//}
System.out.println("隐层结点的权值依次为:");
for(int i = 0 ; i < hiddenLayerWeights.length ;i++){
System.out.print( hiddenLayerWeights[i] + " ");
}
System.out.println();
System.out.println("隐层结点的阈值依次为:");
for(int i = 0 ; i < hiddenLayerTheats.length ;i++){
System.out.print(hiddenLayerTheats[i] + " ");
}
System.out.println();
System.out.println("输出层结点的权值依次为:");
for(int i = 0 ; i < outputLayerWeights.length ;i++){
System.out.print( outputLayerWeights[i] + " ");
}
System.out.println();
System.out.println("输出层结点的阈值依次为:");
for(int i = 0 ; i < outputLayerTheats.length ;i++){
System.out.print(outputLayerTheats[i] + " ");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -