我用C写了一个神经网络。有一个神经元模型结构和输入、隐藏然后输出层结构。这段代码实际上是由 Python 编写的,但我将其转换为 C。调试或运行后出现类似错误 “运行时检查失败 #2 - 变量“outputLayer”周围的堆栈已损坏。” 预测输出是正确,但最后还是出现了错误。这是下面的代码。
#include<stdio.h>
#include<math.h>
#define INPUT_SIZE 3
#define HIDDEN_SIZE 8
#define OUTPUT_SIZE 1
#define LEARNING_RATE 0.09
#define EPOCHS 20000
struct Neuron {
double weights[INPUT_SIZE];
double bias;
};
double relu(double x) {
return fmax(0, x);
}
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
double sigmoid_derivative(double x) {
return x * (1 - x);
}
double calculateNeuronOutput(struct Neuron *neuron, double inputs[], int use_relu) {
double sum = neuron->bias;
for (int i = 0; i < INPUT_SIZE; i++) {
sum += inputs[i] * neuron->weights[i];
}
if (use_relu){
return relu(sum);
}
else {
return sigmoid(sum);
}
}
void backPropagation(struct Neuron inputLayer[], struct Neuron hiddenLayer[],
struct Neuron outputLayer[], double inputVector[], double target) {
double hiddenLayerOutput[HIDDEN_SIZE];
for (int i = 0; i < HIDDEN_SIZE; i++) {
hiddenLayerOutput[i] = calculateNeuronOutput(&hiddenLayer[i], inputVector, 1);
}
double output = calculateNeuronOutput(&outputLayer[0], hiddenLayerOutput, 0);
double error = 0.5 * pow(target - output, 2);
double output_error = target - output;
double output_delta = output_error * sigmoid_derivative(output);
double hiddenLayer_error[HIDDEN_SIZE];
for (int i = 0; i < HIDDEN_SIZE; i++) {
hiddenLayer_error[i] = output_delta * outputLayer[0].weights[i];
}
for (int i = 0; i < HIDDEN_SIZE; i++) {
for (int j = 0; j < INPUT_SIZE; j++) {
hiddenLayer[i].weights[j] += LEARNING_RATE * hiddenLayer_error[i] * inputVector[j];
}
hiddenLayer[i].bias += LEARNING_RATE * hiddenLayer_error[i];
}
for (int i = 0; i < OUTPUT_SIZE; i++) {
for (int j = 0; j < HIDDEN_SIZE; j++) {
outputLayer[i].weights[j] += LEARNING_RATE * output_delta * hiddenLayerOutput[j];
}
outputLayer[i].bias += LEARNING_RATE * output_delta;
}
}
int main() {
struct Neuron inputLayer[INPUT_SIZE] = {
{{0.1, -0.2, 0.3}, 0.1},
{{0.2, -0.1, -0.3}, 0.2},
{{0.3, 0.4, -0.1}, 0.2},
};
struct Neuron hiddenLayer[HIDDEN_SIZE] = {
{{0.1, 0.2, 0.3}, 0.1},
{{0.2, 0.1, -0.3}, 0.2},
{{0.3, 0.4, 0.1}, 0.3},
{{0.2, 0.1, 0.4}, -0.4},
{{0.5, 0.2, -0.3}, -0.1},
{{0.1, 0.3, 0.2}, 0.4},
{{0.3, -0.4, 0.1}, 0.2},
{{0.4, 0.2, -0.1}, -0.3}
};
struct Neuron outputLayer[OUTPUT_SIZE] = {
{{0.1, -0.2, 0.3}, 0.2}
};
double inputVector[4][INPUT_SIZE] = { { 0.0, 0.0, 1.0 },{0.0, 1.0, 0.0},{0.0, 1.0, 1.0},{1.0, 1.0, 1.0 } };
double targetOutput[4] = { {0.0},{1.0},{1.0},{0.0} };
for (int epoch = 0; epoch < EPOCHS; epoch++) {
for(int i=0; i< 4; i++){
backPropagation(inputLayer, hiddenLayer, outputLayer, inputVector[i], targetOutput[i]);
}
if (epoch % 1000 == 0) {
double totalError = 0.0;
for (int i = 0; i < 4; i++) {
double hiddenLayerOutput[HIDDEN_SIZE];
for (int j = 0; j < HIDDEN_SIZE; j++) {
hiddenLayerOutput[j] = calculateNeuronOutput(&hiddenLayer[j], inputVector[i], 1);
}
double output = calculateNeuronOutput(&outputLayer[0], hiddenLayerOutput, 0);
double error = 0.5 * pow(targetOutput[i] - output, 2);
totalError += error;
}
printf("Epoch: %d, Loss: %f\n", epoch, totalError);
}
}
// Tahmin
double predictionInput[4][INPUT_SIZE] = { { 0.0, 0.0, 1.0 },{0.0, 0.0, 1.0},{0.0, 1.0, 1.0},{0.0, 1.0, 1.0 } };
for (int i = 0; i < 4; ++i) {
double hiddenLayerOutput[HIDDEN_SIZE];
for (int j = 0; j < HIDDEN_SIZE; ++j) {
hiddenLayerOutput[j] = calculateNeuronOutput(&hiddenLayer[j], predictionInput[i], 1);
}
double finalOutput = calculateNeuronOutput(&outputLayer[0],hiddenLayerOutput, 0);
printf("Input: (%f, %f, %f), Exact Output: %f, Predicton: %f\n",
predictionInput[i][0], predictionInput[i][1], predictionInput[i][2], targetOutput[i], finalOutput);
}
return 0;
}
我可以得到下面的输出,但在最后一行我得到了这个错误。
{纪元:0,损失:0.526654 纪元:1000,损失:0.003173 纪元:2000,损失:0.001147 纪元:3000,损失:0.000665 纪元:4000,损失:0.000458 纪元:5000,损失:0.000346 纪元:6000,损失:0.000275 纪元:7000,损失:0.000228 纪元:8000,损失:0.000194 纪元:9000,损失:0.000168 纪元:10000,损失:0.000148 纪元:11000,损失:0.000132 纪元:12000,损失:0.000119 纪元:13000,损失:0.000108 纪元:14000,损失:0.000099 纪元:15000,损失:0.000091 纪元:16000,损失:0.000085 纪元:17000,损失:0.000079 纪元:18000,损失:0.000074 纪元:19000,损失:0.000069 输入:(0.000000, 0.000000, 1.000000),实际输出:0.000000,预测:0.006382 输入:(0.000000, 1.000000, 0.000000),实际输出:1.000000,预测:0.999901 输入:(0.000000, 1.000000, 1.000000),实际输出:1.000000,预测:0.992201 输入:(1.000000, 1.000000, 1.000000),实际输出:0.000000,预测:0.005400 }
这是错误{运行时检查失败#2 - 变量“outputLayer”周围的堆栈已损坏。}
至少这个,在
backPropagate()
:
double hiddenLayer_error[HIDDEN_SIZE];
for (int i = 0; i < HIDDEN_SIZE; i++) {
hiddenLayer_error[i] = output_delta * outputLayer[0].weights[i];
}
outputLayer[0].weights[]
是一个大小为 3 的数组。
您的循环将该数组索引为从 0 到 7。