提交 d6ec64af 编写于 作者: T tostq

add CNN

上级
文件已添加

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CNN", "CNN\CNN.vcxproj", "{2FAE4334-982D-4C6B-8901-DDBF4138C59A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2FAE4334-982D-4C6B-8901-DDBF4138C59A}.Debug|Win32.ActiveCfg = Debug|Win32
{2FAE4334-982D-4C6B-8901-DDBF4138C59A}.Debug|Win32.Build.0 = Debug|Win32
{2FAE4334-982D-4C6B-8901-DDBF4138C59A}.Release|Win32.ActiveCfg = Release|Win32
{2FAE4334-982D-4C6B-8901-DDBF4138C59A}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
文件已添加
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{2FAE4334-982D-4C6B-8901-DDBF4138C59A}</ProjectGuid>
<RootNamespace>CNN</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<HeapReserveSize>2000</HeapReserveSize>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="cnn.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="mat.cpp" />
<ClCompile Include="minst.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="cnn.h" />
<ClInclude Include="mat.h" />
<ClInclude Include="minst.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="cnn.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="minst.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="mat.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="cnn.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="minst.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mat.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
\ No newline at end of file
B^E:\CODE\VS2010 CODE\CNN\CNN\MINST.CPP
B^E:\CODE\VS2010 CODE\CNN\CNN\CNN.CPP|E:\CODE\VS2010 CODE\CNN\CNN\MAIN.CPP|E:\CODE\VS2010 CODE\CNN\CNN\MAT.CPP|E:\CODE\VS2010 CODE\CNN\CNN\MINST.CPP
E:\Code\VS2010 code\CNN\CNN\Debug\cl.command.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\CL.read.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\CL.write.1.tlog
E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.EXE.INTERMEDIATE.MANIFEST
E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.OBJ
E:\Code\VS2010 code\CNN\CNN\Debug\CNN.vcxprojResolveAssemblyReference.cache
E:\Code\VS2010 code\CNN\CNN\Debug\CNN.write.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\link.command.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\link.read.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\link.write.1.tlog
E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAIN.OBJ
E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAT.OBJ
E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MINST.OBJ
E:\Code\VS2010 code\CNN\CNN\Debug\mt.command.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\mt.read.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\mt.write.1.tlog
E:\Code\VS2010 code\CNN\CNN\Debug\vc100.idb
E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\VC100.PDB
E:\CODE\VS2010 CODE\CNN\DEBUG\CNN.EXE
E:\CODE\VS2010 CODE\CNN\DEBUG\CNN.ILK
E:\CODE\VS2010 CODE\CNN\DEBUG\CNN.PDB
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
#v4.0:v100:false
Debug|Win32|E:\Code\VS2010 code\CNN\|
生成启动时间为 2016/6/30 21:07:01。
1>项目“E:\Code\VS2010 code\CNN\CNN\CNN.vcxproj”在节点 2 上(build 个目标)。
1>InitializeBuildStatus:
正在创建“Debug\CNN.unsuccessfulbuild”,因为已指定“AlwaysCreate”。
ClCompile:
d:\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt cnn.cpp
cnn.cpp
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(58): warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(151): warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(193): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\microsoft visual studio 10.0\vc\include\stdio.h(234) : 参见“fopen”的声明
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(225): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\microsoft visual studio 10.0\vc\include\stdio.h(234) : 参见“fopen”的声明
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(291): warning C4244: “=”: 从“double”转换到“float”,可能丢失数据
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(537): warning C4244: “参数”: 从“double”转换到“float”,可能丢失数据
1>e:\code\vs2010 code\cnn\cnn\cnn.cpp(620): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\microsoft visual studio 10.0\vc\include\stdio.h(234) : 参见“fopen”的声明
Link:
d:\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"E:\Code\VS2010 code\CNN\Debug\CNN.exe" /NOLOGO libCbc.lib libCgl.lib libClp.lib libCoinUtils.lib libOsi.lib libOsiClp.lib opencv_calib3d2410d.lib opencv_contrib2410d.lib opencv_core2410d.lib opencv_features2d2410d.lib opencv_flann2410d.lib opencv_gpu2410d.lib opencv_highgui2410d.lib opencv_imgproc2410d.lib opencv_legacy2410d.lib opencv_ml2410d.lib opencv_objdetect2410d.lib opencv_ts2410d.lib opencv_video2410d.lib opencv_nonfree2410d.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Debug\CNN.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"E:\Code\VS2010 code\CNN\Debug\CNN.pdb" /HEAP:"2000" /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"E:\Code\VS2010 code\CNN\Debug\CNN.lib" /MACHINE:X86 Debug\cnn.obj
Debug\main.obj
Debug\mat.obj
Debug\minst.obj
LINK : 没有找到 E:\Code\VS2010 code\CNN\Debug\CNN.exe 或上一个增量链接没有生成它;正在执行完全链接
CNN.vcxproj -> E:\Code\VS2010 code\CNN\Debug\CNN.exe
Manifest:
c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mt.exe /nologo /verbose /outputresource:"E:\Code\VS2010 code\CNN\Debug\CNN.exe;#1" /manifest Debug\CNN.exe.intermediate.manifest
FinalizeBuildStatus:
正在删除文件“Debug\CNN.unsuccessfulbuild”。
正在对“Debug\CNN.lastbuildstate”执行 Touch 任务。
1>已完成生成项目“E:\Code\VS2010 code\CNN\CNN\CNN.vcxproj”(build 个目标)的操作。
生成成功。
已用时间 00:00:01.36
B^E:\CODE\VS2010 CODE\CNN\CNN\CNN.CPP
B^E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAIN.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAT.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MINST.OBJ
B^E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAIN.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAT.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MINST.OBJ
B^E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAIN.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MAT.OBJ|E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\MINST.OBJ
B^E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.EXE.INTERMEDIATE.MANIFEST
B^E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.EXE.INTERMEDIATE.MANIFEST
B^E:\CODE\VS2010 CODE\CNN\CNN\DEBUG\CNN.EXE.INTERMEDIATE.MANIFEST
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <random>
#include <time.h>
#include "cnn.h"
void cnnsetup(CNN* cnn,nSize inputSize,int outputSize)
{
cnn->layerNum=5;
nSize mapSize;
mapSize.w=inputSize.w;
mapSize.h=inputSize.h;
cnn->C1=initCovLayer(mapSize.w,mapSize.h,5,6,1,6);
mapSize.w=mapSize.w-5+1;
mapSize.h=mapSize.h-5+1;
cnn->S2=initPoolLayer(mapSize.w,mapSize.h,2,6,6,AvePool);
mapSize.w=mapSize.w/2;
mapSize.h=mapSize.h/2;
cnn->C3=initCovLayer(mapSize.w,mapSize.h,5,12,6,12);
mapSize.w=mapSize.w-5+1;
mapSize.h=mapSize.h-5+1;
cnn->S4=initPoolLayer(mapSize.w,mapSize.h,2,12,12,AvePool);
mapSize.w=mapSize.w/2;
mapSize.h=mapSize.h/2;
cnn->O5=initOutLayer(mapSize.w*mapSize.h*12,outputSize);
cnn->e=(float*)calloc(cnn->O5->outputNum,sizeof(float));
}
CovLayer* initCovLayer(int inputWidth,int inputHeight,int mapSize,int mapNum,int inChannels,int outChannels)
{
CovLayer* covL=(CovLayer*)malloc(sizeof(CovLayer));
covL->inputHeight=inputHeight;
covL->inputWidth=inputWidth;
covL->mapSize=mapSize;
covL->mapNum=mapNum;
covL->inChannels=inChannels;
covL->outChannels=outChannels;
covL->isFullConnect=true; // 默认为全连接
covL->DataSize=mapSize*mapSize*inChannels*outChannels;
covL->mapData=(float*)malloc((covL->DataSize)*sizeof(float));
covL->basicData=(float*)calloc(outChannels,sizeof(float));
int outW=inputWidth-mapSize+1;
int outH=inputHeight-mapSize+1;
covL->d=(float*)calloc(outH*outW*outChannels,sizeof(float));
covL->v=(float*)calloc(outH*outW*outChannels,sizeof(float));
covL->y=(float*)calloc(outH*outW*outChannels,sizeof(float));
// 特征模板的初始化
int i=0;
srand((unsigned)time(NULL));
for(i=0;i<covL->DataSize;i++){
float randnum=(((float)rand()/(float)RAND_MAX)-0.5)*2; // 产生一个-1到1的随机数
covL->mapData[i]=randnum*sqrt((float)6.0/(float)(mapSize*mapSize*(inChannels+outChannels)));
}
return covL;
}
PoolLayer* initPoolLayer(int inputWidth,int inputHeight,int mapSize,int inChannels,int outChannels,int poolType)
{
PoolLayer* poolL=(PoolLayer*)malloc(sizeof(PoolLayer));
poolL->inputHeight=inputHeight;
poolL->inputWidth=inputWidth;
poolL->mapSize=mapSize;
poolL->inChannels=inChannels;
poolL->outChannels=outChannels;
poolL->poolType=poolType;
poolL->basicData=(float*)calloc(outChannels,sizeof(float));
int outW=inputWidth/mapSize;
int outH=inputHeight/mapSize;
poolL->d=(float*)calloc(outH*outW*outChannels,sizeof(float));
poolL->y=(float*)calloc(outH*outW*outChannels,sizeof(float));
return poolL;
}
OutLayer* initOutLayer(int inputNum,int outputNum)
{
OutLayer* outL=(OutLayer*)malloc(sizeof(OutLayer));
outL->inputNum=inputNum;
outL->outputNum=outputNum;
outL->wData=(float*)malloc(inputNum*outputNum*sizeof(float));
outL->basicData=(float*)calloc(outputNum,sizeof(float));
outL->d=(float*)calloc(outputNum,sizeof(float));
outL->v=(float*)calloc(outputNum,sizeof(float));
outL->y=(float*)calloc(outputNum,sizeof(float));
// 权重的初始化
int i=0;
srand((unsigned)time(NULL));
for(i=0;i<(inputNum*outputNum);i++){
float randnum=(((float)rand()/(float)RAND_MAX)-0.5)*2; // 产生一个-1到1的随机数
outL->wData[i]=randnum*sqrt((float)6.0/(float)(inputNum+outputNum));
}
outL->isFullConnect=true;
return outL;
}
void cnntrain(CNN* cnn,nSize inputSize,int outputSize,
float* inputData,float* outputData,int dataNum,CNNOpts opts)
{
int e;
for(e=0;e<opts.numepochs;e++){
int n=0;
for(n=0;n<dataNum;n++){
float* indata=&inputData[n*inputSize.w*inputSize.h];
float* outdata=&outputData[n*outputSize];
cnnff(cnn,indata); // 前向传播,这里主要计算各
cnnbp(cnn,outdata); // 后向传播,这里主要计算各神经元的误差梯度
cnnapplygrads(cnn,opts); // 更新权重
}
}
}
void cnnff(CNN* cnn,float* inputData)
{
int outSizeW=cnn->S2->inputWidth;
int outSizeH=cnn->S2->inputHeight;
// 第一层的传播
int i,j;
// 第一层输出数据
float** C1outData=(float**)malloc((cnn->S2->inChannels)*sizeof(float*));
for(i=0;i<(cnn->C1->inChannels);i++)
for(j=0;j<cnn->C1->outChannels;j++){
float* map=&cnn->C1->mapData[(cnn->C1->mapSize*cnn->C1->mapSize)*j];
nSize mapSize={cnn->C1->mapSize,cnn->C1->mapSize};
nSize inSize={cnn->C1->inputWidth,cnn->C1->inputHeight};
float bas=cnn->C1->basicData[j];
float* outtemp=cov(map,mapSize,inputData,inSize);
int k; // v的赋值
for(k=0;k<outSizeW*outSizeH;k++)
cnn->C1->v[j*outSizeH*outSizeW+k]=outtemp[k];
C1outData[j]=activation_Sigma(outtemp,outSizeW*outSizeH,bas);
for(k=0;k<outSizeW*outSizeH;k++) // y的赋值
cnn->C1->y[j*outSizeH*outSizeW+k]=C1outData[j][k];
}
// 第二层的输出传播
outSizeH=cnn->S2->inputHeight/cnn->S2->mapSize;
outSizeW=cnn->S2->inputWidth/cnn->S2->mapSize;
float** S2outData=(float**)malloc((cnn->S2->outChannels)*sizeof(float*));
for(i=0;i<(cnn->S2->outChannels);i++){
float* input=C1outData[i];
nSize inputSize={cnn->S2->inputWidth,cnn->S2->inputHeight};
int mapSize=cnn->S2->mapSize;
if(cnn->S2->poolType==AvePool)
S2outData[i]=avgPooling(input,inputSize,mapSize);
int k;
for(k=0;k<outSizeW*outSizeH;k++) // y的赋值
cnn->S2->y[i*outSizeH*outSizeW+k]=S2outData[i][k];
}
// 第三层输出传播,这里是全连接
outSizeW=cnn->S4->inputWidth;
outSizeH=cnn->S4->inputHeight;
float** C3outData=(float**)malloc((cnn->S4->inChannels)*sizeof(float*));
for(j=0;j<cnn->C3->outChannels;j++){
float* z=(float*)calloc(outSizeW*outSizeH,sizeof(float));
float* map=&cnn->C3->mapData[(cnn->C3->mapSize*cnn->C3->mapSize)*j];
nSize mapSize={cnn->C3->mapSize,cnn->C3->mapSize};
nSize inSize={cnn->C3->inputWidth,cnn->C3->inputHeight};
float bas=cnn->C3->basicData[j];
for(i=0;i<(cnn->C3->inChannels);i++){
float* outtemp=cov(map,mapSize,S2outData[i],inSize);
int s;
for(s=0;s<(outSizeH*outSizeW);s++)
z[s]=z[s]+outtemp[s];
}
int k; // v的赋值
for(k=0;k<outSizeW*outSizeH;k++)
cnn->C3->v[j*outSizeH*outSizeW+k]=z[k];
C3outData[j]=activation_Sigma(z,outSizeW*outSizeH,bas);
for(k=0;k<outSizeW*outSizeH;k++) // y的赋值
cnn->C3->y[j*outSizeH*outSizeW+k]=C3outData[j][k];
}
// 第四层的输出传播
outSizeH=cnn->S4->inputHeight/cnn->S4->mapSize;
outSizeW=cnn->S4->inputWidth/cnn->S4->mapSize;
float** S4outData=(float**)malloc((cnn->S4->outChannels)*sizeof(float*));
for(i=0;i<(cnn->S4->outChannels);i++){
float* input=C3outData[i];
nSize inputSize={cnn->S4->inputWidth,cnn->S4->inputHeight};
int mapSize=cnn->S4->mapSize;
if(cnn->S4->poolType==AvePool)
S4outData[i]=avgPooling(input,inputSize,mapSize);
int k;
for(k=0;k<outSizeW*outSizeH;k++) // y的赋值
cnn->S4->y[i*outSizeH*outSizeW+k]=S4outData[i][k];
}
// 输出层的处理
float* O5inData=(float*)malloc((cnn->O5->inputNum)*sizeof(float)); //展开得到输出层的向量
int blocksize=cnn->O5->inputNum/(cnn->S4->outChannels);
for(i=0;i<(cnn->S4->outChannels);i++)
for(j=0;j<blocksize;j++)
O5inData[i*blocksize+j]=S2outData[i][j];
nSize nnSize={cnn->O5->inputNum,cnn->O5->outputNum};
float* z=nnff(O5inData,cnn->O5->wData,cnn->O5->basicData,nnSize);
int k; // v的赋值
for(k=0;k<nnSize.h;k++)
cnn->O5->v[k]=z[k];
float* O5outData=activation_Sigma(z,nnSize.h,0);
for(k=0;k<nnSize.h;k++)
cnn->O5->y[k]=O5outData[k];
free(z);
// 释放相关动态数组
// C1outData
for(i=0;i<(cnn->S2->inChannels);i++){
float* temp=C1outData[i];
free(temp);
temp=NULL;
}
free(C1outData);
// S2outData
for(i=0;i<(cnn->S2->outChannels);i++){
float* temp=S2outData[i];
free(temp);
temp=NULL;
}
free(S2outData);
// C3outData
for(i=0;i<(cnn->S4->inChannels);i++){
float* temp=C3outData[i];
free(temp);
temp=NULL;
}
free(C3outData);
// S4outData
for(i=0;i<(cnn->S4->outChannels);i++){
float* temp=S4outData[i];
free(temp);
temp=NULL;
}
free(S4outData);
// O5inData
free(O5inData);
}
float* cov(float* map,nSize mapSize,float* inputData,nSize inSize) // 卷积操作
{
int outSizeW=inSize.w-mapSize.w+1;
int outSizeH=inSize.h-mapSize.h+1;
float* outputData=(float*)calloc(outSizeW*outSizeH,sizeof(float));
int i,j;
int m,n;// 卷积模块的大小
int halfmapsize=(mapSize.w-1)/2; // 卷积模块的半瓣大小
for(i=0;i<outSizeW;i++)
for(j=0;j<outSizeH;j++)
for(m=0;m<mapSize.w;m++)
for(n=0;n<mapSize.h;n++){
outputData[i+j*outSizeW]=outputData[i+j*outSizeW]+map[m+n*mapSize.w]*
inputData[i+halfmapsize-m+(j+halfmapsize-n)*inSize.w];
}
return outputData;
}
// 激活函数 input是数据,inputNum说明数据数目,bas表明偏置
float* activation_Sigma(float* input,int inputNum,float bas) // sigma激活函数
{
int i;
float* output=(float*)malloc(inputNum*sizeof(float));
for(i=0;i<inputNum;i++){
float temp=input[i]+bas;
output[i]=(float)1.0/((float)(1.0+exp(-temp)));
}
return output;
}
float* avgPooling(float* input,nSize inputSize,int mapSize) // 求平均值
{
int outputW=inputSize.w/mapSize;
int outputH=inputSize.h/mapSize;
float* output=(float*)malloc((outputW*outputH)*sizeof(float));
int i,j;
int m,n;
for(i=0;i<outputW;i++)
for(j=0;j<outputH;j++)
{
float sum=0;
for(m=i;m<i+mapSize;m++)
for(n=j;n<j+mapSize;n++)
sum=sum+input[m+n*inputSize.w];
output[i+j*outputW]=sum/(mapSize*mapSize);
}
return output;
}
// 单层全连接神经网络的前向传播
float vecMulti(float* vec1,float* vec2,int vecL)// 两向量相乘
{
int i;
float m=0;
for(i=0;i<vecL;i++)
m=m+vec1[i]*vec2[i];
return m;
}
float* nnff(float* input,float* wdata,float* bas,nSize nnSize)
{
int w=nnSize.w;
int h=nnSize.h;
float* z=(float*)malloc(h*sizeof(float));
int i;
for(i=0;i<h;i++){
float* wi=&wdata[w*i];
z[i]=vecMulti(input,wi,w)+bas[i];
}
return z;
}
float sigma_derivation(float y){ // Logic激活函数的自变量微分
return y*(1-y); // 这里y是指经过激活函数的输出值,而不是自变量
}
float* vecExpand(float* vec,nSize vecSize,int addw,int addh){ // 向量的扩充,宽增加addw倍,高增加addh倍
int w=vecSize.w;
int h=vecSize.h;