Пример #1
0
 public CommonSlice(int numberOfNeurons)
 {
     for (var i = 0; i < numberOfNeurons; i++)
     {
         Neurons.Add(new CommonBiasNeuron());
     }
 }
Пример #2
0
        public override void InitNetwork()
        {
            // Creating neurons
            for (int ii = 0; ii < NeuronCount; ii++)
            {
                Neurons.Add(new Neuron(ii));
            }
            // Adding link btween neurons
            int jj = 0;

            // input to hidden layer
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[0], Neurons[2 + kk], jj++);
            }
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[1], Neurons[2 + kk], jj++);
            }
            // reccurant links
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[2 + kk], Neurons[2 + kk], jj++);
            }
            // hidden layer to input
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[2 + kk], Neurons[NeuronCount - 2], jj++);
            }
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[2 + kk], Neurons[NeuronCount - 1], jj++);
            }
        }
        /// <summary>
        /// Сгенерировать нейроны сети.
        /// </summary>
        /// <param name="attributes">Количество параметров входящего вектора.</param>
        /// <param name="neuronsCount">Количество нейронов.</param>
        public void GenerateNeurons(List <NetworkAttribute> attributes, int neuronsCount)
        {
            Neurons.Clear();
            Weights.Clear();
            InputAttributes.Clear();

            InputAttributes.AddRange(attributes.Select(a => new InputAttributeBase
            {
                InputAttributeNumber = a.OrderNumber,
                Name = a.Name
            })
                                     .ToList());

            for (int i = 0; i < neuronsCount; i++)
            {
                Neurons.Add(new NeuronBase
                {
                    NeuronNumber = i
                });
            }

            foreach (var inputAttribute in InputAttributes)
            {
                foreach (var neuron in Neurons)
                {
                    var randomWeight = NormalizationType.GetNeuronWeight(attributes.Count);
                    Weights.Add(new WeightBase
                    {
                        InputAttributeNumber = inputAttribute.InputAttributeNumber,
                        NeuronNumber         = neuron.NeuronNumber,
                        Value = randomWeight
                    });
                }
            }
        }
Пример #4
0
        internal void Add(NeuronTypes type)
        {
            int arrayIndex = 0;

            for (int i = 0; i < NeuronsInfo.Count; i++)
            {
                arrayIndex += Convert.ToInt32(type == NeuronsInfo[i].neuronType);
            }
            NeuronsInfo.Add(new NeuronInfo(arrayIndex, type));
            switch (type)
            {
            case NeuronTypes.feedForward:
                Neurons.Add(new Neuron(previousLayerLenght));
                break;

            case NeuronTypes.lSTM:
                LSTMCells.Add(new LSTMCell(previousLayerLenght));
                break;

            case NeuronTypes.recurrent:
                RecurrentNeurons.Add(new RecurrentNeuron(previousLayerLenght));
                break;

            default:
                throw new NotImplementedException();
            }
        }
 public void FillLayer()
 {
     for (var i = 0; i < Size; i++)
     {
         Neurons.Add(new Neuron(Index, i));
     }
 }
Пример #6
0
 public InputLayer(int numberOfNeurons, IActivationFunction activationFunction, IInputFunction inputFunction)
 {
     for (int i = 0; i < numberOfNeurons; i++)
     {
         Neurons.Add(new InputNeuron(activationFunction, inputFunction));
     }
 }
Пример #7
0
        /// <summary>
        /// mutates a random link in the neural network
        /// </summary>
        public void MutateNeuron()
        {
            while (true)
            {
                int neuronId = rand.Next(NumInputs, NumInputs + NumOutputs);
                if (Neurons.TryGetValue(neuronId, out Neuron farNeuron) && farNeuron.Connections.Count > 0)
                {
                    Connection conn;
                    int        connectionId;
                    do
                    {
                        connectionId = rand.Next(farNeuron.Connections.Count());
                        conn         = farNeuron.Connections.Values.ElementAt(connectionId);
                    } while (conn.From.ID == 0);

                    Neuron newNeuron = new Neuron(Neurons.Count);

                    Tuple <int, int> nearToMidTuple = new Tuple <int, int>(conn.From.ID, newNeuron.ID);
                    Connection       nearToMid;
                    lock (InnovationNumbers)
                    {
                        if (InnovationNumbers.TryGetValue(nearToMidTuple, out int innoNumber))
                        {
                            nearToMid = new Connection(innoNumber, conn.From, 1);
                        }
                        else
                        {
                            nearToMid = new Connection(InnovationNumbers.Count(), conn.From, 1);
                            InnovationNumbers.Add(nearToMidTuple, InnovationNumbers.Count());
                        }
                    }
                    Connection       midToFar;
                    Tuple <int, int> midToFarTuple = new Tuple <int, int>(newNeuron.ID, farNeuron.ID);
                    lock (InnovationNumbers)
                    {
                        if (InnovationNumbers.TryGetValue(midToFarTuple, out int innoNumber2))
                        {
                            midToFar = new Connection(innoNumber2, newNeuron, conn.Weight);
                        }
                        else
                        {
                            midToFar = new Connection(InnovationNumbers.Count(), newNeuron, conn.Weight);
                            InnovationNumbers.Add(midToFarTuple, InnovationNumbers.Count());
                        }
                    }

                    Neurons.Add(newNeuron.ID, newNeuron);
                    newNeuron.AddConnection(nearToMid.ID, nearToMid);
                    Connection oldConnection = farNeuron.Connections.Values.ElementAt(connectionId);
                    conn.EnableDisable();
                    Connections.Add(nearToMidTuple);
                    Connections.Add(midToFarTuple);

                    farNeuron.AddConnection(midToFar.ID, midToFar);
                    NumNeurons++;
                    return;
                }
            }
        }
Пример #8
0
        public NeuralLayer(int neuronCount, double initialWeight, string name)
        {
            Weight = initialWeight;
            Name   = name;

            Enumerable.Range(0, neuronCount).ToList()
            .ForEach(elem => Neurons.Add(new Neuron()));
        }
Пример #9
0
 public void Populate(int size, AbstractInputFunction inputFunction = null, AbstractActivationFunction activationFunction = null)
 {
     for (int toFill = size - Neurons.Count(); toFill > 0; toFill--)
     {
         Neurons.Add(new Neuron(inputFunction, activationFunction));
     }
     Size = Neurons.Count();
 }
Пример #10
0
 public NeuronLayer(int layerSize, Random rnd)
 {
     _rnd = rnd;
     for (int i = 0; i < layerSize; i++)
     {
         Neurons.Add(new Neuron(_rnd));
     }
 }
Пример #11
0
        public void AddNeuron(Neuron neuron)
        {
            neuron.Index  = Neurons.Count;
            neuron.Parent = this;
            Neurons.Add(neuron);

            Logger.WriteAdd(neuron);
        }
Пример #12
0
        public NeuralLayer(int neuronCount, ActivationType activationType = ActivationType.Tanh)
        {
            ActivationType = activationType;

            for (int i = 0; i < neuronCount; i++)
            {
                Neurons.Add(new Neuron(activationType));
            }
        }
Пример #13
0
 public OutputLayer(int count) : base()
 {
     for (int i = 0; i < count; i++)
     {
         Neurons.Add(new OutputNeuron());
     }
     SubscribeNeuronsCountEnds();
     statusEnds = $"Output layer ends";
 }
 public HiddenLayer(int count) : base()
 {
     for (int i = 0; i < count; i++)
     {
         Neurons.Add(new HiddenNeuron());
     }
     //Neurons.Add(new BiasNeuron());
     SubscribeNeuronsCountEnds();
     statusEnds = $"Hidden layer ends";
 }
Пример #15
0
        public override OutputNeuron CreateNeuron(string name)
        {
            OutputNeuron outputNeuron;

            outputNeuron = new OutputNeuron(name);

            Neurons.Add(outputNeuron);

            return(outputNeuron);
        }
Пример #16
0
 public void addNeurons(int max)
 {
     if (max > Count)
     {
         for (int i = Count + 1; i <= max; i++)
         {
             Neurons.Add(new StdNeuron(this, max));
         }
     }
 }
Пример #17
0
 //Конструктор слоя
 public Layer(int NumberOfNeurons, int NumberOfNeuronInputs)
 {
     this.NumberOfNeurons      = NumberOfNeurons;
     this.NumberOfNeuronInputs = NumberOfNeuronInputs;
     for (int i = 0; i < NumberOfNeurons; i++)
     {
         Neuron ObjectNeuron = new Neuron(NumberOfNeuronInputs);
         Neurons.Add(ObjectNeuron);
     }
 }
        public Neuron CreateNeuron()
        {
            Neuron n = new Neuron();

            n.Id           = Guid.NewGuid().ToString();
            n.InputValue   = 0;
            n.IsBiasNeuron = false;
            Neurons.Add(n);
            return(n);
        }
Пример #19
0
        public override HiddenNeuron CreateNeuron(string name)
        {
            HiddenNeuron hiddenNeuron;

            hiddenNeuron = new HiddenNeuron(name);

            Neurons.Add(hiddenNeuron);

            return(hiddenNeuron);
        }
Пример #20
0
        public override InputNeuron CreateNeuron(string name)
        {
            InputNeuron inputNeuron;

            inputNeuron = new InputNeuron(name);

            Neurons.Add(inputNeuron);

            return(inputNeuron);
        }
 public InputLayer(int count) : base()
 {
     for (int i = 0; i < count; i++)
     {
         Neurons.Add(new InputNeuron());
     }
     //Neurons.Add(new BiasNeuron());
     SubscribeNeuronsCountEnds();
     statusEnds = $"Input layer ends";
 }
Пример #22
0
        public void AddBias()
        {
            Neuron lastNeuron = Neurons.Last();
            Neuron newNeuron  = new Neuron(
                lastNeuron.Dendrites.Count(),
                lastNeuron.ID + 1);

            newNeuron.Value = 1;
            newNeuron.Bias  = true;
            Neurons.Add(newNeuron);
        }
Пример #23
0
        /// <summary>
        /// Forces the genome to get a neuron with the same innovation nb
        /// as the targetNeuron. If there isn't one, create.
        /// </summary>
        private Neuron ForceGetNeuron(Neuron targetNeuron)
        {
            var result = Neurons.FirstOrDefault(x => x.InnovationNb == targetNeuron.InnovationNb);

            if (result == null)
            {
                result = new Neuron(targetNeuron);
                Neurons.Add(result);
            }
            return(result);
        }
Пример #24
0
        public PeceptronNeuron AddNeuron(params Input[] inputs)
        {
            var neuron = new PeceptronNeuron();

            foreach (Input inputItem in inputs)
            {
                neuron.AddInput(inputItem);
            }

            Neurons.Add(neuron);
            return(neuron);
        }
Пример #25
0
        public IList <Neuron> AddNeurons(Neuron sampleNeuron, int count = 1)
        {
            var innovNb = GetFreeNeuronInnovNb();
            var neurons = Enumerable.Range(0, count)
                          .Select(i => sampleNeuron.Clone(innovNb++))
                          .ToArray();

            foreach (var neuron in neurons)
            {
                Neurons.Add(neuron.InnovationNb, neuron);
            }

            return(neurons);
        }
Пример #26
0
 //public List<double> Data
 //{
 //    set
 //    {
 //        for(int i = 0; i < Neurons.Count; i++)
 //        {
 //            Neurons[i].Inputs = value;
 //        }
 //    }
 //}
 protected void NeuronInitialize(byte[][] pixels) //для входного слоя
 {
     for (int i = 0; i < pixels.Length; i++)
     {
         for (int j = 0; j < pixels[i].Length; j++)
         {
             List <double> inputs = new List <double>()
             {
                 pixels[i][j]
             };
             Neurons.Add(new Neuron(inputs, null, _NeuronType, i, j));
         }
     }
 }
Пример #27
0
 public Layer Add(int v, Type type)
 {
     for (int i = 0; i < v; i++)
     {
         var neuronInstance = System.Reflection.Assembly.GetAssembly(type).CreateInstance(type.ToString());
         if (neuronInstance == null)
         {
             throw new Exception("Unable to create neuron of type " + type);
         }
         var neuron = neuronInstance as Neuron;
         neuron.Layer = this;
         Neurons.Add((Neuron)neuron);
     }
     return(this);
 }
Пример #28
0
        public Layer(int neurons, IActivation activation = null, bool addBias = false)
        {
            Activation = activation;

            for (int i = 0; i < neurons; i++)
            {
                Neurons.Add(new Neuron(this));
            }
            if (addBias)
            {
                Neurons.Add(new Neuron(this)
                {
                    IsBias = true
                });
            }
        }
Пример #29
0
        private void CopyFrom(Neat parent)
        {
            lock (ThinkLock) {
                for (int i = 0; i < parent.Neurons.Count; i++)
                {
                    Neurons.Add(parent.Neurons[i].Clone(this));
                }
            }
            for (int i = 0; i < parent.Genes.Count; i++)
            {
                Gene gene = parent.Genes[i];
                Genes.Add(new Gene(gene.from, gene.axon.destination, gene.axon.weight, gene.enabled, gene.innovation));
            }

            innovation = parent.innovation;
        }
Пример #30
0
        public LayerDense(IList <double[]> inputs, int neuronsCount)
        {
            for (int i = 0; i < neuronsCount; i++)
            {
                // Create random weights for each neuron
                var tempWeights = Vector.Random(inputs.Count());
                var newNeuron   = new Neuron
                {
                    Weights = tempWeights,
                    Bias    = 0,
                };
                Neurons.Add(newNeuron);
            }

            Forward(inputs);
        }