bp神经网络java代码_BP神经网络的Java实现

本文分享了一段Java实现的BP神经网络代码,包括构造函数、训练和测试方法。作者提供了一个测试用例,用于判断输入整数的奇偶性和正负性。代码中包含了学习率、动量等参数,并通过随机数初始化权重。读者讨论了代码中的阈值、权值初始化和误差计算方法等细节问题。
摘要由CSDN通过智能技术生成

课程作业要求实现一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值