我正在尝试用C ++编写我的第一个神经网络,但遇到了麻烦。 基本上我得到
分段错误11
所以我尝试调试,结果发现在第41行之后,该程序由于名为
的原因而停止了EXC_BAD_ACCESS,
是什么意思?我该如何扭转呢?我从其他帖子中看到,分段错误意味着堆栈用完了,这将是一个问题,因为这可能是我计划编写的代码的30%
无论如何这是我的代码,我在注释中输入了第41行(请参阅注释)
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
struct Connection
{
double w;
double dw;
};
class Neuron;
typedef vector<Neuron> Layer;
//*************************** class Neuron ***************
class Neuron
{
public:
Neuron(unsigned numOutput, unsigned MyIndex);
void setOtpuVal(double Val){m_outputVal = Val; }
double getOutputval(void) const {return m_outputVal; }
void feedForward (const Layer &prevLayer);
private:
static double randomWeight(void) {return (rand()/(RAND_MAX));}
unsigned m_MyIndex;
double m_outputVal;
vector<Connection> m_outputWeights;
};
void Neuron::feedForward (const Layer &prevLayer){
double sum = 0.0; //<===================================that's line 40
for (unsigned i = 0; i < prevLayer.size(); ++i)
{
sum += prevLayer[i].getOutputval() * prevLayer[i].m_outputWeights[m_MyIndex].w;
}
}
Neuron::Neuron(unsigned numOutput, unsigned MyIndex){
for (unsigned c = 0; c < numOutput; ++c)
{
m_outputWeights.push_back(Connection());
m_outputWeights.back().w = randomWeight();
}
m_MyIndex = MyIndex;
}
//*************************** class Net *********************
class Net{
public:
Net (const vector<unsigned> &Topology);
void feedForward(const vector<double> &Input );
void backProp (const vector<double> &Target){};
void getResults(vector<double> &Output) const{}; //const non modifica l'oggetto
private:
std::vector<Layer> m_layer; // m_layer []
};
void Net::feedForward (const vector<double> &Input ) {
//assert(Input.size() == m_layer[0].size()-1);
// assert(inputVals.size() == m_layers[0].size() - 1);
//feeding
for (unsigned i = 0; i < Input.size()-1; ++i) {
m_layer [0] [i].setOtpuVal(Input[i]);
}
//foorward propagatin
for (unsigned i = 0; i < m_layer.size(); ++i)
{
Layer &prevLayer = m_layer[i-1];
for (unsigned j = 0; j < m_layer[i].size(); ++j)
{
m_layer[i][j].feedForward(prevLayer);
}
}
}
Net::Net (const vector<unsigned> &Topology){
unsigned numLayer = Topology.size();
for (unsigned i = 0; i < numLayer; ++i)
{
m_layer.push_back(Layer());
unsigned numOutput = i == Topology.size() - 1 ? 0 : Topology[i +1]; //inportante!!!!
for (unsigned j = 0; j <= Topology[i]; ++j)
{
m_layer.back().push_back(Neuron(numOutput,j));
cout<<"made a Neuron"<<endl;
}
}
}
int main(int argc, char const *argv[])
{
ifstream reader("Istruzioni.txt",ios::in);
std::vector<double> Input;
std::vector<double> Target;
std::vector<double> Output;
std::vector<unsigned> Topology;
Topology.push_back(3);
Topology.push_back(2);
Topology.push_back(1);
Net MyNet(Topology);
char letter;
for (int i = 0; i < 4; ++i)
{
reader.get(letter);
if (i == 3)
{
Target.push_back( (float)letter - 48);
}
else
Input.push_back((float)letter - 48);
}
reader.close();
MyNet.feedForward (Input);
MyNet.backProp(Target);
MyNet.getResults(Output);
return 0;
}
答案 0 :(得分:0)
错误消息和行为通常是无效的内存访问。这是UB的可能症状之一。因此,此后可以说的只是一个可能的解释,而不能肯定。
查看代码,并假定使用有效的prevLayer
参数对其进行了调用:
for (unsigned i = 0; i < prevLayer.size(); ++i)
{
sum += prevLayer[i].getOutputval() * prevLayer[i].m_outputWeights[m_MyIndex].w;
}
很有可能超出范围。由于我原则上处于范围之内,所以麻烦来了m_MyIndex
。
该怎么办?
prevLayer[i].m_outputWeights.size()