gpt4 book ai didi

c - malloc 分配的数组丢失

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:44 24 4
gpt4 key购买 nike

我正在尝试实现一个神经网络,但初始化不起作用,但我无法发现我的错误:

typedef struct{
int numWeights;
double* weights;
double wBias;
}NeuronTanh;

typedef struct{
int numNeurons;
NeuronTanh* neurons;
}Layer;

typedef struct{
int numLayers;
Layer* layers;
}Network;

//--------------------------------

void initializeNetwork(Network* network){
//malloc stuff
network->numLayers = NUMBER_LAYERS;
network->layers = malloc(NUMBER_LAYERS * sizeof(Layer));
network->layers[0].numNeurons = 1
network->layers[1].numNeurons = 4
network->layers[2].numNeurons = 2

for(int currentLayerIndex=0; currentLayerIndex<network->numLayers;++currentLayerIndex){
Layer l = network->layers[currentLayerIndex];
l.neurons = malloc(l.numNeurons * sizeof(NeuronTanh));
for(int j=0; j<l.numNeurons; ++j){
if(currentLayerIndex==0){
l.neurons[j].numWeights = 2;
}else{
l.neurons[j].numWeights = network->layers[currentLayerIndex-1].numNeurons;
}
l.neurons[j].weights = malloc((1+l.neurons[j].numWeights) * sizeof(double));
randomizeNeuron(&(l.neurons[j]));
}
}
printNetwork(*network);
}

我现在的问题是,在最里面的 for 循环中,我可以在 randomizeNeuron(...) 之后打印所有权重,但是如果我想在函数末尾打印所有权重或神经元,神经元数组为 NULL,而层数组已正确初始化。为什么神经元(和权重)数组为 NULL?

编辑

 printNetwork(Network network){
fprintf(stderr, "Layers:%i\n",network.numLayers);
for(int numLayer = 0; numLayer<network.numLayers; ++numLayer){
fprintf(stderr, "Layer %i -------------------\n",numLayer);
for(int numNeuron=0; numNeuron<network.layers[numLayer].numNeurons; ++numNeuron){
fprintf(stderr, "Neuron %i: ", numNeuron);
fprintf(stderr, "number of neurons: %i: ", network.layers[numLayer].numNeurons);
if(network.layers[numLayer].neurons != NULL){
for(int numWeight=0; numWeight<network.layers[numLayer].neurons[numNeuron].numWeights; ++numWeight){
fprintf(stderr, "%f ",network.layers[numLayer].neurons[numNeuron].weights[numWeight]);
}
fprintf(stderr, "%f\n", network.layers[numLayer].neurons[numNeuron].wBias);
}
}
}
}

输出是

Layers:3
Layer 0 -------------------
Neuron 0: number of weights: 2: Neuron 1: number of weights: 2: Layer 1 -------------------
Neuron 0: number of weights: 4: Neuron 1: number of weights: 4: Neuron 2: number of weights: 4: Neuron 3: number of weights: 4: Layer 2 -------------------
Neuron 0: number of weights: 1:

最佳答案

问题出在这两行:

Layer l = network->layers[currentLayerIndex];
l.neurons = malloc(l.numNeurons * sizeof(NeuronTanh));

带有 malloc 的行不影响 network->layers[currentLayerIndex].neurons

假设 network->layers[currentLayerIndex].neurons 的地址为 X。之后

Layer l = network->layers[currentLayerIndex];

l.neurons 的地址为 X,因为它是从 network->layers[currentLayerIndex].neurons 复制而来的。

现在 malloc 返回地址 Y,它被分配给 l.neuronsnetwork->layers[currentLayerIndex].neurons 仍然是 X

因此 network->layers[currentLayerIndex] 不受进一步初始化的影响。所有这些初始化都在 l 上执行。

编辑:

可能的解决方案是在初始化完成后将 l.neurons 分配回 network->layers[currentLayerIndex].neurons

关于c - malloc 分配的数组丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34254559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com