本文介绍了如何在DOD之后创建多层感知器?或者如何存储动态分配的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 首先,我是对DOD的这个概念的新手,虽然是新的,但从程序员的角度来看,我觉得非常令人兴奋。 一个多层感知器作为一个自己的OO项目,因为我现在学习DOD,我认为这将是很好用这个范例。 struct Neuron { double bias; double error; }; struct Layer {神经元*神经元; double * output; double ** connections; unsigned numberNeurons; }; struct Network { unsigned numberInput; double * input; std :: vector< Layer *>隐; layer * output; }; 我知道它可能不是(几乎肯定不是)最好的格式,以分离我将在Layer的不同数组中使用更多的东西。但是数组存储的方式真的让我感兴趣,因为他们应该被堆叠在一起作为一个结构,以更快的内存读取(或者我错过了什么?)。如果我记得正确,new []在内存中的某个位置分配数组,只存储指向该位置的指针,而结构中的静态数组将在其空间内分配。 基于这一点,我想到了制作Layer(和Network)模板结构: template< unsigned n_neurons,unsigned n_connections> ; struct Layer {神经元神经元[n_neurons]; double output [n_neurons]; double connections [n_neurons] [n_connections]; static const unsigned numberNeurons = n_neurons; }; 如果Layer变成这样的东西,会有什么办法做一个可变的模板网络任意数量的隐藏层?还是我对静态数组的理解错了?在创建这样的数组(和访问时间)之间有什么区别吗?在 Layer >意味着他们将在 struct 中分配是否会提高性能将取决于如何访问您的数据,通常你不想担心太多优化,直到开始出现性能问题。这就是为什么我想知道为什么你不使用 std :: vector 会有很少的性能影响,你不必跟踪大小或担心分配。否则,很难对性能进行概括,这将是应用程序特定的,你应该确定基于基准的最佳方法。 至于使用这个示例应该给你一个好主意如何使它工作: 的可变参数模板 模板< unsigned n_neurons,unsigned n_connections> struct Network { unsigned numberInput; double * input; std :: vector< Layer< n_neurons,n_connections> *>隐; Layer< n_neurons,n_connections> * output; template< typename ... Args> void helper(Layer< n_neurons,n_connections> * layer) { std :: cout< 最终帮助器< std :: endl; std :: cout<<层 - >数字} template< typename ... Args> void helper(Layer< n_neurons,n_connections> * layer,Args ... args) { std :: cout< helper< std :: endl; std :: cout<<层 - >数字 helper(args ...); } template< typename ... Args> Network(Args ... args) { std :: cout< Network ctor()< std :: endl; helper(args ...); } }; int main() { Network< 10,5> N1(新层< 10,5>(),新层< 10,5>(),新层< 10,5> } First of all, I'm new to this concept of DOD, and while new to it, I find it really exciting from a programmer perspective.I made a Multi-Layer Perceptron a while ago as an OO project for myself, and since I'm learning DOD now, I thought it would be nice to make it with this paradigm.struct Neuron{ double bias; double error;};struct Layer{ Neuron* neurons; double* output; double** connections; unsigned numberNeurons;};struct Network{ unsigned numberInput; double* input; std::vector<Layer*> hidden; Layer* output;};I know it may not be (and almost certainly isn't) the best format, but I tried to separate the things I'd use more in different arrays of the Layer. But the way the arrays are stored is really intriguing me, since they are supposed to be stacked together as a struct for faster memory reading (or did I miss something?). If I recall correctly, new[] allocates the array somewhere in the memory and stores only the pointer to that location, while a static array in a struct would be allocated within its space.Based on that, I thought of making Layer (and Network) template structs:template<unsigned n_neurons, unsigned n_connections>struct Layer{ Neuron neurons[n_neurons]; double output[n_neurons]; double connections[n_neurons][n_connections]; static const unsigned numberNeurons = n_neurons;};If Layer became such thing, though, would there be any way to make a variadic template of Network with any number of hidden layers? Or is my understanding of static arrays wrong? Is there any difference between creation of such arrays (and access time)? Where are my keys? 解决方案 While using C-style arrays in Layer means that they will be allocated within the struct whether that will improve performance is going to depend on how you access your data and usually you don't want to worry too much about optimizing until you start having performance issues. Which is why I was wondering why don't you use std::vector there would be little performance hit and you don't have to keep track of size or worry about allocation. Otherwise it is hard to make generalizations about performance it will be application specific and you should determine the best approach based on benchmarks.As for using a variadic template for Network this sample should give you a good idea how to make it work:template<unsigned n_neurons, unsigned n_connections>struct Network{ unsigned numberInput; double* input; std::vector<Layer<n_neurons,n_connections>*> hidden; Layer<n_neurons,n_connections>* output; template <typename... Args> void helper( Layer<n_neurons,n_connections>* layer) { std::cout << "final helper" << std::endl ; std::cout << layer->numberNeurons << std::endl ; } template <typename... Args> void helper( Layer<n_neurons,n_connections>* layer, Args... args) { std::cout << "helper" << std::endl ; std::cout << layer->numberNeurons << std::endl ; helper( args... ) ; } template <typename... Args> Network(Args... args) { std::cout << "Network ctor()" << std::endl ; helper( args... ) ; }};int main(){ Network<10,5> N1( new Layer<10,5>(), new Layer<10,5>(), new Layer<10,5>() ) ;} 这篇关于如何在DOD之后创建多层感知器?或者如何存储动态分配的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 18:54