多层前馈神经元网络
做了一个神经元网络分类器。开始步长设置为迭代次数的倒数,效果不好;后来调整到 0.2 效果比较好。测试一个抛物线边界的例子,准确率大约 96% 以上。[java]public final class NeuroNetwork {private static class Neurode {double err;double output;double theta;}private static enum Status {NEW,TRAINED;}// status of this class, either NEW or TRAINEDprivate Status status;// depth of network, layer 0 is input layerprivate int depth;// neurodes in each layerprivate Neurode[][] neurodes;// weights[i] is a two dimensional array, representing weights between layer i and layer 1+1private double[][][] weights;// initialize the neuronetwork/*** Initialize the neuronetwork** @param depth : the number of layers* @param numNeurodes : the number of neurodes in each layer*/public NeuroNetwork(int depth, int[] numNeurodes) {this.depth = depth;// create and initialize neurodesneurodes = new Neurode[depth][];for ( int d=0; d<depth; d++ ) {neurodes[d] = new Neurode[numNeurodes[d]];for ( int i=0; i<numNeurodes[d]; i++) {neurodes[d][i] = new Neurode();neurodes[d][i].theta = Math.random();}}// initialize weightsweights = new double[depth][][];for ( int d=0; d<depth-1; d++ ) {weights[d] = new double[numNeurodes[d]][numNeurodes[d+1]];for ( int i=0; i<numNeurodes[d]; i++) {for ( int j=0; j<numNeurodes[d+1]; j++ ) {weights[d][i][j] = Math.random();}}}status = Status.NEW;}/*** Calculate output given a input** @param data : an vector representing input*/private void calculateOutput(double[] data) {// initial output of layer 0for (int i=0; i<neurodes[0].length; i++ ) {neurodes[0][i].output = data[i];}// calculate output for each output layerfor ( int d=1; d<depth; d++ ) {for ( int j=0; j<neurodes[d].length; j++) {double input = 0.0;for ( int i=0; i<neurodes[d-1].length; i++ ) {input += neurodes[d-1][i].output*weights[d-1][i][j];}input += neurodes[d][j].theta;neurodes[d][j].output = 1.0/(1.0+Math.exp(0.0-input));}}}/*** Classify and predict** @param data : an vector represent one entry of taining sample* @param target : an vector represent class label of the training sample*/public int predict(double[] data, double[] output) {if ( data.length != neurodes[0].length || output.length != neurodes[depth-1].length ) {throw new IllegalArgumentException();}calculateOutput(data);double x = neurodes[depth-1][0].output;int label = 0;for ( int i=0; i<neurodes[depth-1].length; i++ ) {output[i] = neurodes[depth-1][i].output;if ( x < output[i] ) {x = output[i];label = i;}}return label;}/**补充:软件开发 , Java ,
上一个:Java垃圾回收机制与引用类型
下一个:排序算法--睡眠排序
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值