public
class
CryptoRandom
{
double
RandomValue {
get
;
set
; }
CryptoRandom()
using
(RNGCryptoServiceProvider p =
new
RNGCryptoServiceProvider())
Random r =
Random(p.GetHashCode());
this
.RandomValue = r.NextDouble();
}
Dendrite
Weight {
Dendrite()
CryptoRandom n =
CryptoRandom();
.Weight = n.RandomValue;
Neuron
List<Dendrite> Dendrites {
Bias {
Delta {
Value {
int
DendriteCount
return
Dendrites.Count;
Neuron()
Random n =
Random(Environment.TickCount);
.Bias = n.NextDouble();
.Dendrites =
List<Dendrite>();
Layer
List<Neuron> Neurons {
NeuronCount
Neurons.Count;
Layer(
numNeurons)
Neurons =
List<Neuron>(numNeurons);
NeuralNetwork
List<Layer> Layers {
LearningRate {
LayerCount
Layers.Count;
NeuralNetwork(
learningRate,
[] layers)
if
(layers.Length < 2)
.LearningRate = learningRate;
.Layers =
List<Layer>();
for
(
l = 0; l < layers.Length; l++)
Layer layer =
Layer(layers[l]);
.Layers.Add(layer);
n = 0; n < layers[l]; n++)
layer.Neurons.Add(
Neuron());
layer.Neurons.ForEach((nn) =>
(l == 0)
nn.Bias = 0;
else
d = 0; d < layers[l - 1]; d++)
nn.Dendrites.Add(
Dendrite());
});
private
Sigmoid(
x)
1 / (1 + Math.Exp(-x));
[] Run(List<
> input)
(input.Count !=
.Layers[0].NeuronCount)
null
l = 0; l < Layers.Count; l++)
Layer layer = Layers[l];
n = 0; n < layer.Neurons.Count; n++)
Neuron neuron = layer.Neurons[n];
neuron.Value = input[n];
neuron.Value = 0;
np = 0; np <
.Layers[l - 1].Neurons.Count; np++)
neuron.Value = neuron.Value +
.Layers[l - 1].Neurons[np].Value * neuron.Dendrites[np].Weight;
neuron.Value = Sigmoid(neuron.Value + neuron.Bias);
Layer last =
.Layers[
.Layers.Count - 1];
numOutput = last.Neurons.Count ;
[] output =
[numOutput];
i = 0; i < last.Neurons.Count; i++)
output[i] = last.Neurons[i].Value;
output;
bool
Train(List<
> input, List<
> output)
((input.Count !=
.Layers[0].Neurons.Count) || (output.Count !=
.Layers.Count - 1].Neurons.Count))
false
Run(input);
i = 0; i <
.Layers.Count - 1].Neurons.Count; i++)
Neuron neuron =
.Layers.Count - 1].Neurons[i];
neuron.Delta = neuron.Value * (1 - neuron.Value) * (output[i] - neuron.Value);
j =
.Layers.Count - 2; j > 2; j--)
k = 0; k <
.Layers[j].Neurons.Count; k++)
Neuron n =
.Layers[j].Neurons[k];
n.Delta = n.Value *
(1 - n.Value) *
.Layers[j + 1].Neurons[i].Dendrites[k].Weight *
.Layers[j + 1].Neurons[i].Delta;
i =
.Layers.Count - 1; i > 1; i--)
j=0; j <
.Layers[i].Neurons.Count; j++)
.Layers[i].Neurons[j];
n.Bias = n.Bias + (
.LearningRate * n.Delta);
k = 0; k < n.Dendrites.Count; k++)
n.Dendrites[k].Weight = n.Dendrites[k].Weight + (
.LearningRate *
.Layers[i - 1].Neurons[k].Value * n.Delta);
true
NeuralNetwork nn =
NeuralNetwork(21.5,
[] { 2, 4, 2 });
List<
> ins =
>();
ins.Add(txtIn01.Text);
ins.Add(txtIn02.Text);
[] ots = nn.Run(ins);
txtOt01.Text = ots[0].ToString();
txtOt02.Text = ots[1].ToString();