用户提问: 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地址: 239.246.6.56
搜索次数: 12
提问时间: 2025-04-20 20:26:21
热门提问:
环球医疗
美国的ai大模型
ai人物转插画
ai学习培训机构
中信保诚先进制造混合C