用C语言手写一个神经网络

该程序是模拟tensflow游乐场写的,实现了基本的神经网络效果并验证通过,不多废话,上代码。

核心代码在nn.c中,包含激活函数和损失函数,前向传播,反向传播以及更新权重与偏执的函数。

#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include "config.h"
#include "dataset.h"
#include "nn.h"

int networkShape[] = {2, 8, 8, 8, 8, 8, 8, 1};
NODE **network;

double getOutPut()
{
return network[sizeof(networkShape) / sizeof(int) - 1][0].output;
}

double square(double output, double target)
{
double r = output - target;
return r * r / 2;
}

double squareder(double output, double target)
{
return output - target;
}

double activation(double x)
{
#if ACTIVATIONFUNCTION == RELU
if (x > 0)
{
return x;
}
else
{
return 0;
}
#elif ACTIVATIONFUNCTION == TANH
return tanh(x);
#endif
}

double activationder(double x)
{
#if ACTIVATIONFUNCTION == RELU
if (x > 0)
{
return 1;
}
else
{
return 0;
}
#elif ACTIVATIONFUNCTION == TANH
// tanh的倒数
double y = tanh(x);
return 1 - y * y;
#endif
}

double outlayeractivation(double x)
{
#if OUTLAYERACTIVATIONFUNCTION == TANH
return tanh(x);
#endif
}

double outlayeractivationder(double x)
{
#if OUTLAYERACTIVATIONFUNCTION == TANH
// tanh的倒数
double y = tanh(x);
return 1 - y * y;
#endif
}

void buildNetwork()
{
network = (PPNODE)malloc((sizeof(networkShape) / sizeof(int)) * sizeof(PNODE));
// 输入层
network[0] = (PNODE)malloc(networkShape[0] * sizeof(NODE));
// 隐藏层与输出层
for (int i = 1, leni = sizeof(networkShape) / sizeof(int); i < leni; i++)
{
network[i] = (PNODE)malloc(networkShape[i] * sizeof(NODE));
int prenodeNum = networkShape[i - 1];
for (int j = 0, lenj = networkShape[i]; j < lenj; j++)
{
network[i][j].link = (PLINK)malloc(prenodeNum * sizeof(LINK));
}
}
// 输入层
for (int i = 0; i < networkShape[0]; i++)
{
network[0][i].bias = 0.1;
}
// 隐藏层与输出层
for (int i = 1, leni = sizeof(networkShape) / sizeof(int); i < leni; i++)
{
for (int j = 0, lenj = networkShape[i]; j < lenj; j++)
{
network[i][j].bias = 0.1;
network[i][j].inputDer = 0;
network[i][j].outputDer = 0;
network[i][j].accInputDer = 0;
network[i][j].numAccumulatedDers = 0;
for (int k = 0, lenk = networkShape[i - 1]; k < lenk; k++)
{
network[i][j].link[k].weight = (double)rand() / RAND_MAX - 0.5;
network[i][j].link[k].errorDer = 0;
network[i][j].link[k].accErrorDer = 0;
network[i][j].link[k].numAccumulatedDers = 0;
}
}
}
}

void forwardProp(POINT point)
{
int outlayerNum = sizeof(networkShape) / sizeof(int) - 1; // 输出层所在层
// 输入层
network[0][0].output = point.x;
network[0][1].output = point.y;
// 隐藏层
for (int i = 1, leni = outlayerNum; i < leni; i++)
{
for (int j = 0, lenj = networkShape[i]; j < lenj; j++)
{
network[i][j].totalInput = network[i][j].bias;
for (int k = 0, lenk = networkShape[i - 1]; k < lenk; k++)
{
network[i][j].totalInput += network[i][j].link[k].weight * network[i - 1][k].output;
}
network[i][j].output = activation(network[i][j].totalInput);
}
}
// 输出层
for (int i = 0, leni = networkShape[outlayerNum]; i < leni; i++)
{
network[outlayerNum][i].totalInput = network[outlayerNum][i].bias;
for (int j = 0, lenj = networkShape[outlayerNum - 1]; j < lenj; j++)
{
network[outlayerNum][i].totalInput += network[outlayerNum][i].link[j].weight * network[outlayerNum - 1][j].output;
}
network[outlayerNum][i].output = outlayeractivation(network[outlayerNum][i].totalInput);
}
}

void backProp(POINT point)
{
// 清空所有节点的outputDer
for (int i = 0, leni = sizeof(networkShape) / sizeof(int); i < leni; i++)
{
for (int j = 0; j < networkShape[i]; j++)

{
network[i][j].outputDer = 0;
}
}
int outlayerNum = sizeof(networkShape) / sizeof(int) - 1; // 输出层所在层
// 输出层
for (int i = 0, leni = networkShape[outlayerNum]; i < leni; i++)
{
network[outlayerNum][i].outputDer = squareder(network[outlayerNum][i].output, point.label); // 目标和结果的差距
network[outlayerNum][i].inputDer = network[outlayerNum][i].outputDer * outlayeractivationder(network[outlayerNum][i].totalInput);
network[outlayerNum][i].accInputDer += network[outlayerNum][i].inputDer;
network[outlayerNum][i].numAccumulatedDers++;
for (int j = 0, lenj = networkShape[outlayerNum]; j < lenj; j++)
{
network[outlayerNum][i].link[i].errorDer = network[outlayerNum][i].inputDer * network[outlayerNum - 1][i].output;
network[outlayerNum][i].link[i].accErrorDer += network[outlayerNum][i].link[i].errorDer;
network[outlayerNum][i].link[i].numAccumulatedDers++;
network[outlayerNum - 1][i].outputDer += network[outlayerNum][i].link[i].weight * network[outlayerNum][i].inputDer;
}
}
// 隐藏层
for (int i = outlayerNum; i > 0; i--)
{
for (int j = 0; j < networkShape[i]; j++)
{
network[i][j].inputDer = network[i][j].outputDer * activationder(network[i][j].totalInput);
network[i][j].accInputDer += network[i][j].inputDer;
network[i][j].numAccumulatedDers++;
for (int k = 0; k < networkShape[i - 1]; k++)
{
network[i][j].link[k].errorDer = network[i][j].inputDer * network[i - 1][k].output;
network[i][j].link[k].accErrorDer += network[i][j].link[k].errorDer;
network[i][j].link[k].numAccumulatedDers++;
network[i - 1][k].outputDer += network[i][j].link[k].weight * network[i][j].inputDer;
}
}
}
}

void updateWeights()
{
// 隐藏层与输出层
for (int i = 1; i < sizeof(networkShape) / sizeof(int); i++)
{
for (int j = 0; j < networkShape[i]; j++)
{
if (network[i][j].numAccumulatedDers > 0)
{
network[i][j].bias -= LEARNINGRATE * network[i][j].accInputDer / network[i][j].numAccumulatedDers;
network[i][j].accInputDer = 0;
network[i][j].numAccumulatedDers = 0;
}
for (int k = 0; k < networkShape[i - 1]; k++)
{
if (network[i][j].link[k].numAccumulatedDers > 0)
{
network[i][j].link[k].weight -= LEARNINGRATE * network[i][j].link[k].accErrorDer / network[i][j].link[k].numAccumulatedDers;
network[i][j].link[k].accErrorDer = 0;
network[i][j].link[k].numAccumulatedDers = 0;
}
}
}
}
}

对应头文件为nn.h

#ifndef __NN_H__
#define __NN_H__

#include "config.h"

typedef struct LINK
{
double weight;
double errorDer;
double accErrorDer;
int numAccumulatedDers;
} LINK;
typedef LINK *PLINK;

typedef struct NODE
{
double bias;
PLINK link;
double output;
double inputDer;
double outputDer;
double accInputDer;
int numAccumulatedDers;
double totalInput;
} NODE;
typedef NODE *PNODE;
typedef PNODE *PPNODE;

double getOutPut();
double square(double output, double target);
double squareder(double output, double target);
double tanhder(double x); // tanh的倒数
double activation(double x);
double activationder(double x);
double outlayeractivation(double x);
double outlayeractivationder(double x);
void buildNetwork();
void forwardProp(POINT point);
void backProp(POINT point);
void updateWeights();

#endif

自动创建与生成训练集与测试集的程序,这里就创建了一个基于半径为5的圆型,圆中间是一部分数据,圆外围是一部分数据。

#include <stdlib.h>
#include <math.h>
#include "config.h"
#include "dataset.h"

POINT points[NUMSAMPLES];

void shuffle()
{
for (int i = 0; i < NUMSAMPLES; i++)
{
int index = i * ((double)rand() / RAND_MAX);
POINT point = points[i];
points[i] = points[index];
points[index] = point;
}
}

// 创建NUMSAMPLES个参数,按照原型来创建
void classifyCircleData()
{
double radius = 5;
// 创建内部圆上的点
for (int i = 0; i < NUMSAMPLES / 2; i++)
{
double r = 0.5 * radius * rand() / RAND_MAX; // 生成随机的半径
double angle = 2.0 * M_PI * rand() / RAND_MAX; // 生成随机的角度
points[i].x = r * cos(angle);
points[i].y = r * sin(angle);
points[i].label = 1;
}
// 创建外部圆上的点
for (int i = NUMSAMPLES / 2; i < NUMSAMPLES; i++)
{
double r = 0.7 * radius + 0.3 * radius * rand() / RAND_MAX; // 生成随机的半径
double angle = 2.0 * M_PI * rand() / RAND_MAX; // 生成随机的角度
points[i].x = r * cos(angle);
points[i].y = r * sin(angle);
points[i].label = -1;
}
shuffle();
}

对应头文件为dataset.h

#ifndef __DATASET_H__
#define __DATASET_H__

typedef struct
{
double x;
double y;
double label;
} POINT;

void classifyCircleData();

#endif

程序配置部分为config.h,定义了数据集大小,学习率以及batchsize大小,还有激活函数,损失函数等应该选什么。

#ifndef __CONFIG_H__ #define __CONFIG_H__ #define NUMSAMPLES 500 // 创建测试点的数量,其中前一半作为训练集,后一半作为测试集 #ifndef __CONFIG_H__

#define __CONFIG_H__

#define NUMSAMPLES 500 // 创建测试点的数量,其中前一半作为训练集,后一半作为测试集
#define LEARNINGRATE 0.03
#define BATCHSIZE 10
#define ACTIVATIONFUNCTION RELU
#define OUTLAYERACTIVATIONFUNCTION TANH

#endif

main.c主要是调用上述函数,初始化网络以及数据集,以及训练。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "config.h"
#include "dataset.h"
#include "nn.h"

extern POINT points[NUMSAMPLES];

double getLoss(int mode) // 0代表训练集,1代表测试集
{
double loss = 0;
if (mode)
{
for (int i = NUMSAMPLES / 2; i < NUMSAMPLES; i++)
{
forwardProp(points[i]);
loss += square(getOutPut(), points[i].label);
}
}
else
{
for (int i = 0; i < NUMSAMPLES / 2; i++)
{
forwardProp(points[i]);
loss += square(getOutPut(), points[i].label);
}
}
return loss / (NUMSAMPLES / 2);
}

void training()
{
for (int i = 0; i < NUMSAMPLES / 2; i++)
{
forwardProp(points[i]);
backProp(points[i]);
if ((i + 1) % BATCHSIZE == 0)
{
updateWeights();
}
}
double lossTrain = getLoss(0);
double lossTest = getLoss(1);
printf("lossTrain:%f,lossTest:%f\n", lossTrain, lossTest);
}

int main(int argc, char **argv)
{
srand((unsigned)time(NULL));
classifyCircleData();
buildNetwork();
double lossTrain = getLoss(0);
double lossTest = getLoss(1);
printf("lossTrain:%f,lossTest:%f\n", lossTrain, lossTest);
for (int i = 0; i < 100; i++)
{
training();
}
return 0;
}

代码完整地址为:https://github.com/worldflyingct/ann

后期可能会根据我学习的深入继续更新这份代码,就不另行通知了。


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容