Initial forward-feeding net

This commit is contained in:
Michael Mandl 2015-03-23 19:28:29 +01:00
parent 341a88ee2e
commit c8e08193f1
5 changed files with 330 additions and 0 deletions

159
Net.h Normal file
View file

@ -0,0 +1,159 @@
#pragma once
#include <vector>
class Neuron
{
private:
double outputValue;
std::vector<double> outputWeights;
public:
void setOutputValue(double value)
{
outputValue = value;
}
static double transferFunction(double inputValue)
{
return std::tanh(inputValue);
}
void feedForward(double inputValue)
{
outputValue = Neuron::transferFunction(inputValue);
}
double getWeightedOutputValue(int outputNeuron) const
{
return outputValue * outputWeights[outputNeuron];
}
void createOutputWeights(unsigned int number)
{
outputWeights.clear();
for (unsigned int i = 0; i < number; ++i)
{
outputWeights.push_back(std::rand() / (double)RAND_MAX);
}
}
double getOutputValue() const
{
return outputValue;
}
};
class Layer : public std::vector < Neuron >
{
public:
Layer(unsigned int numNeurons)
{
for (unsigned int i = 0; i < numNeurons; ++i)
{
push_back(Neuron());
}
}
void setOutputValues(const std::vector<double> & outputValues)
{
if (size() != outputValues.size())
{
throw std::exception("The number of output values has to match the layer size");
}
auto valueIt = outputValues.begin();
for (Neuron &neuron : *this)
{
neuron.setOutputValue(*valueIt++);
}
}
void feedForward(const Layer &inputLayer)
{
int neuronNumber = 0;
for (Neuron &neuron : *this)
{
neuron.feedForward(inputLayer.getWeightedSum(neuronNumber));
}
}
double getWeightedSum(int outputNeuron) const
{
double sum = 0.0;
for (const Neuron &neuron : *this)
{
sum += neuron.getWeightedOutputValue(outputNeuron);
}
return sum;
}
void connectTo(const Layer & nextLayer)
{
for (Neuron &neuron : *this)
{
neuron.createOutputWeights(nextLayer.size());
}
}
};
class Net : public std::vector < Layer >
{
public:
Net(std::initializer_list<unsigned int> layerSizes)
{
if (layerSizes.size() < 3)
{
throw std::exception("A net needs at least 3 layers");
}
for (unsigned int numNeurons : layerSizes)
{
push_back(Layer(numNeurons));
}
for (auto layerIt = begin(); layerIt != end() - 1; ++layerIt)
{
Layer &currentLayer = *layerIt;
const Layer &nextLayer = *(layerIt + 1);
currentLayer.connectTo(nextLayer);
}
}
void feedForward(const std::vector<double> &inputValues)
{
Layer &inputLayer = front();
if (inputLayer.size() != inputValues.size())
{
throw std::exception("The number of input values has to match the input layer size");
}
inputLayer.setOutputValues(inputValues);
for (auto layerIt = begin(); layerIt != end() - 1; ++layerIt)
{
const Layer &currentLayer = *layerIt;
Layer &nextLayer = *(layerIt + 1);
nextLayer.feedForward(currentLayer);
}
}
std::vector<double> getResult()
{
std::vector<double> result;
const Layer &outputLayer = back();
for (const Neuron &neuron : outputLayer)
{
result.push_back(neuron.getOutputValue());
}
return result;
}
};

33
Neuro.cpp Normal file
View file

@ -0,0 +1,33 @@
#include <iostream>
#include <exception>
#include "Net.h"
int main()
{
try
{
std::cout << "Neuro running" << std::endl;
Net myNet({ 2, 3, 1 });
myNet.feedForward({ 1.0, 0.0 });
std::vector<double> result = myNet.getResult();
std::cout << "Result: ";
for (double &value : result)
{
std::cout << value << " ";
}
std::cout << std::endl;
}
catch (std::exception &ex)
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}

22
Neuro.sln Normal file
View file

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Neuro", "Neuro.vcxproj", "{EC045881-3BCD-4054-895B-176FAD3FB8C2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC045881-3BCD-4054-895B-176FAD3FB8C2}.Debug|Win32.ActiveCfg = Debug|Win32
{EC045881-3BCD-4054-895B-176FAD3FB8C2}.Debug|Win32.Build.0 = Debug|Win32
{EC045881-3BCD-4054-895B-176FAD3FB8C2}.Release|Win32.ActiveCfg = Release|Win32
{EC045881-3BCD-4054-895B-176FAD3FB8C2}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

89
Neuro.vcxproj Normal file
View file

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.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>{EC045881-3BCD-4054-895B-176FAD3FB8C2}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Neuro</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</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 Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</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;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Neuro.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Net.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

27
Neuro.vcxproj.filters Normal file
View file

@ -0,0 +1,27 @@
<?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>
<ClCompile Include="Neuro.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Net.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>