用c动态数组(不用c++vector)实现手撸神经网咯230901
用c语言动态数组(不用c++的vector)实现:输入数据inputs = { {1, 1}, {0,0},{1, 0},{0,1} };目标数据targets={0,0,1,1}; 测试数据 inputs22 = { {1, 0}, {1,1},{0,1} }; 构建神经网络,例如:NeuralNetwork nn({ 2, 4,3,1 }); 则网络有四层、输入层2个nodes、输出层1个节点、第1隐藏层4nodes、第2隐藏层3nodes,网络有反向传播、梯度下降适当优化…等。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>#define LEARNING_RATE 0.05// Sigmoid and its derivative
float sigmoid(float x) {return 1 / (1 + exp(-x));
}float sigmoid_derivative(float x) {float sig = sigmoid(x);return sig * (1 - sig);
}typedef struct {float*** weights;int num_layers;int* layer_sizes;float** layer_outputs;float** deltas;
} NeuralNetwork;NeuralNetwork initialize_nn(int* topology, int num_layers) {NeuralNetwork nn;nn.num_layers = num_layers;nn.layer_sizes = topology;// Allocate memory for weights