Skip to content

Instantly share code, notes, and snippets.

@NMZivkovic
Last active January 31, 2018 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NMZivkovic/0dcc3e0b2d63437d4d20f963d7eadaba to your computer and use it in GitHub Desktop.
Save NMZivkovic/0dcc3e0b2d63437d4d20f963d7eadaba to your computer and use it in GitHub Desktop.
public class Neuron : INeuron
{
private IActivationFunction _activationFunction;
private IInputFunction _inputFunction;
/// <summary>
/// Input connections of the neuron.
/// </summary>
public List<ISynapse> Inputs { get; set; }
/// <summary>
/// Output connections of the neuron.
/// </summary>
public List<ISynapse> Outputs { get; set; }
public Guid Id { get; private set; }
/// <summary>
/// Calculated partial derivate in previous iteration of training process.
/// </summary>
public double PreviousPartialDerivate { get; set; }
public Neuron(IActivationFunction activationFunction, IInputFunction inputFunction)
{
Id = Guid.NewGuid();
Inputs = new List<ISynapse>();
Outputs = new List<ISynapse>();
_activationFunction = activationFunction;
_inputFunction = inputFunction;
}
/// <summary>
/// Connect two neurons.
/// This neuron is the output neuron of the connection.
/// </summary>
/// <param name="inputNeuron">Neuron that will be input neuron of the newly created connection.</param>
public void AddInputNeuron(INeuron inputNeuron)
{
var synapse = new Synapse(inputNeuron, this);
Inputs.Add(synapse);
inputNeuron.Outputs.Add(synapse);
}
/// <summary>
/// Connect two neurons.
/// This neuron is the input neuron of the connection.
/// </summary>
/// <param name="outputNeuron">Neuron that will be output neuron of the newly created connection.</param>
public void AddOutputNeuron(INeuron outputNeuron)
{
var synapse = new Synapse(this, outputNeuron);
Outputs.Add(synapse);
outputNeuron.Inputs.Add(synapse);
}
/// <summary>
/// Calculate output value of the neuron.
/// </summary>
/// <returns>
/// Output of the neuron.
/// </returns>
public double CalculateOutput()
{
return _activationFunction.CalculateOutput(_inputFunction.CalculateInput(this.Inputs));
}
/// <summary>
/// Input Layer neurons just receive input values.
/// For this they need to have connections.
/// This function adds this kind of connection to the neuron.
/// </summary>
/// <param name="inputValue">
/// Initial value that will be "pushed" as an input to connection.
/// </param>
public void AddInputSynapse(double inputValue)
{
var inputSynapse = new InputSynapse(this, inputValue);
Inputs.Add(inputSynapse);
}
/// <summary>
/// Sets new value on the input connections.
/// </summary>
/// <param name="inputValue">
/// New value that will be "pushed" as an input to connection.
/// </param>
public void PushValueOnInput(double inputValue)
{
((InputSynapse)Inputs.First()).Output = inputValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment