请使用Java代码写一个预测价格的BP神经网络算法代码,并将输出的值缩放回原始的量级范围
时间: 2024-03-13 11:43:51 浏览: 61
基于BP神经网络实现电价预测附matlab代码
5星 · 资源好评率100%
以下是一个简单的Java代码实现BP神经网络算法预测价格,并将输出的值缩放回原始的量级范围:
```java
import java.util.Random;
public class BPNeuralNetwork {
private int inputNum; // 输入层节点数
private int hiddenNum; // 隐藏层节点数
private int outputNum; // 输出层节点数
private double[][] inputHiddenWeight; // 输入层到隐藏层的权值矩阵
private double[][] hiddenOutputWeight; // 隐藏层到输出层的权值矩阵
private double[] hiddenThreshold; // 隐藏层阈值
private double[] outputThreshold; // 输出层阈值
private double[] hiddenValue; // 隐藏层节点输出值
private double[] outputValue; // 输出层节点输出值
private double[] hiddenError; // 隐藏层节点误差
private double[] outputError; // 输出层节点误差
private double learningRate = 0.1; // 学习率
private double momentum = 0.5; // 冲量
public BPNeuralNetwork(int inputNum, int hiddenNum, int outputNum) {
this.inputNum = inputNum;
this.hiddenNum = hiddenNum;
this.outputNum = outputNum;
inputHiddenWeight = new double[inputNum][hiddenNum];
hiddenOutputWeight = new double[hiddenNum][outputNum];
hiddenThreshold = new double[hiddenNum];
outputThreshold = new double[outputNum];
hiddenValue = new double[hiddenNum];
outputValue = new double[outputNum];
hiddenError = new double[hiddenNum];
outputError = new double[outputNum];
initWeights();
}
private void initWeights() { // 初始化权值和阈值
Random rand = new Random();
for (int i = 0; i < inputNum; i++) {
for (int j = 0; j < hiddenNum; j++) {
inputHiddenWeight[i][j] = rand.nextDouble() - 0.5; // 随机初始化权值
}
}
for (int i = 0; i < hiddenNum; i++) {
for (int j = 0; j < outputNum; j++) {
hiddenOutputWeight[i][j] = rand.nextDouble() - 0.5; // 随机初始化权值
}
hiddenThreshold[i] = rand.nextDouble() - 0.5; // 随机初始化阈值
}
for (int i = 0; i < outputNum; i++) {
outputThreshold[i] = rand.nextDouble() - 0.5; // 随机初始化阈值
}
}
private double sigmoid(double x) { // Sigmoid激活函数
return 1.0 / (1.0 + Math.exp(-x));
}
private double[] predict(double[] input) { // 前向传播预测输出
for (int i = 0; i < hiddenNum; i++) {
hiddenValue[i] = 0.0;
for (int j = 0; j < inputNum; j++) {
hiddenValue[i] += input[j] * inputHiddenWeight[j][i];
}
hiddenValue[i] = sigmoid(hiddenValue[i] - hiddenThreshold[i]);
}
for (int i = 0; i < outputNum; i++) {
outputValue[i] = 0.0;
for (int j = 0; j < hiddenNum; j++) {
outputValue[i] += hiddenValue[j] * hiddenOutputWeight[j][i];
}
outputValue[i] = sigmoid(outputValue[i] - outputThreshold[i]);
}
return outputValue;
}
private void train(double[] input, double[] targetOutput) { // BP算法训练网络
predict(input); // 前向传播预测输出
for (int i = 0; i < outputNum; i++) {
outputError[i] = (targetOutput[i] - outputValue[i]) * outputValue[i] * (1.0 - outputValue[i]); // 输出层误差
}
for (int i = 0; i < hiddenNum; i++) {
hiddenError[i] = 0.0;
for (int j = 0; j < outputNum; j++) {
hiddenError[i] += outputError[j] * hiddenOutputWeight[i][j];
}
hiddenError[i] *= hiddenValue[i] * (1.0 - hiddenValue[i]); // 隐藏层误差
}
for (int i = 0; i < hiddenNum; i++) {
for (int j = 0; j < outputNum; j++) {
double delta = learningRate * outputError[j] * hiddenValue[i] + momentum * hiddenOutputWeight[i][j];
hiddenOutputWeight[i][j] += delta; // 更新权值
}
}
for (int i = 0; i < inputNum; i++) {
for (int j = 0; j < hiddenNum; j++) {
double delta = learningRate * hiddenError[j] * input[i] + momentum * inputHiddenWeight[i][j];
inputHiddenWeight[i][j] += delta; // 更新权值
}
}
for (int i = 0; i < outputNum; i++) {
outputThreshold[i] -= learningRate * outputError[i]; // 更新阈值
}
for (int i = 0; i < hiddenNum; i++) {
hiddenThreshold[i] -= learningRate * hiddenError[i]; // 更新阈值
}
}
public double[] scale(double[] input, double minValue, double maxValue) { // 缩放输入值到[0,1]范围内
double[] scaledInput = new double[inputNum];
for (int i = 0; i < inputNum; i++) {
scaledInput[i] = (input[i] - minValue) / (maxValue - minValue);
}
return scaledInput;
}
public double[] unscale(double[] output, double minValue, double maxValue) { // 将输出值缩放回原始的量级范围
double[] unscaledOutput = new double[outputNum];
for (int i = 0; i < outputNum; i++) {
unscaledOutput[i] = output[i] * (maxValue - minValue) + minValue;
}
return unscaledOutput;
}
}
```
使用方法示例:
```java
public class BPNeuralNetworkTest {
public static void main(String[] args) {
double[][] input = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}}; // 输入样本
double[] targetOutput = {10.0, 20.0, 30.0}; // 目标输出
BPNeuralNetwork net = new BPNeuralNetwork(3, 4, 1); // 创建BP神经网络
double minValue = 1.0; // 输入值最小值
double maxValue = 9.0; // 输入值最大值
for (int i = 0; i < 1000; i++) { // 训练网络
for (int j = 0; j < input.length; j++) {
double[] scaledInput = net.scale(input[j], minValue, maxValue); // 缩放输入值到[0,1]范围内
double scaledTargetOutput = (targetOutput[j] - minValue) / (maxValue - minValue); // 缩放目标输出到[0,1]范围内
net.train(scaledInput, new double[]{scaledTargetOutput});
}
}
double[] testInput = {2.5, 4.5, 6.5}; // 测试输入
double[] scaledTestInput = net.scale(testInput, minValue, maxValue); // 缩放测试输入值到[0,1]范围内
double[] scaledTestOutput = net.predict(scaledTestInput); // 预测输出值
double[] testOutput = net.unscale(scaledTestOutput, minValue, maxValue); // 将输出值缩放回原始的量级范围
System.out.println(Arrays.toString(testOutput)); // 输出预测结果
}
}
```
阅读全文