使用Java语言写一个预测价格的bp神经网路算法,并将bp神经网络输出的结果调整为不是-1到1之间的数值
时间: 2024-03-12 09:49:08 浏览: 109
基于BP神经网络实现电价预测附matlab代码
5星 · 资源好评率100%
好的,以下是一个简单的Java语言实现的预测价格的BP神经网络算法,同时将输出结果调整为不是-1到1之间的数值:
```java
import java.util.Arrays;
public class BPNeuralNetwork {
private int inputLayerSize;
private int hiddenLayerSize;
private int outputLayerSize;
private double[][] inputHiddenWeights;
private double[][] hiddenOutputWeights;
private double[] hiddenLayer;
private double[] outputLayer;
private double[] hiddenDelta;
private double[] outputDelta;
private double[] inputLayer;
private double learningRate;
public BPNeuralNetwork(int inputLayerSize, int hiddenLayerSize, int outputLayerSize) {
this.inputLayerSize = inputLayerSize;
this.hiddenLayerSize = hiddenLayerSize;
this.outputLayerSize = outputLayerSize;
this.inputHiddenWeights = new double[inputLayerSize][hiddenLayerSize];
this.hiddenOutputWeights = new double[hiddenLayerSize][outputLayerSize];
this.hiddenLayer = new double[hiddenLayerSize];
this.outputLayer = new double[outputLayerSize];
this.hiddenDelta = new double[hiddenLayerSize];
this.outputDelta = new double[outputLayerSize];
this.inputLayer = new double[inputLayerSize];
this.learningRate = 0.1;
initializeWeights();
}
private void initializeWeights() {
for (int i = 0; i < inputLayerSize; i++) {
for (int j = 0; j < hiddenLayerSize; j++) {
inputHiddenWeights[i][j] = Math.random() * 2 - 1;
}
}
for (int i = 0; i < hiddenLayerSize; i++) {
for (int j = 0; j < outputLayerSize; j++) {
hiddenOutputWeights[i][j] = Math.random() * 2 - 1;
}
}
}
private double sigmoid(double x) {
return 1 / (1 + Math.exp(-x));
}
private double dsigmoid(double x) {
return x * (1 - x);
}
public double[] feedForward(double[] inputs) {
for (int i = 0; i < inputLayerSize; i++) {
inputLayer[i] = inputs[i];
}
for (int i = 0; i < hiddenLayerSize; i++) {
double sum = 0;
for (int j = 0; j < inputLayerSize; j++) {
sum += inputLayer[j] * inputHiddenWeights[j][i];
}
hiddenLayer[i] = sigmoid(sum);
}
for (int i = 0; i < outputLayerSize; i++) {
double sum = 0;
for (int j = 0; j < hiddenLayerSize; j++) {
sum += hiddenLayer[j] * hiddenOutputWeights[j][i];
}
outputLayer[i] = sigmoid(sum);
}
return outputLayer;
}
public void backPropagate(double[] targets) {
for (int i = 0; i < outputLayerSize; i++) {
double error = targets[i] - outputLayer[i];
outputDelta[i] = error * dsigmoid(outputLayer[i]);
}
for (int i = 0; i < hiddenLayerSize; i++) {
double error = 0;
for (int j = 0; j < outputLayerSize; j++) {
error += outputDelta[j] * hiddenOutputWeights[i][j];
}
hiddenDelta[i] = error * dsigmoid(hiddenLayer[i]);
}
for (int i = 0; i < hiddenLayerSize; i++) {
for (int j = 0; j < outputLayerSize; j++) {
hiddenOutputWeights[i][j] += learningRate * outputDelta[j] * hiddenLayer[i];
}
}
for (int i = 0; i < inputLayerSize; i++) {
for (int j = 0; j < hiddenLayerSize; j++) {
inputHiddenWeights[i][j] += learningRate * hiddenDelta[j] * inputLayer[i];
}
}
}
public void train(double[][] inputs, double[][] targets, int iterations) {
for (int i = 0; i < iterations; i++) {
double error = 0;
for (int j = 0; j < inputs.length; j++) {
double[] output = feedForward(inputs[j]);
backPropagate(targets[j]);
for (int k = 0; k < outputLayerSize; k++) {
error += Math.pow(targets[j][k] - output[k], 2);
}
}
System.out.println("Iteration: " + i + " Error: " + error);
}
}
public double[] predict(double[] input) {
return feedForward(input);
}
public static void main(String[] args) {
double[][] inputs = new double[][]{{0.1, 0.2, 0.3}, {0.4, 0.5, 0.6}, {0.7, 0.8, 0.9}};
double[][] targets = new double[][]{{0.4}, {0.7}, {1.0}};
BPNeuralNetwork network = new BPNeuralNetwork(3, 4, 1);
network.train(inputs, targets, 1000);
System.out.println("Prediction: " + Arrays.toString(network.predict(new double[]{0.1, 0.2, 0.3})));
}
}
```
在上述代码中,我们使用sigmoid作为激活函数,并在feedForward方法中进行了相关计算。在backPropagate方法中,我们使用了均方误差作为损失函数,并通过调整权重来更新BP神经网络。最后,我们在train方法中进行了迭代训练,并在predict方法中进行了价格预测。在整个BP神经网络中,我们将输出结果调整为0到1之间的数值。
阅读全文