• 大小: 28KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: Java
  • 标签:

资源简介

java实现的bp神经网络算法,代码超级简单,里面有实际数据例子,一看就懂!

资源截图

代码片段和文件信息

package com.bp;

import java.text.DecimalFormat;
import java.util.Random;

public class BpDeep {
public double[][] layer;//神经网络各层节点
    public double[][] layerErr;//神经网络各节点误差
    public double[][][] layer_weight;//各层节点权重
    public double[][][] layer_weight_delta;//各层节点权重动量
    public double mobp;//动量系数
    public double rate;//学习系数
    public double cost;//单次代价

    public BpDeep(int[] layernum double rate double mobp){
        this.mobp = mobp;
        this.rate = rate;
        this.cost = 0.0f;//单次代价

        layer = new double[layernum.length][];
        layerErr = new double[layernum.length][];
        layer_weight = new double[layernum.length][][];
        layer_weight_delta = new double[layernum.length][][];
        Random random = new Random();
        for(int l=0;lyernum.length;l++){
            layer[l]=new double[layernum[l]];
            layerErr[l]=new double[layernum[l]];
            if(l+1yernum.length){
                layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
                layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
                for(int j=0;jyernum[l]+1;j++)
                    for(int i=0;iyernum[l+1];i++){
                     double r1 = Math.sqrt(6) / Math.sqrt(layernum[l] + layernum[l+1] + 1);
                        layer_weight[l][j][i]=random.nextDouble()*2*r1-r1;//随机初始化权重
                     //layer_weight[l][j][i]=random.nextDouble();
                    }
            }   
        }
    }
    //逐层向前计算输出
    public double[] computeOut(double[] in){
        for(int l=1;lyer.length;l++){
            for(int j=0;jyer[l].length;j++){
                double z=layer_weight[l-1][layer[l-1].length][j];
                for(int i=0;iyer[l-1].length;i++){
                    layer[l-1][i]=l==1?in[i]:layer[l-1][i];// l==1时说明是第一层输入层
                    z+=layer_weight[l-1][i][j]*layer[l-1][i];
                }
                layer[l][j]=1/(1+Math.exp(-z));
            }
        }
        return layer[layer.length-1];
    }
    //逐层反向计算误差并修改权重
    public void updateWeight(double[] tar){
        int l=layer.length-1;
        for(int j=0;jyerErr[l].length;j++)
            layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);

        while(l-->0){
            for(int j=0;jyerErr[l].length;j++){
                double z = 0.0;
                for(int i=0;iyerErr[l+1].length;i++){
                    z=z+l>0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
                    layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整
                    layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
                    if(j==layerErr[l].length-1){
                        layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整
                        layer_weight[l][j+1][i]+=layer_weight_delta

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-08-25 15:07  backpropagation\
     文件         411  2016-08-26 10:08  backpropagation\.classpath
     文件         391  2016-08-25 15:07  backpropagation\.project
     目录           0  2016-08-25 15:07  backpropagation\.settings\
     文件         598  2016-08-25 15:07  backpropagation\.settings\org.eclipse.jdt.core.prefs
     目录           0  2016-08-26 10:08  backpropagation\bin\
     目录           0  2016-08-26 10:08  backpropagation\bin\com\
     目录           0  2016-08-26 10:55  backpropagation\bin\com\bp\
     文件        3234  2016-08-27 11:15  backpropagation\bin\com\bp\BpDeep.class
     文件        3114  2016-08-27 11:17  backpropagation\bin\com\bp\BpDeepRealize.class
     文件        1486  2016-08-26 17:47  backpropagation\bin\com\bp\BpDeepTest.class
     文件        7839  2016-08-25 16:26  backpropagation\bin\com\bp\data.txt
     文件        5941  2016-08-25 16:26  backpropagation\bin\com\bp\data_y.txt
     文件        5754  2016-08-25 16:27  backpropagation\bin\com\bp\per_y.txt
     文件        5576  2016-08-25 16:26  backpropagation\bin\com\bp\perdata.txt
     文件         732  2016-08-26 10:08  backpropagation\bin\com\bp\testjava.class
     目录           0  2016-08-25 15:09  backpropagation\src\
     目录           0  2016-08-25 15:09  backpropagation\src\com\
     目录           0  2016-08-27 11:18  backpropagation\src\com\bp\
     文件        4307  2016-08-27 11:15  backpropagation\src\com\bp\BpDeep.java
     文件        4179  2016-08-27 11:17  backpropagation\src\com\bp\BpDeepRealize.java
     文件        1484  2016-08-26 17:47  backpropagation\src\com\bp\BpDeepTest.java
     文件        3166  2016-08-27 11:18  backpropagation\src\com\bp\bp.zip
     文件        7839  2016-08-25 16:26  backpropagation\src\com\bp\data.txt
     文件        5941  2016-08-25 16:26  backpropagation\src\com\bp\data_y.txt
     文件        5754  2016-08-25 16:27  backpropagation\src\com\bp\per_y.txt
     文件        5576  2016-08-25 16:26  backpropagation\src\com\bp\perdata.txt
     文件         247  2016-08-25 17:09  backpropagation\src\com\bp\testjava.java

评论

共有 条评论

相关资源