LeNet-5神经网络——C源代码


LeNet-5神经网络 C源代码,这个写的比较好,可以用gcc编译去跑,结合理论可以对深度学习有更深刻的了解 介绍 根据YANN LECUN的论文《Gradient-based Learning Applied To Document Recognition》设计的LeNet-5神经网络,C语言写成,不依赖任何第三方库。 MNIST手写字符集初代训练识别率97%,多代训练识别率98%。 DEMO main.c文件为MNIST数据集的识别DEMO,直接编译即可运行,训练集60000张,测试集10000张。 项目环境 该项目为VISUAL STUDIO 2015项目,用VISUAL STUDIO 2015 UPDATE1及以上直接打开即可编译。采用ANSI C编写,因此源码无须修改即可在其它平台上编译。 如果因缺少openmp无法编译,请将lenet.c中的#include和#pragma omp parallel for删除掉即可。 API #####批量训练 lenet: LeNet5的权值的指针,LeNet5神经网络的核心 inputs: 要训练的多个图片对应unsigned char二维数组的数组,指向的二维数组的batchSize倍大小内存空间指针。在MNIST测试DEMO中二维数组为28x28,每个二维数组数值分别为对应位置图像像素灰度值 resMat:结果向量矩阵 labels:要训练的多个图片分别对应的标签数组。大小为batchSize batchSize:批量训练输入图像(二维数组)的数量 void TrainBatch(LeNet5 *lenet, image *inputs, const char(*resMat)[OUTPUT],uint8 *labels, int batchSize); #####单个训练 lenet: LeNet5的权值的指针,LeNet5神经网络的核心 input: 要训练的图片对应二维数组 resMat:结果向量矩阵 label: 要训练的图片对应的标签 void Train(LeNet5 *lenet, image input, const char(*resMat)[OUTPUT],uint8 label); #####预测 lenet: LeNet5的权值的指针,LeNet5神经网络的核心 input: 输入的图像的数据 labels: 结果向量矩阵指针 count: 结果向量个数 return 返回值为预测的结果 int Predict(LeNet5 *lenet, image input, const char(*labels)[LAYER6], int count); #####初始化 lenet: LeNet5的权值的指针,LeNet5神经网络的核心
资源截图
代码片段和文件信息
#include “lenet.h“
#include 
#include 
#include 
#include 

#define GETLENGTH(array) (sizeof(array)/sizeof(*(array)))

#define GETCOUNT(array)  (sizeof(array)/sizeof(double))

#define FOREACH(icount) for (int i = 0; i < count; ++i)

#define CONVOLUTE_VALID(inputoutputweight)
{
FOREACH(o0GETLENGTH(output))
FOREACH(o1GETLENGTH(*(output)))
FOREACH(w0GETLENGTH(weight))
FOREACH(w1GETLENGTH(*(weight)))
(output)[o0][o1] += (input)[o0 + w0][o1 + w1] * (weight)[w0][w1];
}

#define CONVOLUTE_FULL(inputoutputweight)
{
FOREACH(i0GETLENGTH(input))
FOREACH(i1GETLENGTH(*(input)))
FOREACH(w0GETLENGTH(weight))
FOREACH(w1GETLENGTH(*(weight)))
(output)[i0 + w0][i1 + w1] += (input)[i0][i1] * (weight)[w0][w1];
}

#define CONVOLUTION_FORWARD(inputoutputweightbiasaction)
{
for (int x = 0; x < GETLENGTH(weight); ++x)
for (int y = 0; y < GETLENGTH(*weight); ++y)
CONVOLUTE_VALID(input[x] output[y] weight[x][y]);
FOREACH(j GETLENGTH(output))
FOREACH(i GETCOUNT(output[j]))
((double *)output[j])[i] = action(((double *)output[j])[i] + bias[j]);
}

#define CONVOLUTION_BACKWARD(inputinerrorouterrorweightwdbdactiongrad)
{
for (int x = 0; x < GETLENGTH(weight); ++x)
for (int y = 0; y < GETLENGTH(*weight); ++y)
CONVOLUTE_FULL(outerror[y] inerror[x] weight[x][y]);
FOREACH(i GETCOUNT(inerror))
((double *)inerror)[i] *= actiongrad(((double *)input)[i]);
FOREACH(j GETLENGTH(outerror))
FOREACH(i GETCOUNT(outerror[j]))
bd[j] += ((double *)outerror[j])[i];
for (int x = 0; x < GETLENGTH(weight); ++x)
for (int y = 0; y < GETLENGTH(*weight); ++y)
CONVOLUTE_VALID(input[x] wd[x][y] outerror[y]);
}


#define SUBSAMP_MAX_FORWARD(inputoutput)
{
const int len0 = GETLENGTH(*(input)) / GETLENGTH(*(output));
const int len1 = GETLENGTH(**(input)) / GETLENGTH(**(output));
FOREACH(i GETLENGTH(output))
FOREACH(o0 GETLENGTH(*(output)))
FOREACH(o1 GETLENGTH(**(output)))
{
int x0 = 0 x1 = 0 ismax;
FOREACH(l0 len0)
FOREACH(l1 len1)
{
ismax = input[i][o0*len0 + l0][o1*len1 + l1] > input[i][o0*len0 + x0][o1*len1 + x1];
x0 += ismax * (l0 - x0);
x1 += ismax * (l1 - x1);
}
output[i][o0][o1] = input[i][o0*len0 + x0][o1*len1 + x1];
}
}

#define SUBSAMP_MAX_BACKWARD(inputinerrorouterror)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-01-01 04:35  LeNet-5-master
     文件        2518  2017-01-01 04:35  LeNet-5-master.gitattributes
     文件        3461  2017-01-01 04:35  LeNet-5-master.gitignore
     文件        1078  2017-01-01 04:35  LeNet-5-masterLICENSE
     文件        1275  2017-01-01 04:35  LeNet-5-masterLeNet-5.sln
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeproj
     文件        8202  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojproject.pbxproj
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojproject.xcworkspace
     文件         152  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojproject.xcworkspacecontents.xcworkspacedata
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojproject.xcworkspacexcuserdata
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojproject.xcworkspacexcuserdatafanwenjie.xcuserdatad
     文件       35009  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojproject.xcworkspacexcuserdatafanwenjie.xcuserdatadUserInterfaceState.xcuserstate
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojxcuserdata
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojxcuserdatafanwenjie.xcuserdatad
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojxcuserdatafanwenjie.xcuserdatadxcschemes
     文件        3287  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojxcuserdatafanwenjie.xcuserdatadxcschemesLeNet-5.xcscheme
     文件         479  2017-01-01 04:35  LeNet-5-masterLeNet-5.xcodeprojxcuserdatafanwenjie.xcuserdatadxcschemesxcschememanagement.plist
     目录           0  2017-01-01 04:35  LeNet-5-masterLeNet-5
     文件        7411  2017-01-01 04:35  LeNet-5-masterLeNet-5LeNet-5.vcxproj
     文件        1543  2017-01-01 04:35  LeNet-5-masterLeNet-5LeNet-5.vcxproj.filters
     文件       10348  2017-01-01 04:35  LeNet-5-masterLeNet-5lenet.c
     文件        2115  2017-01-01 04:35  LeNet-5-masterLeNet-5lenet.h
     文件        3285  2017-01-01 04:35  LeNet-5-masterLeNet-5main.c
     文件     7840016  2017-01-01 04:35  LeNet-5-masterLeNet-5 10k-images-idx3-ubyte
     文件       10008  2017-01-01 04:35  LeNet-5-masterLeNet-5 10k-labels-idx1-ubyte
     文件    47040016  2017-01-01 04:35  LeNet-5-masterLeNet-5 rain-images-idx3-ubyte
     文件       60008  2017-01-01 04:35  LeNet-5-masterLeNet-5 rain-labels-idx1-ubyte
     文件        2023  2017-01-01 04:35  LeNet-5-masterREADME.md

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)