Skip to content

Commit 649b15e

Browse files
committed
Added main.cpp
1 parent 094b5ca commit 649b15e

File tree

6 files changed

+214
-2
lines changed

6 files changed

+214
-2
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
#
35+
*.vcxproj.user
36+
*.aps
37+
38+
# Folder
39+
.vs
40+
.vscode
41+
_Output
42+
x64
43+
build

Common.props

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ImportGroup Label="PropertySheets" />
4+
<PropertyGroup Label="UserMacros" />
5+
<PropertyGroup>
6+
<OutDir>$(SolutionDir)\_Output\$(Platform)_$(Configuration)\</OutDir>
7+
<IntDir>$(SolutionDir)\_Output\$(Platform)_$(Configuration)\Intermediate\$(ProjectName)\</IntDir>
8+
<IncludePath>$(SolutionDir);$(IncludePath)</IncludePath>
9+
</PropertyGroup>
10+
<ItemDefinitionGroup>
11+
<ClCompile>
12+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
13+
<PreprocessorDefinitions>__FF_WINDOWS__;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
14+
<WarningLevel>Level3</WarningLevel>
15+
</ClCompile>
16+
</ItemDefinitionGroup>
17+
<ItemGroup />
18+
</Project>

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
# SimpleCudaNeuralNet
2-
Concise neural network with C++ and Cuda
1+
# SimpleCudaNeuralNet
2+
This is a simple code for self-studying both neural network and Cuda.
3+
4+
## Status
5+
### Layers
6+
* Fully connected layer
7+
### Activation
8+
* Relu activation function
9+
### Loss functions
10+
* Cost : Sum of Squares
11+
### Learning
12+
* Adam
13+
14+
15+
16+

SimpleCudaNn.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30413.136
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimpleCudaNn", "SimpleCudaNn.vcxproj", "{E98B4D14-5020-4EDE-AF6F-4769B2A4D2E5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Release|x64 = Release|x64
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E98B4D14-5020-4EDE-AF6F-4769B2A4D2E5}.Debug|x64.ActiveCfg = Debug|x64
15+
{E98B4D14-5020-4EDE-AF6F-4769B2A4D2E5}.Debug|x64.Build.0 = Debug|x64
16+
{E98B4D14-5020-4EDE-AF6F-4769B2A4D2E5}.Release|x64.ActiveCfg = Release|x64
17+
{E98B4D14-5020-4EDE-AF6F-4769B2A4D2E5}.Release|x64.Build.0 = Release|x64
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {326E4A03-6C44-4021-91CF-EF3EADFEA750}
24+
EndGlobalSection
25+
EndGlobal

SimpleCudaNn.vcxproj

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<VCProjectVersion>16.0</VCProjectVersion>
15+
<Keyword>Win32Proj</Keyword>
16+
<ProjectGuid>{e98b4d14-5020-4ede-af6f-4769b2a4d2e5}</ProjectGuid>
17+
<RootNamespace>SimpleCudaNn</RootNamespace>
18+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
22+
<ConfigurationType>Application</ConfigurationType>
23+
<UseDebugLibraries>true</UseDebugLibraries>
24+
<PlatformToolset>v142</PlatformToolset>
25+
<CharacterSet>Unicode</CharacterSet>
26+
</PropertyGroup>
27+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
28+
<ConfigurationType>Application</ConfigurationType>
29+
<UseDebugLibraries>false</UseDebugLibraries>
30+
<PlatformToolset>v142</PlatformToolset>
31+
<WholeProgramOptimization>true</WholeProgramOptimization>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
35+
<ImportGroup Label="ExtensionSettings">
36+
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 11.0.props" />
37+
</ImportGroup>
38+
<ImportGroup Label="Shared">
39+
</ImportGroup>
40+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
41+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
42+
<Import Project="Common.props" />
43+
</ImportGroup>
44+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
45+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
46+
<Import Project="Common.props" />
47+
</ImportGroup>
48+
<PropertyGroup Label="UserMacros" />
49+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
50+
<LinkIncremental>true</LinkIncremental>
51+
</PropertyGroup>
52+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
53+
<LinkIncremental>false</LinkIncremental>
54+
</PropertyGroup>
55+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
56+
<ClCompile>
57+
<WarningLevel>Level3</WarningLevel>
58+
<SDLCheck>true</SDLCheck>
59+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
60+
<ConformanceMode>true</ConformanceMode>
61+
</ClCompile>
62+
<Link>
63+
<SubSystem>Console</SubSystem>
64+
<GenerateDebugInformation>true</GenerateDebugInformation>
65+
</Link>
66+
</ItemDefinitionGroup>
67+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
68+
<ClCompile>
69+
<WarningLevel>Level3</WarningLevel>
70+
<FunctionLevelLinking>true</FunctionLevelLinking>
71+
<IntrinsicFunctions>true</IntrinsicFunctions>
72+
<SDLCheck>true</SDLCheck>
73+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
74+
<ConformanceMode>true</ConformanceMode>
75+
</ClCompile>
76+
<Link>
77+
<SubSystem>Console</SubSystem>
78+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
79+
<OptimizeReferences>true</OptimizeReferences>
80+
<GenerateDebugInformation>true</GenerateDebugInformation>
81+
</Link>
82+
</ItemDefinitionGroup>
83+
<ItemGroup>
84+
<CudaCompile Include="ffCudaNn.cpp" />
85+
</ItemGroup>
86+
<ItemGroup>
87+
<ClInclude Include="ffCudaNn.h" />
88+
</ItemGroup>
89+
<ItemGroup>
90+
<ClCompile Include="main.cpp" />
91+
</ItemGroup>
92+
<ItemGroup>
93+
<None Include="README.md" />
94+
</ItemGroup>
95+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
96+
<ImportGroup Label="ExtensionTargets">
97+
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 11.0.targets" />
98+
</ImportGroup>
99+
</Project>

main.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "ffCudaNn.h"
2+
3+
int main()
4+
{
5+
ff::CudaNn nn;
6+
nn.AddFc(1000, 1000);
7+
nn.AddReluFc(1000, 500);
8+
nn.AddReluFc(500, 10);
9+
nn.AddSumOfSquares();
10+
11+
ff::CudaTensor x(1000, 64);
12+
ff::CudaTensor y(10, 64);
13+
x.Random();
14+
y.Random();
15+
16+
const ff::CudaTensor* yPred = nullptr;
17+
for (int i = 0; i < 10000; ++i)
18+
{
19+
yPred = nn.Forward(&x);
20+
if (nullptr == yPred)
21+
{
22+
printf("Error: at Forward()\n");
23+
return -1;
24+
}
25+
26+
nn.Backward(&y);
27+
nn.UpdateWs(0.00001);
28+
29+
ff::CudaTensor* __yPred = const_cast<ff::CudaTensor*>(yPred);
30+
__yPred->Pull();
31+
32+
double loss = 0.0;
33+
for (int r = 0; r < yPred->_d1; ++r)
34+
{
35+
for (int c = 0; c < yPred->_d0; ++c)
36+
{
37+
int index = c + r * yPred->_d0;
38+
double diff = yPred->_data[index] - y._data[index];
39+
loss += (diff * diff);
40+
}
41+
}
42+
printf("loss: %f\n", loss);
43+
}
44+
return 0;
45+
}

0 commit comments

Comments
 (0)