我目前正在编写需要矢量的东西。我有两个嵌套循环;
for (size_t i = 0; i < m_neurons.size(); i++) {
for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
}
}
这部分是我们感兴趣的部分:
d.out ("i == " + std::to_string (i));
d.out ("m_neurons.size() == " + std::to_string (m_neurons.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
我打印出一些尺寸然后,我希望C ++循环遍历矢量;够容易吧?
除非发生这种情况:
i == 0
m_neurons.size() == 1
m_neurons[i].m_weights.size() == 4
m_neurons[i].m_weights.size() == 4
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (Speicherabzug geschrieben)
[maximilian@new-host library]$
1)不知何故,矢量被清空了
2)不知何故,数据的收集被视为我使用.at (int index)
我有:
vector.at()
和vector[]
我知道:
现在我的问题是:
编辑:
我制作了一些可以解决问题的代码。似乎工作正常。问题是:我的代码和那里的代码之间没有任何变化。 什么可能是触发错误的东西?如上所述:在dbg中,向量的大小从一步变为另一步
#include <iostream>
#include <vector>
#include "debugMachine.h"
class bar {
public:
bar (std::vector<double> i) {
foobarmiz = i;
}
std::vector<double> foobarmiz;
};
class foo {
public:
std::vector<bar> foobar;
void test (std::vector<double>& expected) {
d.out ("HI");
for (size_t i = 0; i < foobar.size(); i++) {
d.out ("HI2");
d.out ("i == " + std::to_string (i));
d.out ("m_neurons.size() == " + std::to_string (foobar.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (foobar.at(i).foobarmiz.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (foobar.at(i).foobarmiz.size()));
for (size_t j = 0; j < foobar[0].foobarmiz.size(); j++) {
d.info ("Gonna change weights");
foobar.at(i).foobarmiz[j] +=1;
}
d.out ("out of backward");
}
}
} f;
int main () {
d.write = true;
std::vector<double> test {1.0f, 2.0f, 3.0f, 5.34f};
f.foobar.push_back (bar (test));
f.foobar.push_back (bar (test));
f.foobar.push_back (bar (test));
f.test (test);
}
这是错误的功能的完整性:
void CLayer::m_backward (std::vector<double>& expected) {
d.out ("HI");
for (size_t i = 0; i < m_neurons.size(); i++) {
d.out ("HI2");
d.out ("i == " + std::to_string (i));
d.out ("m_neurons.size() == " + std::to_string (m_neurons.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
d.info ("Gonna change weights");
d.info ("m_weights before : " + std::to_string(m_neurons.at(i).m_weights.at(j)));
m_neurons.at(i).m_weights[j] -= (0.003f * -(m_expected.at(i) - m_output.at(i)) * m_output.at(i) * (1 - m_output.at(i)) * m_input.at(j));
d.info ("m_weights after : " + std::to_string(m_neurons[i].m_weights[j]));
}
d.out ("out of backward");
}
}
答案 0 :(得分:0)
这是提出问题的示例。
#include <string>
#include <iostream>
#include <vector>
class bar {
public:
bar(std::vector<double> i) {
foobarmiz = i;
}
std::vector<double> foobarmiz;
};
class foo {
public:
std::vector<bar> foobar;
void test(std::vector<double>& expected) {
std::cout << ("HI");
for (size_t i = 0; i < foobar.size(); i++) {
std::cout << ("HI2") << std::endl;
std::cout << ("i == " + std::to_string(i)) << std::endl;
std::cout << ("m_neurons.size() == " + std::to_string(foobar.size())) << std::endl;
std::cout << ("m_neurons[i].m_weights.size() == " + std::to_string(foobar.at(i).foobarmiz.size())) << std::endl;
std::cout << ("m_neurons[i].m_weights.size() == " + std::to_string(foobar.at(i).foobarmiz.size())) << std::endl;
for (size_t j = 0; j < foobar[0].foobarmiz.size(); j++) { // because of this line
std::cout << ("Gonna change weights") << std::endl;
foobar.at(i).foobarmiz[j] += 1;
}
std::cout << ("out of backward") << std::endl;
}
}
} f;
int main()
{
std::vector<double> test{ 1.0f, 2.0f, 3.0f, 5.34f };
f.foobar.push_back(bar(test));
std::vector<double> test2{ 1.0f, 23.3f, 2.1f };
f.foobar.push_back(bar(test2));
std::vector<double> test3{ 1.0f, 23.3f };
f.foobar.push_back(bar(test3));
f.test(test);
}