C++ 神经网络分类:二维点分类示例
C++ 神经网络分类:二维点分类示例
本文将介绍一个使用 C++ 编写的简单神经网络分类器示例,用于实现二维点的分类。该示例使用两个输入特征 x1 和 x2,训练数据集包含两个点,每个点的标签为 0 或 1。通过调用 train 函数进行训练,然后通过调用 predict 函数进行预测。
神经网络结构
神经网络结构如下所示:

代码示例
#include <iostream>
#include <cmath>
#include <vector>
class Network {
private:
int epoches;
double learning_rate;
double w1, w2, w3, w4, w5, w6;
double b1, b2, b3;
public:
Network(int epoches, double learning_rate) {
this->epoches = epoches;
this->learning_rate = learning_rate;
this->w1 = 0.0;
this->w2 = 0.0;
this->w3 = 0.0;
this->w4 = 0.0;
this->w5 = 0.0;
this->w6 = 0.0;
this->b1 = 0.0;
this->b2 = 0.0;
this->b3 = 0.0;
}
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
double deriv_sigmoid(double x) {
double sigmoid_x = sigmoid(x);
return sigmoid_x * (1.0 - sigmoid_x);
}
double forward(double x1, double x2) {
double h1 = sigmoid(w1 * x1 + w2 * x2 + b1);
double h2 = sigmoid(w3 * x1 + w4 * x2 + b2);
double output = sigmoid(w5 * h1 + w6 * h2 + b3);
return output;
}
void train(std::vector<std::vector<double>>& data) {
for (int epoch = 0; epoch < epoches; epoch++) {
for (int i = 0; i < data.size(); i++) {
double x1 = data[i][0];
double x2 = data[i][1];
double target = data[i][2];
double h1 = sigmoid(w1 * x1 + w2 * x2 + b1);
double h2 = sigmoid(w3 * x1 + w4 * x2 + b2);
double output = sigmoid(w5 * h1 + w6 * h2 + b3);
double loss = 0.5 * pow(target - output, 2);
double dloss_doutput = output - target;
double doutput_dw5 = h1;
double doutput_dw6 = h2;
double doutput_db3 = 1.0;
double doutput_dh1 = w5;
double doutput_dh2 = w6;
double dh1_dw1 = x1;
double dh1_dw2 = x2;
double dh1_db1 = 1.0;
double dh2_dw3 = x1;
double dh2_dw4 = x2;
double dh2_db2 = 1.0;
w1 -= learning_rate * dloss_doutput * doutput_dh1 * deriv_sigmoid(h1) * dh1_dw1;
w2 -= learning_rate * dloss_doutput * doutput_dh1 * deriv_sigmoid(h1) * dh1_dw2;
b1 -= learning_rate * dloss_doutput * doutput_dh1 * deriv_sigmoid(h1) * dh1_db1;
w3 -= learning_rate * dloss_doutput * doutput_dh2 * deriv_sigmoid(h2) * dh2_dw3;
w4 -= learning_rate * dloss_doutput * doutput_dh2 * deriv_sigmoid(h2) * dh2_dw4;
b2 -= learning_rate * dloss_doutput * doutput_dh2 * deriv_sigmoid(h2) * dh2_db2;
w5 -= learning_rate * dloss_doutput * doutput_dw5;
w6 -= learning_rate * dloss_doutput * doutput_dw6;
b3 -= learning_rate * dloss_doutput * doutput_db3;
}
}
}
int predict(double x1, double x2) {
double output = forward(x1, x2);
return (output >= 0.5) ? 1 : 0;
}
};
int main() {
std::vector<std::vector<double>> data = {{1.0, 1.0, 0.0}, {-1.0, -1.0, 1.0}};
Network network(1000, 0.1);
network.train(data);
int result1 = network.predict(2.0, 2.0);
int result2 = network.predict(-2.0, -2.0);
std::cout << "Prediction for (2.0, 2.0): " << result1 << std::endl;
std::cout << "Prediction for (-2.0, -2.0): " << result2 << std::endl;
return 0;
}
代码解释
- Network 类:
- 属性变量包含训练次数
epoches、学习率learning_rate、网络参数w1,w2,w3,w4,w5,w6和b1,b2,b3。 - 行为函数包括构造函数
Network、激活函数sigmoid、激活函数求导deriv_sigmoid、前向传播函数forward、训练函数train和预测函数predict。
- 属性变量包含训练次数
- sigmoid 函数: 作为激活函数,将输入值压缩到 0 到 1 之间。
- deriv_sigmoid 函数: sigmoid 函数的导数,用于反向传播过程中更新权重。
- forward 函数: 执行前向传播,计算网络的输出。
- train 函数: 使用梯度下降算法训练神经网络。
- predict 函数: 使用训练好的神经网络预测输入特征的标签。
- main 函数: 创建 Network 对象,训练网络并预测两个点的标签。
总结
该示例代码展示了一个简单的神经网络分类器的实现,可以用于实现二维点的分类。通过该示例可以学习神经网络的基本结构、训练和预测过程。
扩展
- 可以增加更多数据点和输入特征,训练更复杂的神经网络。
- 可以使用不同的激活函数,例如 ReLU。
- 可以使用不同的优化算法,例如 Adam。
- 可以使用 TensorFlow 或 PyTorch 等深度学习框架,简化神经网络的实现过程。
原文地址: https://www.cveoy.top/t/topic/qg1L 著作权归作者所有。请勿转载和采集!