如何在 C++ 中填充数据或访问 3 维向量
How to fill the data or access a 3-dimensional vector in C++
我正在 Windows、Visual Studio 2015 年实施二维神经元网络。
图片来自Dave Miller博客https://github.com/davidrmiller/neural2d#2D
我有一个拓扑变量,它是网络的结构=>因此它是神经元的3维向量,就像上图一样。
我的拓扑结构是层向量:
typedef vector<vector<Neuron>> Layer; // this is written outside main()
vector<Layer> topology; // this is within main()
然后,在我的 main() 中,我创建了带有示例层的测试拓扑来测试我的 Net class 构造函数:
int main()
{
// test topology {3x3,2x2,2}
vector<Layer> topology;
Layer L0 [3][3];
Layer L1 [2][2];
Layer L2 [1][2];
topology.push_back(L0);
topology.push_back(L1);
topology.push_back(L2);
Net myNet(topology); // create the network
return 0;
}
为了测试我的网络构造函数,我将在每次创建新神经元时{cout}字母'k':
class Net
{
public:
Net(vector<Layer> topology) // constructor
{
// Create the Layers and fill it with neurons
for (int LayersNumber = 0; LayersNumber < topology.size(); LayersNumber++) //(0 is the input layer, last is output, rest are the hidden)
{
m_layers.push_back(Layer()); // add a 2-d layer
for (int NeuronRow = 0; NeuronRow < topology[LayersNumber].size(); NeuronRow++) // fill the layer with Neurons
{
m_layers.back().push_back(vector<Neuron>(0)); // add a vector of Neurons on the new 2-d layer (using .back())
for (int NeuronColumn = 0; NeuronColumn <= topology[LayersNumber][NeuronRow].size(); NeuronColumn++) // <= for the Bias Neuron
{
m_layers[LayersNumber].back().push_back(Neuron()); // add a Neuron
// m_layers[LayerNum][NeuronRow]{NeuronColumn]
cout << "K" << endl; // display test
}
}
}
};
出于某种原因,我在调用 .push_back 函数时在 main() 中出错。它说参数与参数类型不匹配。我想不通。
感谢任何帮助。提前致谢
您的矢量类型是 Layer,但您的变量类型是 Layer[][],因此不匹配。您可以改为将矢量类型更改为 std::array
std::vector<std::array<Layer, 12> >
并将数组更改为 std::array
或在 vector
中使用 vector
std::vector<std::vector<Layer> > multiLayer;
而不是处理 std 向量;恕我直言,您应该使用为多维设计的数据结构。 cppconf2015 中提供了一个很好的示例:https://youtu.be/CPPX4kwqh80;您可以在视频说明中找到 git 中心 link。
我正在 Windows、Visual Studio 2015 年实施二维神经元网络。
图片来自Dave Miller博客https://github.com/davidrmiller/neural2d#2D
我有一个拓扑变量,它是网络的结构=>因此它是神经元的3维向量,就像上图一样。
我的拓扑结构是层向量:
typedef vector<vector<Neuron>> Layer; // this is written outside main()
vector<Layer> topology; // this is within main()
然后,在我的 main() 中,我创建了带有示例层的测试拓扑来测试我的 Net class 构造函数:
int main()
{
// test topology {3x3,2x2,2}
vector<Layer> topology;
Layer L0 [3][3];
Layer L1 [2][2];
Layer L2 [1][2];
topology.push_back(L0);
topology.push_back(L1);
topology.push_back(L2);
Net myNet(topology); // create the network
return 0;
}
为了测试我的网络构造函数,我将在每次创建新神经元时{cout}字母'k':
class Net
{
public:
Net(vector<Layer> topology) // constructor
{
// Create the Layers and fill it with neurons
for (int LayersNumber = 0; LayersNumber < topology.size(); LayersNumber++) //(0 is the input layer, last is output, rest are the hidden)
{
m_layers.push_back(Layer()); // add a 2-d layer
for (int NeuronRow = 0; NeuronRow < topology[LayersNumber].size(); NeuronRow++) // fill the layer with Neurons
{
m_layers.back().push_back(vector<Neuron>(0)); // add a vector of Neurons on the new 2-d layer (using .back())
for (int NeuronColumn = 0; NeuronColumn <= topology[LayersNumber][NeuronRow].size(); NeuronColumn++) // <= for the Bias Neuron
{
m_layers[LayersNumber].back().push_back(Neuron()); // add a Neuron
// m_layers[LayerNum][NeuronRow]{NeuronColumn]
cout << "K" << endl; // display test
}
}
}
};
出于某种原因,我在调用 .push_back 函数时在 main() 中出错。它说参数与参数类型不匹配。我想不通。
感谢任何帮助。提前致谢
您的矢量类型是 Layer,但您的变量类型是 Layer[][],因此不匹配。您可以改为将矢量类型更改为 std::array
std::vector<std::array<Layer, 12> >
并将数组更改为 std::array
或在 vector
std::vector<std::vector<Layer> > multiLayer;
而不是处理 std 向量;恕我直言,您应该使用为多维设计的数据结构。 cppconf2015 中提供了一个很好的示例:https://youtu.be/CPPX4kwqh80;您可以在视频说明中找到 git 中心 link。