📄 node.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace NeuralNetCS
{
class Node
{
private Node[] _inputs;
private double[] _inputWeights;
private double _value;
private double _lastInput;
/// <summary>
/// Creates a new node with no inputs and the specified value.
/// </summary>
/// <param name="value">The initial value of the node.</param>
/// <remarks>This constructor creates a node with no inputs, which can be used for nodes on the
/// input layer, or for bias nodes.</remarks>
public Node(double value)
{
_value = value;
}
/// <summary>
/// Creates a node with the specified inputs.
/// </summary>
/// <param name="inputs">The inputs for this node.</param>
/// <remarks>The weights for the inputs will be initialized to a random value. The value of the
/// node is zero until Eval is called.</remarks>
public Node(Node[] inputs)
{
if( inputs == null )
throw new ArgumentNullException("inputs");
if( inputs.Length == 0 )
throw new ArgumentException("A node cannot have an empty input list.", "inputs");
_inputs = inputs;
_inputWeights = new double[inputs.Length];
Random rnd = new Random();
for( int x = 0; x < _inputWeights.Length; ++x )
_inputWeights[x] = rnd.NextDouble();
}
/// <summary>
/// Evaluates the value of the node.
/// </summary>
/// <remarks>
/// Computes the weighted sum of the inputs and uses the sigmoid activation function to
/// determine the activation value of this node. After calling Eval, the activation value
/// can be retrieved using the <see cref="Value" /> property.
/// </remarks>
public void Eval()
{
if( _inputs != null )
{
double weightedInput = 0.0f;
int length = _inputs.Length;
for( int x = 0; x < length; ++x )
weightedInput += _inputs[x].Value * _inputWeights[x];
_lastInput = weightedInput;
_value = Net.Sigmoid(weightedInput);
}
}
/// <summary>
/// Gets the weight for the specified input.
/// </summary>
/// <param name="input">The input whose weight to get.</param>
/// <returns>The weight for the specified input.</returns>
public double GetWeigth(int input)
{
return _inputWeights[input];
}
/// <summary>
/// Sets the weight for the specified input.
/// </summary>
/// <param name="input">The input whose weight to set.</param>
/// <param name="weight">The new weight value.</param>
public void SetWeight(int input, double weight)
{
_inputWeights[input] = weight;
}
/// <summary>
/// Gets or sets the current value of the node.
/// </summary>
public double Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
/// <summary>
/// Gets or sets the weighted sum of the inputs that was used in the last call
/// to <see cref="Eval" />.
/// </summary>
public double LastInput
{
get
{
return _lastInput;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -