如何在C ++中实现神经网络

问题描述 投票:-1回答:1

原始问题

我正在研究一个简单的C ++库。我正在尝试实现神经网络。我有两个问题:

  1. 是否有任何教程可以解释如何实施它们?
  2. 在实施神经网络时,我是否真的需要绘制图形?

我到目前为止编写的代码是:

#ifndef NEURAL_NETWORK_H
#define NEURAL_NETWORK_H

#include <ctime>
#include <cstdlib>

class NeuralNetwork {
    public :
        void SetWeight(double tempWeights [15]) {
            for (int i = 0; i < (sizeof(tempWeights) / sizeof(double)); ++i) {
                weights[i] = tempWeights[i];
            }
        }

        double GetWeights() {
            return weights;
        }

        void Train(int numInputs, int numOutputs, double inputs[], double outputs[]) {
            double tempWeights[numOutputs];

            int iterator = 0;

            while (iterator < 10000) {
                  // This loop will train the Neural Network
            }

            SetWeights(tempWeights);
        }

        double[] Calculate(double inputs[]) {
             // Calculate Outputs...

             return outputs;
        }

        NeuralNetwork(double inputs[], double outputs[]) {
            int numberOfInputs = sizeof(inputs) / sizeof(double);
            int numberOfOutputs = sizeof(outputs) / sizeof(double);

            Train(numberOfInputs, numberOfOutputs, inputs[], outputs[]);
        }
    private :
        double weights[15];
};

#endif // NEURAL_NETWORK_H

编辑和更新的问题

感谢评论的帮助,我设法实现了神经网络。

现在,我正在努力解决性能问题。 srand实际上开始变得无益了......

有更好的随机功能吗?

c++ neural-network backpropagation
1个回答
0
投票

首先,我从这个项目中学到了很多东西,我学到了std::uniform_real_distribution<>std::vector<>和语法结构。

srandtime是C函数。因此,为了获得最佳优化,不应使用它们。

那么,我们应该使用什么? std::uniform_real_distribution因为它更灵活和稳定。

std::vector<double> set_random_weights()
{ 
    std::default_random_engine generator; 
    std::uniform_real_distribution<double> distribution(0.0,1.0);

    std::vector<double> temp_weights;

    for (unsigned int i = 0; i < (num_input_nodes * num_hidden_nodes); ++i)
    {
        temp_weights.push_back(distribution(generator));
    }

   return temp_weights;
}

但是要使用std::uniform_real_distributionstd::default_random_engine,我们需要包含random标题:

#include <random>

要使用std::vectors,我们必须在vector标题中:

#include <vector>
© www.soinside.com 2019 - 2024. All rights reserved.