我正在尝试实现一个神经网络,但初始化不起作用,但我无法发现我的错误:
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:%in",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, "%fn", 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.neurons
,而network->layers[currentLayerIndex].neurons
保持X。
因此CCD_ 8不受进一步初始化的影响。所有这些初始化都是在CCD_ 9上执行的。
编辑:
可能的解决方案是在初始化完成后将l.neurons
分配回network->layers[currentLayerIndex].neurons
。