First commit. Create MVS solution. Create sample class. Create functional tests.

This commit is contained in:
davidjacnogueira
2016-01-16 16:46:36 +00:00
parent b8ccc3fcd6
commit 5223a4fc50
8 changed files with 524 additions and 0 deletions

63
.gitattributes vendored Normal file
View File

@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

28
MLP_MVS/MLP_MVS.sln Normal file
View File

@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MLP_MVS", "MLP_MVS.vcxproj", "{6BFA9D94-B136-4985-83A1-EE76FFF6F374}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Debug|x64.ActiveCfg = Debug|x64
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Debug|x64.Build.0 = Debug|x64
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Debug|x86.ActiveCfg = Debug|Win32
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Debug|x86.Build.0 = Debug|Win32
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Release|x64.ActiveCfg = Release|x64
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Release|x64.Build.0 = Release|x64
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Release|x86.ActiveCfg = Release|Win32
{6BFA9D94-B136-4985-83A1-EE76FFF6F374}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

156
MLP_MVS/MLP_MVS.vcxproj Normal file
View File

@@ -0,0 +1,156 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.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>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{6BFA9D94-B136-4985-83A1-EE76FFF6F374}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>MLP_MVS</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</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>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<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|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\src\MLP.h" />
<ClInclude Include="..\src\Sample.h" />
<ClInclude Include="..\src\Utils.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\Main.cpp" />
<ClCompile Include="..\src\MLP.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<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>
<ClInclude Include="..\src\Sample.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\MLP.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\MLP.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -1,2 +1,6 @@
# MLP
Simple multilayer perceptron c++ implementation.
David Nogueira, 2016.01.16

130
src/Main.cpp Normal file
View File

@@ -0,0 +1,130 @@
//============================================================================
// Name : Main.cpp
// Author : David Nogueira
//============================================================================
#include "MLP.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cassert>
void LearnAND() {
std::cout << "Train AND function with mlp." << std::endl;
std::vector<TrainingSample> training_set =
{
{{ 1, 0, 0 },{1,0}},
{{ 1, 0, 1 },{1,0}},
{{ 1, 1, 0 },{1,0}},
{{ 1, 1, 1 },{0,1}}
};
MLP my_mlp(0.1, 100, 0.5);
my_mlp.Train(training_set, 1, 1);
assert(my_mlp.GetOutput({ 1, 0, 0 }) == 0);
assert(my_mlp.GetOutput({ 1, 0, 1 }) == 0);
assert(my_mlp.GetOutput({ 1, 1, 0 }) == 0);
assert(my_mlp.GetOutput({ 1, 1, 1 }) == 1);
std::cout << "Trained with success." << std::endl;
std::cout << std::endl;
}
void LearnNAND() {
std::cout << "Train NAND function with mlp." << std::endl;
std::vector<TrainingSample> training_set =
{
{{ 1, 0, 0 },{0,1}},
{{ 1, 0, 1 },{0,1}},
{{ 1, 1, 0 },{0,1}},
{{ 1, 1, 1 },{1,0}}
};
MLP my_mlp(0.1, 100, 0.5);
my_mlp.Train(training_set, 1, 1);
assert(my_mlp.GetOutput({ 1, 0, 0 }) == 1);
assert(my_mlp.GetOutput({ 1, 0, 1 }) == 1);
assert(my_mlp.GetOutput({ 1, 1, 0 }) == 1);
assert(my_mlp.GetOutput({ 1, 1, 1 }) == 0);
std::cout << "Trained with success." << std::endl;
std::cout << std::endl;
}
void LearnOR() {
std::cout << "Train OR function with mlp." << std::endl;
std::vector<TrainingSample> training_set =
{
{{ 1, 0, 0 },{1,0}},
{{ 1, 0, 1 },{0,1}},
{{ 1, 1, 0 },{0,1}},
{{ 1, 1, 1 },{0,1}}
};
MLP my_mlp(0.1, 100, 0.5);
my_mlp.Train(training_set, 1, 1);
assert(my_mlp.GetOutput({ 1, 0, 0 }) == 0);
assert(my_mlp.GetOutput({ 1, 0, 1 }) == 1);
assert(my_mlp.GetOutput({ 1, 1, 0 }) == 1);
assert(my_mlp.GetOutput({ 1, 1, 1 }) == 1);
std::cout << "Trained with success." << std::endl;
std::cout << std::endl;
}
void LearnNOR() {
std::cout << "Train NOR function with mlp." << std::endl;
std::vector<TrainingSample> training_set =
{
{{ 1, 0, 0 },{0,1}},
{{ 1, 0, 1 },{1,0}},
{{ 1, 1, 0 },{1,0}},
{{ 1, 1, 1 },{1,0}}
};
MLP my_mlp(0.1, 100, 0.5);
my_mlp.Train(training_set, 1, 1);
assert(my_mlp.GetOutput({ 1, 0, 0 }) == 1);
assert(my_mlp.GetOutput({ 1, 0, 1 }) == 0);
assert(my_mlp.GetOutput({ 1, 1, 0 }) == 0);
assert(my_mlp.GetOutput({ 1, 1, 1 }) == 0);
std::cout << "Trained with success." << std::endl;
std::cout << std::endl;
}
void LearnNOT() {
std::cout << "Train NOT function with mlp." << std::endl;
std::vector<TrainingSample> training_set =
{
{{ 1, 0},{0,1}},
{{ 1, 1},{1,1}}
};
MLP my_mlp(0.1, 100, 0.5);
my_mlp.Train(training_set, 1, 1);
assert(my_mlp.GetOutput({ 1, 0 }) == 1);
assert(my_mlp.GetOutput({ 1, 1 }) == 0);
std::cout << "Trained with success." << std::endl;
std::cout << std::endl;
}
int main() {
LearnAND();
LearnNAND();
LearnOR();
LearnNOR();
LearnNOT();
return 0;
}

45
src/Sample.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef TRAININGSAMPLE_H
#define TRAININGSAMPLE_H
#include <stdlib.h>
#include <vector>
class Sample {
public:
Sample(const std::vector<double> & input_vector) {
m_input_vector = input_vector;
}
std::vector<double> & input_vector() {
return m_input_vector;
}
uint32_t GetInputVectorSize() const {
return m_input_vector.size();
}
void AddBiasValue(double bias_value) {
m_input_vector.insert(m_input_vector.begin(), bias_value);
}
protected:
std::vector<double> m_input_vector;
};
class TrainingSample : public Sample {
public:
TrainingSample(const std::vector<double> & input_vector,
const std::vector<double> & output_vector) :
Sample(input_vector) {
m_output_vector = output_vector;
}
std::vector<double> & output_vector() {
return m_output_vector;
}
uint32_t GetOutputVectorSize() const {
return m_output_vector.size();
}
protected:
std::vector<double> m_output_vector;
};
#endif // TRAININGSAMPLE_H

62
src/Utils.h Normal file
View File

@@ -0,0 +1,62 @@
#ifndef UTILS_H
#define UTILS_H
#include <stdlib.h>
#include <math.h>
#include <numeric>
#include <chrono>
#ifdef _WIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
namespace utils {
struct gen_rand {
double factor;
public:
gen_rand(double r = 1.0) : factor(r / RAND_MAX) {}
double operator()() {
return rand() * factor;
}
};
inline double sigmoid(double x) {
//Typical sigmoid function created from input x
//param x: input value
//return: sigmoided value
return 1 / (1 + exp(-x));
}
// Derivative of sigmoid function
inline double deriv_sigmoid(double x) {
return sigmoid(x)*(1 - sigmoid(x));
};
class Chronometer {
public:
Chronometer() {
time_span = std::chrono::steady_clock::duration::zero();
};
virtual ~Chronometer() {};
void GetTime() {
clock_begin = std::chrono::steady_clock::now();
}
void StopTime() {
std::chrono::steady_clock::time_point clock_end = std::chrono::steady_clock::now();
time_span += clock_end - clock_begin;
}
//Return elapsed time in seconds
double GetElapsedTime() {
return double(time_span.count()) *
std::chrono::steady_clock::period::num / std::chrono::steady_clock::period::den;
}
protected:
std::chrono::steady_clock::time_point clock_begin;
std::chrono::steady_clock::duration time_span;
};
}
#endif // UTILS_H