This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
Neuro/Neuron.cpp

63 lines
1.1 KiB
C++
Raw Normal View History

#pragma once
#include "Neuron.h"
2015-03-24 12:45:38 +00:00
Neuron::Neuron(double value)
: outputValue(value)
, gradient(0)
2015-03-24 12:45:38 +00:00
{
}
void Neuron::setOutputValue(double value)
{
outputValue = value;
}
double Neuron::transferFunction(double inputValue)
{
return std::tanh(inputValue);
}
double Neuron::transferFunctionDerivative(double inputValue)
{
return 1.0 - (inputValue * inputValue);
}
void Neuron::feedForward(double inputValue)
{
outputValue = transferFunction(inputValue);
}
2015-03-24 12:45:38 +00:00
double Neuron::getWeightedOutputValue(unsigned int outputNeuron) const
{
2015-03-24 12:45:38 +00:00
if (outputNeuron < outputWeights.size())
{
return outputValue * outputWeights[outputNeuron];
}
return 0.0;
}
2015-10-15 20:37:13 +00:00
void Neuron::createRandomOutputWeights(size_t numberOfWeights)
{
outputWeights.clear();
for (unsigned int i = 0; i < numberOfWeights; ++i)
{
outputWeights.push_back(std::rand() / (double)RAND_MAX);
}
}
double Neuron::getOutputValue() const
{
return outputValue;
}
void Neuron::calcOutputGradients(double targetValue)
{
double delta = targetValue - outputValue;
gradient = delta * transferFunctionDerivative(outputValue);
}