⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 network.java

📁 bp神经网络的java实现源代码
💻 JAVA
字号:
package mathtools.bptools;

import java.util.Random;
import java.util.Vector;

/**
 * <p>Title: 高级算法</p>
 * <p>Description: 包括遗传算法和SCE算法</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: 大连理工大学水利信息研究所</p>
 * @author 廖胜利
 * @version 1.0
 */

public class Network {

    private static final int BIGNUMBER = 1000000;
    private Random rand = new Random();

    private char index_ActiveFun = '0';
    private boolean trainRandomMode;

    private int[] order;
    private int n_input;
    private int n_output;
    private int n_layers;

    private Layer[] hideLayers;
    private Layer inputLayer;
    private Layer outputLayer;

    private double alfa;
    private double lph;
    private double lpo;

    private double[] inputs;
    private double[] desireds;

    private double error_total;
    private double error_total_one_circle_all;
    private double error_compared_to_tolerance;
    private double error_total_one_circle;


    public Network(double[][] n_input,double[] n_output,int n_hideLayers){
    }

    public void setParamters(double alfa,double lph,double lpo){
        this.lph = lph;
        this.lpo = lpo;
        this.alfa = alfa;
    }

    public void firstTimeSetting(){
        for (int i = 0 ;i< n_layers;i++)
          for (int j = 0 ;j< hideLayers[i].n_nodes ;j++)
            hideLayers[i].nodes[j].threshold = -1.0 * rand.nextDouble()*BIGNUMBER / BIGNUMBER;

        for (int i = 0 ;i< n_output ;i++)
          outputLayer.nodes[i].threshold = -1.0 * rand.nextDouble()*BIGNUMBER / BIGNUMBER;

    }

    public void beforeTraining(double[] inputs,double[] desireds){
        for(int i=0;i<inputs.length;i++)
            inputLayer.nodes[i].activation = inputs[i];

        this.desireds = desireds;
    }

    public void calActivation(){
        for (int i = 0; i < n_layers; i++) {
            if (i == 0) {
                for (int j = 0; j < hideLayers[i].n_nodes; j++) {
                    hideLayers[i].nodes[j].activation = 0;

                    for (int ci = 0; ci < n_input; ci++) {
                        hideLayers[i].nodes[j].activation +=
                            inputLayer.nodes[ci].activation *
                            inputLayer.nodes[ci].weights[j];
                    }

                    hideLayers[i].nodes[j].activation += hideLayers[i].nodes[j].threshold;
                    hideLayers[i].nodes[j].activation = Activefun(hideLayers[i].
                        nodes[j].activation, index_ActiveFun);

                }
            }
            else {
                for (int j = 0; j < hideLayers[i].n_nodes; j++) {

                    hideLayers[i].nodes[j].activation = 0;
                    for (int ci = 0; ci < hideLayers[i - 1].n_nodes; ci++) {

                        hideLayers[i].nodes[j].activation +=
                            hideLayers[i -1].nodes[ci].activation
                            * hideLayers[i - 1].nodes[ci].weights[j];
                    }
                    hideLayers[i].nodes[j].activation += hideLayers[i].nodes[j].
                        threshold;
                    hideLayers[i].nodes[j].activation = Activefun(hideLayers[i].
                        nodes[j].activation, index_ActiveFun);

                }

            }
        }
    }

    public void calActivation(double[] result) {
        for (int i = 0; i < n_layers; i++) {
            if (i == 0)
                for (int j = 0; j < hideLayers[i].n_nodes; j++) {
                    hideLayers[i].nodes[j].activation = 0;
                    for (int ci = 0; ci < n_input; ci++) {
                        hideLayers[i].nodes[j].activation = hideLayers[i].nodes[
                            j].activation
                            +
                            inputLayer.nodes[ci].activation *
                            inputLayer.nodes[ci].weights[j];
                    }
                    hideLayers[i].nodes[j].activation = hideLayers[i].nodes[j].
                        activation
                        + hideLayers[i].nodes[j].threshold;
                    hideLayers[i].nodes[j].activation = Activefun(hideLayers[i].
                        nodes[j].activation, index_ActiveFun);
                }
            else
                for (int j = 0; j < hideLayers[i].n_nodes; j++) {
                    hideLayers[i].nodes[j].activation = 0;
                    for (int ci = 0; ci < hideLayers[i - 1].n_nodes; ci++) {
                        hideLayers[i].nodes[j].activation = hideLayers[i].nodes[
                            j].activation
                            + hideLayers[i -
                            1].nodes[ci].activation * hideLayers[i -
                            1].nodes[ci].weights[j];
                    }
                    hideLayers[i].nodes[j].activation = hideLayers[i].nodes[j].
                        activation
                        + hideLayers[i].nodes[j].threshold;
                    hideLayers[i].nodes[j].activation = Activefun(hideLayers[i].
                        nodes[j].activation, index_ActiveFun);

                }
        }

        for (int j = 0; j < outputLayer.n_nodes; j++) {
            outputLayer.nodes[j].activation = 0;
            for (int ci = 0; ci < hideLayers[n_layers - 1].n_nodes; ci++) {
                outputLayer.nodes[j].activation +=
                    hideLayers[n_layers -1].nodes[ci].activation
                    * hideLayers[n_layers -1].nodes[ci].weights[j];
            }
            outputLayer.nodes[j].activation += outputLayer.nodes[j].threshold;
            outputLayer.nodes[j].activation = Activefun(outputLayer.nodes[j].
                activation, index_ActiveFun);
        }

        for (int i = 0; i < n_output; i++)
            result[i] = outputLayer.nodes[i].activation;

    }

    public void calErrorOutput() {
        for (int i = 0; i < n_output; i++) {
            outputLayer.nodes[i].error = outputLayer.nodes[i].activation -
                desireds[i];
            outputLayer.nodes[i].error *=
                difActivefun(outputLayer.nodes[i].activation, index_ActiveFun);
        }

    }

    public void calErrorHide() {
        for (int j = 0; j < hideLayers[n_layers - 1].n_nodes; j++) {
            hideLayers[n_layers - 1].nodes[j].error = 0;
            for (int x = 0; x < n_output; x++) {
                hideLayers[n_layers - 1].nodes[j].error +=
                    hideLayers[n_layers -1].nodes[j].weights[x] * outputLayer.nodes[x].error;
            }
            hideLayers[n_layers - 1].nodes[j].error *=
                difActivefun(hideLayers[n_layers - 1].nodes[j].activation, index_ActiveFun);
        }

        for (int i = n_layers - 2; i >= 0; i--)
            for (int j = 0; j < hideLayers[i].n_nodes; j++) {
                hideLayers[i].nodes[j].error = 0;
                for (int x = 0; x < hideLayers[i + 1].n_nodes; x++) {
                    hideLayers[i].nodes[j].error +=
                        hideLayers[i].nodes[j].weights[x] * hideLayers[i + 1].nodes[x].error;
                }
                hideLayers[i].nodes[j].error *=
                    difActivefun(hideLayers[i].nodes[j].activation, index_ActiveFun);
            }

        for (int j = 0; j < inputLayer.n_nodes; j++) {
            inputLayer.nodes[j].error = 0;
            for (int x = 0; x < hideLayers[0].n_nodes; x++) {
                inputLayer.nodes[j].error +=
                    inputLayer.nodes[j].weights[x] * hideLayers[0].nodes[x].error;
            }
            inputLayer.nodes[j].error *= difActivefun(inputLayer.nodes[j].activation, index_ActiveFun);
        }
    }

    public void calNewThresholds() {
        double delta = 0;
        for (int i = 0; i < n_layers; i++) {
            for (int x = 0; x < hideLayers[i].n_nodes; x++) {
                delta = alfa * hideLayers[i].nodes[x].deltathresholdlast
                    + hideLayers[i].nodes[x].error * lph;
                hideLayers[i].nodes[x].deltathresholdlast = delta;
                hideLayers[i].nodes[x].threshold = hideLayers[i].nodes[x].
                    threshold + delta;
            }
        }

        for (int x = 0; x < outputLayer.n_nodes; x++) {
            delta = alfa * outputLayer.nodes[x].deltathresholdlast
                + outputLayer.nodes[x].error * lpo;
            outputLayer.nodes[x].deltathresholdlast = delta;
            outputLayer.nodes[x].threshold = outputLayer.nodes[x].threshold +
                delta;
        }
    }

    public void calNewWeightsInHides() {
        double tmp = 0;
        double delta = 0;

        for (int j = 0; j < hideLayers[n_layers - 1].n_nodes; j++) {
            tmp = hideLayers[n_layers - 1].nodes[j].activation * lpo;
            for (int x = 0; x < n_output; x++) {
                delta =
                    alfa * hideLayers[n_layers -1].nodes[j].deltaweightslast[x]
                    + tmp * outputLayer.nodes[x].error;
                hideLayers[n_layers - 1].nodes[j].deltaweightslast[x] = delta;
                hideLayers[n_layers - 1].nodes[j].weights[x] =
                    hideLayers[n_layers - 1].nodes[j].weights[x] + delta;
            }
        }

        for (int i = 0; i < n_layers - 1; i++) {
            for (int j = 0; j < hideLayers[i].n_nodes; j++) {
                tmp = hideLayers[i].nodes[j].activation * lph;
                for (int x = 0; x < hideLayers[i + 1].n_nodes - 1; x++) {
                    delta = alfa * hideLayers[i].nodes[j].deltaweightslast[x]
                        + tmp * hideLayers[i + 1].nodes[x].error;
                    hideLayers[i].nodes[j].deltaweightslast[x] = delta;
                    hideLayers[i].nodes[j].weights[x] =
                        hideLayers[i].nodes[j].weights[x] + delta;
                }
            }
        }

    }

    public void calNewWeightsInInputs(){
        double tmp = 0;
        double delta = 0;
        for(int i=0;i<n_input;i++){
            tmp = inputLayer.nodes[i].activation*lph;
            for(int x=0;x<hideLayers[0].n_nodes;x++){
                delta = alfa*inputLayer.nodes[i].deltaweightslast[x]
                    +tmp*hideLayers[0].nodes[x].error;
                inputLayer.nodes[i].weights[x] = inputLayer.nodes[i].weights[x]+delta;
            }
        }
    }

    public void calErrorTotalOneCircle(){

    }

    public void resetErrorTotal(){

    }

    public void trainingOnePattern(){

    }

    public void trainingOnePattern(double[] result){

    }

    public void training(double[][] inputs,double[][] outputs,int maxtime){
        if(order==null)
            order = new int[0];
    }

    public void destory(){

    }

    public double Activefun(double input,int index_fun){
        return 0;
    }

    public double difActivefun(double input,int index_fun){
        return 0;
    }


    static class ActiveFunctions{

    }

    class Layer{
        private int n_nodes;
        private Node[] nodes;

        Layer(){

        }

        Layer(int n_nodes){
            this.n_nodes = n_nodes;
            nodes = new Node[n_nodes];

            for(int i=0;i<n_nodes;i++)
                nodes[i] = new Node();
        }
    }

    class Node{
        private double activation;
        private double threshold;
        private double[] weights;
        private double[] deltaweightslast;
        private double deltathresholdlast;
        private double error;
        private int n_weights;

        Node(){
            activation = 0;
            threshold = 0;
            error = 0;
        }

        Node(int n_weights){
            this.n_weights = n_weights;

            this.weights = new double[n_weights];
            this.deltaweightslast = new double[n_weights];
            this.deltathresholdlast = 0;
            this.error = 0;

            for(int i=0;i<n_weights;i++){
                this.weights[i] = 2*rand.nextDouble()*BIGNUMBER;
                this.deltaweightslast[i] = 0;
            }
            this.threshold = 2*rand.nextDouble()*BIGNUMBER;
        }

        Node(double activation,double threshold,int n_weights){
            this.n_weights = n_weights;
            this.activation = activation;
            this.threshold = threshold;

            weights = new double[n_weights];
        }

    }












}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -