多层前馈神经元网络(一)

2014-11-24 11:30:19 · 作者: · 浏览: 19
做了一个神经元网络分类器。开始步长设置为迭代次数的倒数,效果不好;后来调整到 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 TRAINED
private Status status;
// depth of network, layer 0 is input layer
private int depth;
// neurodes in each layer
private Neurode[][] neurodes;
// weights[i] is a two dimensional array, representing weights between layer i and layer 1+1
private 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 neurodes
neurodes = new Neurode[depth][];
for ( int d=0; d
neurodes[d] = new Neurode[numNeurodes[d]];
for ( int i=0; i
neurodes[d][i] = new Neurode();
neurodes[d][i].theta = Math.random();
}
}
// initialize weights
weights = new double[depth][][];
for ( int d=0; d
weights[d] = new double[numNeurodes[d]][numNeurodes[d+1]];
for ( int i=0; i
for ( int j=0; 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 0
for (int i=0; i
neurodes[0][i].output = data[i];
}
// calculate output for each output layer
for ( int d=1; d
for ( int j=0; j
double input = 0.0;
for ( int i=0; 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
output[i] = neurodes[depth-1][i].output;
if ( x < output[i] ) {
x = output[i];
label = i;
}
}
return label;
}
/**
* Train the neuronetwork
*
* @param data : input matrix of train data, with data[i] represents the ith sample
* @param target : input matrix of train label, with target[i] represen