C exc_bad_access这是什么意思



我正在尝试在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;
} 

错误消息和行为对于无效的内存访问是典型的。这是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()
  • 如果这无助于解决问题,则意味着代码中的其他一些位置弄乱(例如,verlayer已经指的是不存在的对象,或者某些内存腐败之前发生并且没有显示任何记忆损坏结果直到这一刻)。

相关内容

  • 没有找到相关文章

最新更新