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

📄 neuron.java

📁 用java实现的神经网络bp的核心算法
💻 JAVA
字号:
/*
 * Neuron.java
 *
 * Created on 2007年10月25日, 上午8:44
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package neuralNetwork;
import java.util.*;
import java.io.*;
/**
 *This is a neuron.
 * @author yuhui_bear
 */
public  class Neuron implements Serializable{
    public Dendrite[] dendrites;
    public ArrayList<Dendrite>  sub;
    public Dendrite[] subDen;
    public double grad =0;    
    private int count =0;
    private double vk =0 , out =0;
    private int length =0;
    /**
     * half absolute output range of network.
     */
    private final float a =1.716F;
    private final float b = 0.6F;
    /**
     * Creates a new instance of Neuron 
     * 
     */
    public Neuron() {
        sub = new ArrayList();
    }   
    /**
     * Creates a new instance of Neuron 
     * 
     * @param w array of weight.
     * @param ct serial number of the neuron.
     */
    public Neuron(double[] w ,int ct) {
        count = ct;
        length = w.length;
        dendrites = new Dendrite[length];
        sub = new ArrayList();
         for ( int i = 0 ; i< length; i++) {
            dendrites[i] = new Dendrite(w[i]);
            dendrites[i].out=this;
         }
    }
     
    public void convertToSubArray(){
        subDen = new Dendrite[sub.size()];
        for (int i = 0 ; i<subDen.length;i++){
            subDen[i] = sub.get(i);
        }
    }
    /**
     * entrence of input data .
     * @param inArray input data.
     */
    public void input(double[] inArray) {
        vk=0;
        for (int i = 0 ; i<inArray.length; i++){
            vk = vk + dendrites[i+1].weight * inArray[i];
        }
        vk += (-1) * dendrites[0].weight;
        out = activeFunction(vk);
    }    
    /**
     * return output value.
     * @return output data of network.
     */
    public double output(){        
        return out;
    }    
    public double vk(){
        return vk;
    }
    /** update weight **/
    public void setWeight(double step , double aw){
        for (int i =1;i<dendrites.length;i++){
            dendrites[i].weight +=  step * grad * dendrites[i].in.output() - aw * dendrites[i].preDifWeight;
            dendrites[i].preDifWeight = step * grad * dendrites[i].in.output();
        }
        dendrites[0].weight +=  step * grad * (-1) - aw * dendrites[0].preDifWeight;
        dendrites[0].preDifWeight = step * grad * (-1);
    }
    /** for test only**/
    public float[] getWeight(){
        float[] tempW =new float[length];
        for (int i = 0 ; i < length;i++){
            tempW[i] = (float)dendrites[i].weight;
        }
        return tempW;
    }
    /**
     * calculate output
     * @param  v , summary of  input
     * @return output
     */
    private double activeFunction(double v){
//        double t =  2*a/(1+Math.pow(Math.E,(-b*v)))-a;
//                2*16/(1+2.718^(-0.4*x))-16
        return 1/(1+ Math.pow (Math.E,-v));
//        return 2*a/(1+Math.pow(Math.E,(-b*v)))-a;
    }   

    public String toString(){
        String str =new String();
        str = "[" + count +"],out," + out + ",grad," + grad +",vk,"+vk +",weight,";
        for (int i =0; i < dendrites.length;i++){
            str += dendrites[i].weight + ",";
        }
        return str;
    }
    /**
     * get exclusive number (ID of the neuron).
     * @return ID of the neuron.
     */
    public int getIDNumber(){
        return count;
    }
}

⌨️ 快捷键说明

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