课程作业要求实现一个BPNN。这次尝试使用Java实现了一个。现共享之。版权属于大家。关于BPNN的原理,就不赘述了。
下面是BPNN的实现代码。类名为BP。
package ml;
import java.util.Random;
/**
* BPNN.
*
* @author RenaQiu
*
*/
public class BP {
/**
* input vector.
*/
private final double[] input;
/**
* hidden layer.
*/
private final double[] hidden;
/**
* output layer.
*/
private final double[] output;
/**
* target.
*/
private final double[] target;
/**
* delta vector of the hidden layer .
*/
private final double[] hidDelta;
/**
* output layer of the output layer.
*/
private final double[] optDelta;
/**
* learning rate.
*/
private final double eta;
/**
* momentum.
*/
private final double momentum;
/**
* weight matrix from input layer to hidden layer.
*/
private final double[][] iptHidWeights;
/**
* weight matrix from hidden layer to output layer.
*/
private final double[][] hidOptWeights;
/**
* previous weight update.
*/
private final double[][] iptHidPrevUptWeights;
/**
* previous weight update.
*/
private final double[][] hidOptPrevUptWeights;
public double optErrSum = 0d;
public double hidErrSum = 0d;
private final Random random;
/**
* Constructor.
*
* Note: The capacity of each layer will be the parameter
* plus 1. The additional unit is used for smoothness.
*
*
* @param inputSize
* @param hiddenSize
* @param outputSize
* @param eta
* @param momentum
* @param epoch
*/
public BP(int inputSize, int hiddenSize, int outputSize, double eta,
double momentum) {
input = new double[inputSize + 1];
hidden = new double[hiddenSize + 1];
output = new double[outputSize + 1];
target = new double[outputSize + 1];
hidDelta = new double[hiddenSize + 1];
optDelta = new double[outputSize + 1];
iptHidWeights = new double[inputSize + 1][hiddenSize + 1];
hidOptWeights = new double[hiddenSize + 1][outputSize + 1];
random = new Random(19881211);
randomizeWeights(iptHidWeights);
randomizeWeights(hidOptWeights);
iptHidPrevUptWeights = new double[inputSize + 1][hiddenSize + 1];
hidOptPrevUptWeights = new double[hiddenSize + 1][outputSize + 1];
this.eta = eta;
this.momentum = momentum;
}
private void randomizeWeights(double[][] matrix) {
for (int i = 0, len = matrix.length; i != len; i++)
for (int j = 0, len2 = matrix[i].length; j != len2; j++) {
double real = random.nextDouble();
matrix[i][j] = random.nextDouble() > 0.5 ? real : -real;
}
}
/**
* Constructor with default eta = 0.25 and momentum = 0.3.
*
* @param inputSize
* @param hiddenSize
* @param outputSize
* @param epoch
*/
public BP(int inputSize, int hiddenSize, int outputSize) {
this(inputSize, hiddenSize, outputSize, 0.25, 0.9);
}
/**
* Entry method. The train data should be a one-dim vector.
*
* @param trainD