先参考一篇关于有监督的Hebb学习的博文:
http://blog.csdn.net/laviewpbt/article/details/1382491
该系列的博文主要介绍的是简单Hebb学习的原理还有如何用Matlab来实现.
这个是我在PPT上简单介绍的原理图
可以看到实现联想器的关键就是权值矩阵W的计算.
下面是实现代码(java语言实现,矩阵类采用开源的jama包)
//利用hebb规则求W public static Matrix heddGetW(List<Matrix> inMatrixlist,List<Matrix> outMatrixlist){ int row = inMatrixlist.get(0).getRowDimension(); Matrix w = new Matrix(row,row,0); for(int i = 0;i < inMatrixlist.size();i++){ //即W=TP w = w.plus(inMatrixlist.get(i).times(outMatrixlist.get(i).transpose())); } return w; } //利用仿逆规则求W public static Matrix heddGetWinFangni(List<Matrix> inMatrixlist,List<Matrix> outMatrixlist){ int row = inMatrixlist.get(0).getRowDimension(); Matrix w = new Matrix(row,row,0); for(int i = 0;i < inMatrixlist.size();i++){ //计算P+ Matrix pp = inMatrixlist.get(i).transpose() .times(inMatrixlist.get(i)).inverse() .times(inMatrixlist.get(i).transpose()); //计算W=TP+ w = w.plus(outMatrixlist.get(i).times(pp)); } return w; }
//计算输出 public static Matrix heddGetA(Matrix p,Matrix w){ //计算WP Matrix a = w.times(p); //hardlims函数:即计算hardlims(WP) for(int row = 0 ; row < a.getRowDimension() ; row++){ for(int col = 0 ; col < a.getColumnDimension() ; col++){ if(a.get(row,col) < 0){ a.set(row, col, -1); }else{ a.set(row, col, 1); } } } return a; }
两个重要的工具函数,用于转换数组和矩阵
public static Matrix arrayToMatrix(double[][] array,int row,int col){ Matrix mat = new Matrix(row*col,1); int k = 0; for(int i = 0; i < col; i++ ){ for(int j = 0; j < row; j++ ){ mat.set(k , 0, array[j][i]); k++; } } return mat; } public static double[][] MatrixToArray(Matrix mat,int row,int col){ double[][] array = new double[row][col]; int k = 0; for(int i = 0; i < col; i++ ){ for(int j = 0; j < row; j++ ){ array[j][i] = mat.get(k , 0); k++; } } return array; }
关于这个效果的实现请看接下来的博文
http://blog.csdn.net/suren__123/article/details/8099250