🗣 SL提问: c++ai代码
✍️ Ai批改: Creating AI-related code in C++ can cover a wide range of applications, from basic machine learning algorithms to more complex neural networks、For simplicity, let's focus on a basic example of a neural network implemented in C++、This example will be quite rudimentary and is intended to introduce you to the concept、
This example will create a simple neural network with one input layer, one hidden layer, and one output layer、It will be trained on a simple dataset for a binary classification problem.
Dependencies
For this example, we'll keep dependencies minimal、However, for more complex AI projects, you might want to consider using libraries like TensorFlow, OpenCV, or Caffe、Here, we'll stick with the Standard Template Library (STL) and basic C++ for simplicity.
Simple Neural Network Example
cpp
include <iostream
include <vector
include <cmath
include <random
// Sigmoid function and its derivative
double sigmoid(double x) {
return 1 / (1 + exp(-x));
}
double sigmoidDerivative(double x) {
return x * (1 x);
}
class NeuralNetwork {
public:
int inputs, hidden, outputs;
std::vector<std::vector<double weightsIH, weightsHO;
std::vector<double hiddenLayer, outputLayer;
NeuralNetwork(int i, int h, int o) : inputs(i), hidden(h), outputs(o) {
// Initialize weights randomly
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double dis(0.0, 1.0);
weightsIH.resize(inputs, std::vector<double(hidden, 0.0));
weightsHO.resize(hidden, std::vector<double(outputs, 0.0));
for (int i = 0; i < inputs; ++i) {
for (int j = 0; j < hidden; ++j) {
weightsIH[i][j] = dis(gen);
}
}
for (int i = 0; i < hidden; ++i) {
for (int j = 0; j < outputs; ++j) {
weightsHO[i][j] = dis(gen);
}
}
hiddenLayer.resize(hidden, 0.0);
outputLayer.resize(outputs, 0.0);
}
void feedForward(const std::vector<double& input) {
for (int i = 0; i < hidden; ++i) {
double sum = 0.0;
for (int j = 0; j < inputs; ++j) {
sum += input[j] * weightsIH[j][i];
}
hiddenLayer[i] = sigmoid(sum);
}
for (int i = 0; i < outputs; ++i) {
double sum = 0.0;
for (int j = 0; j < hidden; ++j) {
sum += hiddenLayer[j] * weightsHO[j][i];
}
outputLayer[i] = sigmoid(sum);
}
}
void train(const std::vector<double& input, const std::vector<double& target, double learningRate) {
// Calculate output layer errors
std::vector<double outputErrors(outputs, 0.0);
for (int i = 0; i < outputs; ++i) {
double error = target[i] outputLayer[i];
outputErrors[i] = error * sigmoidDerivative(outputLayer[i]);
}
// Adjust hidden-output weights
for (int i = 0; i < hidden; ++i) {
for (int j = 0; j < outputs; ++j) {
weightsHO[i][j] += learningRate * hiddenLayer[i] * outputErrors[j];
}
}
// Calculate hidden layer errors
std::vector<double hiddenErrors(hidden, 0.0);
for (int i = 0; i < outputs; ++i) {
for (int j = 0; j < hidden; ++j) {
hiddenErrors[j] += outputErrors[i] * weightsHO[j][i];
}
}
// Adjust input-hidden weights
for (int i = 0; i < inputs; ++i) {
for (int j = 0; j < hidden; ++j) {
weightsIH[i][j] += learningRate * input[i] * hiddenErrors[j] * sigmoidDerivative(hiddenLayer[j]);
}
}
}
void printOutput() {
for (double val : outputLayer) {
std::cout << val << ;
}
std::cout << std::endl;
}
};
int main() {
// Example usage
NeuralNetwork nn(2, 2, 1); // 2 inputs, 2 hidden units, 1 output
// Example training data (XOR function)
std::vector<std::vector<double inputs = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
std::vector<std::vector<double targets = {{0}, {1}, {1}, {0}};
for (int epoch = 0; epoch < 10000; ++epoch) {
for (int i = 0; i < inputs.size(); ++i) {
nn.feedForward(inputs[i]);
nn.train(inputs[i], targets[i], 1.0); // Learning rate of 1.0 for simplicity
}
}
// Test
for (const auto& input : inputs) {
nn.feedForward(input);
nn.printOutput();
}
return 0;
}
Explanation
NeuralNetwork Class: This class represents a simple neural network with one hidden layer.
feedForward: This method performs a forward pass through the network, calculating the output based on the current weights and inputs.
train: This method adjusts the weights based on the error and learning rate, performing a simple backpropagation.
sigmoid & sigmoidDerivative: Activation function and its derivative used for calculating outputs and gradients.
Note
This is a very basic implementation、Real-world applications would likely use more sophisticated libraries (like TensorFlow or PyTorch, which have C++ APIs) for efficiency and practicality.
The example uses the XOR function as a training dataset for simplicity、The XOR function is a basic example often used in neural network explanations because it's not linearly separable and thus requires a hidden layer.
0
IP地址: 105.213.245.73
搜索次数: 100
提问时间: 2025-04-20 20:26:21
❓️ 热门提问:
现货黄金交易十大平台
黄金手链珠珠
冰岛金条茶多少钱一盒
介绍域名su7.today的含义、价值与适合的行业。
ai做放射
国大深圳金银珠宝质检中心有限公司
掉期外汇交易
4mm黄金手镯多少克
黄金外汇储备是什么
查询网站域名ip
豌豆Ai站群搜索引擎系统
🤝 关于我们:
三乐Ai
作文批改
英语分析
在线翻译
拍照识图
Ai提问
英语培训
本站流量
联系我们
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.05.20》搭建本站。