英文:
Segmentation Fault In Neural Network Program c++
问题
我正在开发一个用于实现神经网络的C++程序,但我弄不清楚为什么这段代码会导致分段错误。
class Node {
public:
// 为权重和偏置创建向量
vector<double> weights;
double biasN;
// 构造函数
Node(int numNodeIn) {
// 循环将数组中的所有输入和权重放入它们各自的向量中
for (int i = 0; i < numNodeIn; i++) {
double z = (double)(rand() % 100) / 100;
cout << z << "##" << endl;
weights.push_back(z);
}
// 特定节点的偏置值
biasN = (double)(rand() % 1000) / 100;
cout << biasN << "#" << endl;
}
};
class Layer {
public:
// 变量
int numNodesIn, numNodesOut;
vector<Node> nodes;
Layer(int NodesIn, int NodesOut) {
this->numNodesIn = NodesIn;
this->numNodesOut = NodesOut;
// 根据需求创建并添加节点
for (int i = 0; i < numNodesOut; i++) {
// 创建Node类的对象
Node N(numNodesIn);
// 将对象添加到向量中
nodes.push_back(N);
}
}
double* calcOutput(double inputs[]) {
// 用于存储计算后的加权输入的动态数组
double* weightedInputs = new double[numNodesOut];
// 循环迭代输出节点并计算加权输入
for (int nodeOut = 0; nodeOut < numNodesOut; nodeOut++) {
// 将偏置添加到加权输入
double weightedInput = nodes[nodeOut].biasN;
// 添加所有与权重相乘的输入
for (int nodeIn = 0; nodeIn < numNodesIn; nodeIn++) {
weightedInput += inputs[nodeOut] * nodes[nodeOut].weights[nodeIn];
}
weightedInputs[nodeOut] = weightedInput;
}
return weightedInputs;
}
};
// 主函数
int main() {
// 设置随机种子为当前时间
srand(static_cast<unsigned>(time(0)));
const int hiddenLayers = 4;
// 用于测试的变量
Layer layer(3, hiddenLayers);
double inp[3] = {2.5, 2.5, 1.5};
double* out = layer.calcOutput(inp);
// 打印结果
for (int i = 0; i < hiddenLayers; i++) {
cout << out[i] << endl;
}
delete[] out;
}
在没有更新srand时间种子的情况下运行时,会出现“make: *** [Makefile:22: run] Segmentation fault”错误。
在更新srand种子后运行时,会出现“munmap_chunk(): invalid pointer”错误。
分段错误和无效指针错误都发生在所有值打印后。似乎错误发生在所有计算完成后。我还没有使用C++调试器进行测试,因为我以前从未使用过C++调试器。
非常感谢您浏览这段长代码。任何帮助将不胜感激。感谢提前帮助。我已经尝试替换可能有问题的代码部分,但似乎没有任何帮助。
英文:
I was working on a C++ program to implement a neural network.
But I cant figure out why this code produces a segmentation fault.
class Node{
public:
//Creating Vectors for weights and biases
vector<double> weights;
double biasN;
//Constructor
Node(int numNodeIn){
//The Loop enters all the inputs and weights from the array into their respective vectors
for (int i = 0; i < numNodeIn; i++)
{
double z = (double)(rand() % 100) /100;;
cout << z<<"##"<<endl;
weights.push_back(z);
}
//The bias value of a particular Node
biasN = (double)(rand() % 1000) /100;
cout << biasN<<"#"<<endl;
}
};
class Layer{
public:
//Vars
int numNodesIn,numNodesOut;
vector<Node> nodes;
Layer(int NodesIn,int NodesOut){
this->numNodesIn = NodesIn;
this->numNodesOut = NodesOut;
//Creates and adds Nodes as per requirement
for (int i = 0; i < numNodesOut; i++)
{
//Creates a object of class Node
Node N(numNodesIn);
//Adds the object in the vector
nodes.push_back(N);
}
}
double* calcOutput(double inputs[]){
//Dynamic array for storing weighted inputs after computation
double *weightedInputs = new double(numNodesOut);
//For loop to iterate over the Outnodes and compute weighted inputs
for (int nodeOut = 0; nodeOut < numNodesOut; nodeOut++)
{
// Adding Bias to the weighted input
double weightedInput = nodes[nodeOut].biasN;
// Adding all the inputs multiplied with weights
for (int nodeIn = 0; nodeIn < numNodesIn; nodeIn++)
{
weightedInput += inputs[nodeOut] * nodes[nodeOut].weights[nodeIn];
}
weightedInputs[nodeOut] = weightedInput;
}
return weightedInputs;
}
};
//Main Function
int main() {
// long double out = input[0]*weight[0] + input[1]*weight[1] + input[2]*weight[2] + input[3]*weight[3] + input[4]*weight[4] + bias[0];
// Setting random seed as current time
srand (static_cast <unsigned> (time(0)));
const int hiddenLayers = 4;
// Vars for testing
Layer layer(3,hiddenLayers) ;
double inp[3] = {2.5,2.5,1.5};
double *out = layer.calcOutput(inp);
// Printing the result
for (int i = 0; i < hiddenLayers; i++)
{
cout << out[i]<< endl;
}
delete [] out;
}
When run without the srand time seed update it produces a make: *** [Makefile:22: run] Segmentation fault.
When run with the srand seed update it produces a munmap_chunk(): invalid pointer.
The segmentation fault as well invalid pointer error occur after all the values have been printed.
It seems that the error occurs after all the computations are done.
I have not tested with a debugger yet since I have never used a c++ debugger before.
Any help would be appreciated. Thanks in advance for looking through the long code.
I tried replacing all the parts of the code that could be problematic but nothing seems to help.
答案1
得分: 2
一个简单的错误是
double *weightedInputs = new double(numNodesOut);
应该改成
double *weightedInputs = new double[numNodesOut];
但正如注释中所指出的,应该避免使用 new
和指针。weightedInputs
应该是一个 vector
,而 calcOutput
应该返回这个 vector
。
正如这里有人曾经说过,在C++中,“动态数组”的拼写是 std::vector
。
英文:
One simple error is
double *weightedInputs = new double(numNodesOut);
which should be
double *weightedInputs = new double[numNodesOut];
But as stated in the comments avoid new
and pointers altogether. weightedInputs
should be a vector
and calcOutput
should return that vector
.
As someone here once said. in C++ 'dynamic array' is spelled std::vector
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论