机器学习01之基本概念及感知器

来源:互联网 发布:软件开发生命周期模型 编辑:程序博客网 时间:2024/06/05 06:22

    转载自: http://www.cnblogs.com/hesi/p/7149678.html


public class perception {         public float[] weigth;//权值         public float[][] x;//输入值         public int[] y;//样本的真实类标         public float rate;//学习数率,决定每一次循环训练中所产生的权值变化;         public float[] output;//输出的类标         public float b=0;//阈值,也称为偏差     /**     * 实例化感知器     * @param x 输入的数据     * @param d  学习速率     */    public perception(float[][] x, float d,int[] y) {        super();        this.x = x;//输入数据        this.rate = d;//学习数率        this.y=y;//样本的真实类标        weigth=new float[x[0].length];//初始化权值数组        randomWeigth(x[0].length);//随机给权值赋值    }    /**     * 给权值进行赋值,初始值为0     * @param n 权值数组的大小     */    public void randomWeigth(int n){                // Random random = new Random();        for(int i=0;i<n;i++){            weigth[i]=0;        }    }     /**     * 训练感知器:计算出误差,然后进行权值更新     */    public void train(){        output=new float[x.length];        //获取输出值        for(int i=0;i<x.length;i++){            output[i]=getoutput(x[i]);        }        //更新        for(int i=0;i<output.length;i++){            float update=rate*(y[i]-output[i]);            //更新权重            for(int j=0;j<weigth.length;j++){                weigth[j]=weigth[j]+update*x[i][j];            }            //更新偏差            b=b+update;        }                      }    //计算输出值    public int getoutput(float[] x){        int output;//输出值                //计算净输入        float z = 0;        for(int i=0;i<x.length;i++){            z+=x[i]*weigth[i];        }                //激励函数        if(z>=b)            output=1;        else            output=-1;        return output;    }      }




原创粉丝点击