BP神经网络模型C语言实现
时间: 2023-06-26 18:05:52 浏览: 112
BP神经网络的C语言实现
3星 · 编辑精心推荐
以下是一个简单的BP神经网络模型的C语言实现。
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define INPUT_NUM 2 // 输入层神经元数目
#define HIDDEN_NUM 2 // 隐藏层神经元数目
#define OUTPUT_NUM 1 // 输出层神经元数目
#define MAX_EPOCHS 10000 // 最大迭代次数
#define ERROR_THRESHOLD 0.05 // 最小误差阈值
#define LEARNING_RATE 0.5 // 学习率
double input[INPUT_NUM]; // 输入向量
double hidden[HIDDEN_NUM]; // 隐藏层输出向量
double output[OUTPUT_NUM]; // 输出向量
double target[OUTPUT_NUM]; // 目标向量
double hidden_weights[INPUT_NUM][HIDDEN_NUM]; // 输入层到隐藏层的权值矩阵
double output_weights[HIDDEN_NUM][OUTPUT_NUM]; // 隐藏层到输出层的权值矩阵
double hidden_bias[HIDDEN_NUM]; // 隐藏层偏置
double output_bias[OUTPUT_NUM]; // 输出层偏置
// sigmoid激活函数
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
// sigmoid函数的导数
double sigmoid_derivative(double x) {
return x * (1.0 - x);
}
// 初始化权值和偏置
void initialize_weights_and_bias() {
for (int i = 0; i < INPUT_NUM; i++) {
for (int j = 0; j < HIDDEN_NUM; j++) {
hidden_weights[i][j] = (double)rand() / RAND_MAX;
}
}
for (int i = 0; i < HIDDEN_NUM; i++) {
for (int j = 0; j < OUTPUT_NUM; j++) {
output_weights[i][j] = (double)rand() / RAND_MAX;
}
}
for (int i = 0; i < HIDDEN_NUM; i++) {
hidden_bias[i] = (double)rand() / RAND_MAX;
}
for (int i = 0; i < OUTPUT_NUM; i++) {
output_bias[i] = (double)rand() / RAND_MAX;
}
}
// 前向传播
void forward_propagation() {
for (int i = 0; i < HIDDEN_NUM; i++) {
double sum = 0.0;
for (int j = 0; j < INPUT_NUM; j++) {
sum += input[j] * hidden_weights[j][i];
}
hidden[i] = sigmoid(sum + hidden_bias[i]);
}
for (int i = 0; i < OUTPUT_NUM; i++) {
double sum = 0.0;
for (int j = 0; j < HIDDEN_NUM; j++) {
sum += hidden[j] * output_weights[j][i];
}
output[i] = sigmoid(sum + output_bias[i]);
}
}
// 反向传播
void backward_propagation() {
double output_error[OUTPUT_NUM];
for (int i = 0; i < OUTPUT_NUM; i++) {
output_error[i] = (target[i] - output[i]) * sigmoid_derivative(output[i]);
}
double hidden_error[HIDDEN_NUM];
for (int i = 0; i < HIDDEN_NUM; i++) {
double sum = 0.0;
for (int j = 0; j < OUTPUT_NUM; j++) {
sum += output_error[j] * output_weights[i][j];
}
hidden_error[i] = sum * sigmoid_derivative(hidden[i]);
}
for (int i = 0; i < HIDDEN_NUM; i++) {
for (int j = 0; j < OUTPUT_NUM; j++) {
output_weights[i][j] += LEARNING_RATE * output_error[j] * hidden[i];
}
}
for (int i = 0; i < INPUT_NUM; i++) {
for (int j = 0; j < HIDDEN_NUM; j++) {
hidden_weights[i][j] += LEARNING_RATE * hidden_error[j] * input[i];
}
}
for (int i = 0; i < OUTPUT_NUM; i++) {
output_bias[i] += LEARNING_RATE * output_error[i];
}
for (int i = 0; i < HIDDEN_NUM; i++) {
hidden_bias[i] += LEARNING_RATE * hidden_error[i];
}
}
// 计算误差
double calculate_error() {
double error = 0.0;
for (int i = 0; i < OUTPUT_NUM; i++) {
error += pow(target[i] - output[i], 2);
}
return error / 2.0;
}
// 训练
void train(double inputs[][INPUT_NUM], double targets[][OUTPUT_NUM], int num_examples) {
for (int epoch = 1; epoch <= MAX_EPOCHS; epoch++) {
double total_error = 0.0;
for (int i = 0; i < num_examples; i++) {
for (int j = 0; j < INPUT_NUM; j++) {
input[j] = inputs[i][j];
}
for (int j = 0; j < OUTPUT_NUM; j++) {
target[j] = targets[i][j];
}
forward_propagation();
backward_propagation();
total_error += calculate_error();
}
if (total_error / num_examples < ERROR_THRESHOLD) {
printf("Training finished! Total epochs: %d\n", epoch);
return;
}
if (epoch % 1000 == 0) {
printf("Epoch: %d, Error: %lf\n", epoch, total_error / num_examples);
}
}
printf("Training finished! Total epochs: %d\n", MAX_EPOCHS);
}
// 预测
double predict(double x1, double x2) {
input[0] = x1;
input[1] = x2;
forward_propagation();
return output[0];
}
int main() {
initialize_weights_and_bias();
double inputs[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
double targets[4][1] = {{0}, {1}, {1}, {0}};
train(inputs, targets, 4);
printf("0 0 -> %lf\n", predict(0, 0));
printf("0 1 -> %lf\n", predict(0, 1));
printf("1 0 -> %lf\n", predict(1, 0));
printf("1 1 -> %lf\n", predict(1, 1));
return 0;
}
```
这段代码实现了一个简单的BP神经网络,用于解决异或问题。我们先对神经网络进行了初始化,然后对输入数据进行迭代训练,直到误差达到阈值或者迭代次数达到最大值。最后,我们可以使用训练好的神经网络对新的数据进行预测。
注:此代码只是一个简化的BP神经网络模型,实际应用中可能需要更复杂的模型和算法来解决更复杂的问题。
阅读全文