bp神经网络鸢尾花java
时间: 2023-08-26 08:07:07 浏览: 128
Python 基于BP神经网络实现鸢尾花的分类+源代码+文档说明
好的,以下是使用Java实现BP神经网络进行鸢尾花分类的示例代码:
首先,我们需要定义一个BP神经网络类,包含输入层、隐藏层和输出层,并实现前向传播和反向传播算法。
```java
public class BPNeuralNetwork {
private double[][] inputLayer;
private double[][] hiddenLayer;
private double[][] outputLayer;
private double[][] inputWeights;
private double[][] hiddenWeights;
private double[] inputBias;
private double[] hiddenBias;
private double learningRate;
private double momentum;
public BPNeuralNetwork(int inputNodes, int hiddenNodes, int outputNodes, double learningRate, double momentum) {
this.inputLayer = new double[1][inputNodes];
this.hiddenLayer = new double[1][hiddenNodes];
this.outputLayer = new double[1][outputNodes];
this.inputWeights = new double[inputNodes][hiddenNodes];
this.hiddenWeights = new double[hiddenNodes][outputNodes];
this.inputBias = new double[hiddenNodes];
this.hiddenBias = new double[outputNodes];
this.learningRate = learningRate;
this.momentum = momentum;
Random rand = new Random();
for (int i = 0; i < inputNodes; i++) {
for (int j = 0; j < hiddenNodes; j++) {
inputWeights[i][j] = rand.nextDouble() - 0.5;
}
}
for (int i = 0; i < hiddenNodes; i++) {
for (int j = 0; j < outputNodes; j++) {
hiddenWeights[i][j] = rand.nextDouble() - 0.5;
}
}
for (int i = 0; i < hiddenNodes; i++) {
inputBias[i] = rand.nextDouble() - 0.5;
}
for (int i = 0; i < outputNodes; i++) {
hiddenBias[i] = rand.nextDouble() - 0.5;
}
}
public double sigmoid(double x) {
return 1 / (1 + Math.exp(-x));
}
public double sigmoidDerivative(double x) {
return x * (1 - x);
}
public double[][] forwardPropagation(double[][] inputs) {
inputLayer = inputs;
for (int i = 0; i < hiddenLayer[0].length; i++) {
double sum = 0;
for (int j = 0; j < inputLayer[0].length; j++) {
sum += inputLayer[0][j] * inputWeights[j][i];
}
hiddenLayer[0][i] = sigmoid(sum + inputBias[i]);
}
for (int i = 0; i < outputLayer[0].length; i++) {
double sum = 0;
for (int j = 0; j < hiddenLayer[0].length; j++) {
sum += hiddenLayer[0][j] * hiddenWeights[j][i];
}
outputLayer[0][i] = sigmoid(sum + hiddenBias[i]);
}
return outputLayer;
}
public void backPropagation(double[][] inputs, double[][] targets) {
double[][] outputErrors = new double[1][outputLayer[0].length];
double[][] hiddenErrors = new double[1][hiddenLayer[0].length];
for (int i = 0; i < outputLayer[0].length; i++) {
outputErrors[0][i] = (targets[0][i] - outputLayer[0][i]) * sigmoidDerivative(outputLayer[0][i]);
}
for (int i = 0; i < hiddenLayer[0].length; i++) {
double sum = 0;
for (int j = 0; j < outputLayer[0].length; j++) {
sum += outputErrors[0][j] * hiddenWeights[i][j];
}
hiddenErrors[0][i] = sum * sigmoidDerivative(hiddenLayer[0][i]);
}
for (int i = 0; i < inputLayer[0].length; i++) {
for (int j = 0; j < hiddenLayer[0].length; j++) {
double weightDelta = learningRate * hiddenErrors[0][j] * inputLayer[0][i] + momentum * inputWeights[i][j];
inputWeights[i][j] += weightDelta;
}
}
for (int i = 0; i < hiddenLayer[0].length; i++) {
for (int j = 0; j < outputLayer[0].length; j++) {
double weightDelta = learningRate * outputErrors[0][j] * hiddenLayer[0][i] + momentum * hiddenWeights[i][j];
hiddenWeights[i][j] += weightDelta;
}
}
for (int i = 0; i < hiddenLayer[0].length; i++) {
double biasDelta = learningRate * hiddenErrors[0][i];
inputBias[i] += biasDelta;
}
for (int i = 0; i < outputLayer[0].length; i++) {
double biasDelta = learningRate * outputErrors[0][i];
hiddenBias[i] += biasDelta;
}
}
}
```
然后,我们需要读取鸢尾花数据集并进行预处理。
```java
public class IrisData {
private double[][] inputs;
private double[][] targets;
public double[][] getInputs() {
return inputs;
}
public double[][] getTargets() {
return targets;
}
public IrisData(String filename) {
ArrayList<double[]> inputsList = new ArrayList<>();
ArrayList<double[]> targetsList = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
String[] values = line.split(",");
double[] input = new double[4];
input[0] = Double.parseDouble(values[0]);
input[1] = Double.parseDouble(values[1]);
input[2] = Double.parseDouble(values[2]);
input[3] = Double.parseDouble(values[3]);
inputsList.add(input);
double[] target = new double[3];
if (values[4].equals("Iris-setosa")) {
target[0] = 1;
} else if (values[4].equals("Iris-versicolor")) {
target[1] = 1;
} else if (values[4].equals("Iris-virginica")) {
target[2] = 1;
}
targetsList.add(target);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
inputs = new double[inputsList.size()][4];
targets = new double[targetsList.size()][3];
for (int i = 0; i < inputsList.size(); i++) {
inputs[i] = inputsList.get(i);
targets[i] = targetsList.get(i);
}
}
}
```
最后,我们可以使用BP神经网络进行训练和预测。
```java
public class IrisClassifier {
public static void main(String[] args) {
IrisData data = new IrisData("iris.csv");
BPNeuralNetwork network = new BPNeuralNetwork(4, 5, 3, 0.1, 0.9);
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < data.getInputs().length; j++) {
double[][] inputs = new double[1][4];
inputs[0] = data.getInputs()[j];
double[][] targets = new double[1][3];
targets[0] = data.getTargets()[j];
network.forwardPropagation(inputs);
network.backPropagation(inputs, targets);
}
}
double[][] inputs = new double[1][4];
inputs[0][0] = 5.1;
inputs[0][1] = 3.5;
inputs[0][2] = 1.4;
inputs[0][3] = 0.2;
double[][] outputs = network.forwardPropagation(inputs);
System.out.println(Arrays.toString(outputs[0]));
}
}
```
这个示例代码可以将输入的四个特征值作为神经网络的输入,输出三个类别中每个类别的概率。你可以根据自己的需要进行调整和修改。
阅读全文