http://fantasticinblur.iteye.com/blog/1465497
课程作业要求实现一个BPNN。这次尝试使用Java实现了一个。现共享之。版权属于大家。关于BPNN的原理,就不赘述了。
下面是BPNN的实现代码。类名为BP。
- package ml;
-
- import java.util.Random;
-
- public class BP {
-
- private final double[] input;
-
- private final double[] hidden;
-
- private final double[] output;
-
- private final double[] target;
-
-
- private final double[] hidDelta;
-
- private final double[] optDelta;
-
-
- private final double eta;
-
- private final double momentum;
-
-
- private final double[][] iptHidWeights;
-
- private final double[][] hidOptWeights;
-
-
- private final double[][] iptHidPrevUptWeights;
-
- private final double[][] hidOptPrevUptWeights;
-
- public double optErrSum = 0d;
-
- public double hidErrSum = 0d;
-
- private final Random random;
-
-
- 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;
- }
- }
-
-
- public BP(int inputSize, int hiddenSize, int outputSize) {
- this(inputSize, hiddenSize, outputSize, 0.25, 0.9);
- }
-
-
- public void train(double[] trainData, double[] target) {
- loadInput(trainData);
- loadTarget(target);
- forward();
- calculateDelta();
- adjustWeight();
- }
-
-
- public double[] test(double[] inData) {
- if (inData.length != input.length - 1) {
- throw new IllegalArgumentException("Size Do Not Match.");
- }
- System.arraycopy(inData, 0, input, 1, inData.length);
- forward();
- return getNetworkOutput();
- }
-
-
- private double[] getNetworkOutput() {
- int len = output.length;
- double[] temp = new double[len - 1];
- for (int i = 1; i != len; i++)
- temp[i - 1] = output[i];
- return temp;
- }
-
-
- private void loadTarget(double[] arg) {
- if (arg.length != target.length - 1) {
- throw new IllegalArgumentException("Size Do Not Match.");
- }
- System.arraycopy(arg, 0, target, 1, arg.length);
- }
-
-
- private void loadInput(double[] inData) {
- if (inData.length != input.length - 1) {
- throw new IllegalArgumentException("Size Do Not Match.");
- }
- System.arraycopy(inData, 0, input, 1, inData.length);
- }
-
-
- private void forward(double[] layer0, double[] layer1, double[][] weight) {
-
- layer0[0] = 1.0;
- for (int j = 1, len = layer1.length; j != len; ++j) {
- double sum = 0;
- for (int i = 0, len2 = layer0.length; i != len2; ++i)
- sum += weight[i][j] * layer0[i];
- layer1[j] = sigmoid(sum);
- }
- }
-
-
- private void forward() {
- forward(input, hidden, iptHidWeights);
- forward(hidden, output, hidOptWeights);
- }
-
-
- private void outputErr() {
- double errSum = 0;
- for (int idx = 1, len = optDelta.length; idx != len; ++idx) {
- double o = output[idx];
- optDelta[idx] = o * (1d - o) * (target[idx] - o);
- errSum += Math.abs(optDelta[idx]);
- }
- optErrSum = errSum;
- }
-
-
- private void hiddenErr() {
- double errSum = 0;
- for (int j = 1, len = hidDelta.length; j != len; ++j) {
- double o = hidden[j];
- double sum = 0;
- for (int k = 1, len2 = optDelta.length; k != len2; ++k)
- sum += hidOptWeights[j][k] * optDelta[k];
- hidDelta[j] = o * (1d - o) * sum;
- errSum += Math.abs(hidDelta[j]);
- }
- hidErrSum = errSum;
- }
-
-
- private void calculateDelta() {
- outputErr();
- hiddenErr();
- }
-
-
- private void adjustWeight(double[] delta, double[] layer,
- double[][] weight, double[][] prevWeight) {
-
- layer[0] = 1;
- for (int i = 1, len = delta.length; i != len; ++i) {
- for (int j = 0, len2 = layer.length; j != len2; ++j) {
- double newVal = momentum * prevWeight[j][i] + eta * delta[i]
- * layer[j];
- weight[j][i] += newVal;
- prevWeight[j][i] = newVal;
- }
- }
- }
-
-
- private void adjustWeight() {
- adjustWeight(optDelta, hidden, hidOptWeights, hidOptPrevUptWeights);
- adjustWeight(hidDelta, input, iptHidWeights, iptHidPrevUptWeights);
- }
-
-
- private double sigmoid(double val) {
- return 1d / (1d + Math.exp(-val));
- }
- }
为了验证正确性,我写了一个测试用例,目的是对于任意的整数(int型),BPNN在经过训练之后,能够准确地判断出它是奇数还是偶数,正数还是负数。首先对于训练的样本(是随机生成的数字),将它转化为一个32位的向量,向量的每个分量就是其二进制形式对应的位上的0或1。将目标输出视作一个4维的向量,[1,0,0,0]代表正奇数,[0,1,0,0]代表正偶数,[0,0,1,0]代表负奇数,[0,0,0,1]代表负偶数。
训练样本为1000个,学习200次。
- package ml;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
-
- public class Test {
-
-
- public static void main(String[] args) throws IOException {
- BP bp = new BP(32, 15, 4);
-
- Random random = new Random();
- List<Integer> list = new ArrayList<Integer>();
- for (int i = 0; i != 1000; i++) {
- int value = random.nextInt();
- list.add(value);
- }
-
- for (int i = 0; i != 200; i++) {
- for (int value : list) {
- double[] real = new double[4];
- if (value >= 0)
- if ((value & 1) == 1)
- real[0] = 1;
- else
- real[1] = 1;
- else if ((value & 1) == 1)
- real[2] = 1;
- else
- real[3] = 1;
- double[] binary = new double[32];
- int index = 31;
- do {
- binary[index--] = (value & 1);
- value >>>= 1;
- } while (value != 0);
-
- bp.train(binary, real);
- }
- }
-
- System.out.println("训练完毕,下面请输入一个任意数字,神经网络将自动判断它是正数还是复数,奇数还是偶数。");
-
- while (true) {
- byte[] input = new byte[10];
- System.in.read(input);
- Integer value = Integer.parseInt(new String(input).trim());
- int rawVal = value;
- double[] binary = new double[32];
- int index = 31;
- do {
- binary[index--] = (value & 1);
- value >>>= 1;
- } while (value != 0);
-
- double[] result = bp.test(binary);
-
- double max = -Integer.MIN_VALUE;
- int idx = -1;
-
- for (int i = 0; i != result.length; i++) {
- if (result[i] > max) {
- max = result[i];
- idx = i;
- }
- }
-
- switch (idx) {
- case 0:
- System.out.format("%d是一个正奇数\n", rawVal);
- break;
- case 1:
- System.out.format("%d是一个正偶数\n", rawVal);
- break;
- case 2:
- System.out.format("%d是一个负奇数\n", rawVal);
- break;
- case 3:
- System.out.format("%d是一个负偶数\n", rawVal);
- break;
- }
- }
- }
-
- }
运行结果截图如下:

这个测试的例子非常简单。大家可以根据自己的需要去使用BP这个类。
BP神经网络的Java实现(转)
原文:http://www.cnblogs.com/bnuvincent/p/6476040.html