Ejemplo n.º 1
0
        /// <summary>
        /// Обновить данные слоя.
        /// </summary>
        /// <param name="updatedFilterCore">Обновлённое ядро фильтра.</param>
        /// <param name="updatedInputLayerData">Обновлённый входной слой.</param>
        public void UpdateData(double[,] updatedFilterCore,
                               Dictionary <string, double> updatedInputLayerData)
        {
            var offset = MatrixConstants.MATRIX_SIZE - MatrixConstants.FILTER_MATRIX_SIZE;
            var step   = MatrixConstants.MATRIX_SIZE - offset;

            var updatedConvolutionalLayerNeurons = new List <NeuronModel>();
            var neuronIndex = 0;

            for (var xIndex = 0; xIndex < step; ++xIndex)
            {
                for (var yIndex = 0; yIndex < step; ++yIndex)
                {
                    var inputs  = new List <double>();
                    var weights = new List <double>();

                    GetDataByFilterCore(updatedFilterCore, xIndex, yIndex, inputs, weights);

                    var neuron = new NeuronModel(inputs, weights)
                    {
                        LastWeights = _convolutionalLayerData[neuronIndex].LastWeights
                    };

                    ++neuronIndex;
                }
            }
        }
Ejemplo n.º 2
0
        private void RenderNeuronInfo(NeuronModel neuron, Vector3 screenPosition)
        {
            screenPosition.X += 10;
            screenPosition.Y += 10;

            using (Blender.AveragingBlender())
            {
                GL.PushMatrix();
                GL.Translate(screenPosition);

                GL.Color4(new Color4(100, 100, 100, 220));
                GL.Begin(PrimitiveType.Quads);

                GL.Vertex3(0, 0, 0);
                GL.Vertex3(100, 0, 0);
                GL.Vertex3(100, 30, 0);
                GL.Vertex3(0, 30, 0);

                GL.End();
                GL.PopMatrix();

                QFont.Begin();
                GL.Translate(10, 0, 0);
                GL.Translate(screenPosition);

                m_font.Print($"{neuron.RegionModel.Index} : {neuron.Index}", QFontAlignment.Left);

                QFont.End();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Инициализация слоёв нейронной сети.
        /// </summary>
        /// <param name="listOfPicturesmatrix">Список матриц изображений.</param>
        /// <param name="layers">Список слоёв.</param>
        /// <param name="filterCore">Ядро фильтра.</param>
        /// <param name="inputLayerNeurons">Нейроны выходного слоя.</param>
        /// <param name="convolutionalLayerNeurons">Нейроны свёрточного слоя.</param>
        /// <param name="hiddenLayerNeurons">Нейроны скрытого слоя.</param>
        /// <param name="outputNeuron">Нейроны выходного слоя.</param>
        private static void LayersInitialize(List <double[, ]> listOfPicturesmatrix, List <Layer> layers,
                                             double[,] filterCore, out Dictionary <string, double> inputLayerNeurons,
                                             out List <NeuronModel> convolutionalLayerNeurons, out List <NeuronModel> hiddenLayerNeurons,
                                             out NeuronModel outputNeuron)
        {
            var firstDataSet = listOfPicturesmatrix.First();
            var inputLayer   = new InputLayer(firstDataSet);

            inputLayer.Initialize();
            layers.Add(inputLayer);

            inputLayerNeurons = inputLayer.GetLayerNeurons();
            var convolutionalLayer = new ConvolutionalLayer(inputLayerNeurons);

            convolutionalLayer.Initialize(filterCore);
            layers.Add(convolutionalLayer);

            convolutionalLayerNeurons = convolutionalLayer.GetLayerNeurons();
            var hiddenLayer = new HiddenLayer(convolutionalLayerNeurons);

            hiddenLayer.Initialize();
            layers.Add(hiddenLayer);

            hiddenLayerNeurons = hiddenLayer.GetLayerNeurons();
            var outputLayer = new OutputLayer(hiddenLayerNeurons);

            outputLayer.Initilize();
            layers.Add(outputLayer);

            outputNeuron = outputLayer.GetOutputNeuron();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Инициализацяи свёрточного слоя.
        /// </summary>
        /// <param name="filterCore">Ядро фильтра.</param>
        public void Initialize(double[,] filterCore)
        {
            _convolutionalLayerData = new List <NeuronModel>();

            var offset = MatrixConstants.MATRIX_SIZE - MatrixConstants.FILTER_MATRIX_SIZE;
            var step   = MatrixConstants.MATRIX_SIZE - offset;

            var emptyLastWeights = new List <double>();

            for (var index = 0; index < MatrixConstants.MATRIX_SIZE *
                 MatrixConstants.MATRIX_SIZE; ++index)
            {
                emptyLastWeights.Add(0d);
            }

            for (var xIndex = 0; xIndex < step; ++xIndex)
            {
                for (var yIndex = 0; yIndex < step; ++yIndex)
                {
                    var inputs  = new List <double>();
                    var weights = new List <double>();

                    GetDataByFilterCore(filterCore, xIndex, yIndex, inputs, weights);
                    var neuron = new NeuronModel(inputs, weights)
                    {
                        LastWeights = emptyLastWeights
                    };

                    _convolutionalLayerData.Add(neuron);
                }
            }
        }
        public void RepositionsSynapse()
        {
            RegionModel  addedRegion;
            SynapseModel addedSynapse;

            SetupRegionWithSynapse(out addedRegion, out addedSynapse);

            NeuronModel neuron1 = m_model.Regions.First().Neurons.First();
            NeuronModel neuron2 = m_model.Regions.First().Neurons.Last();

            SynapseModel synapse = m_model.Regions.First().Synapses.First();

            neuron1.Position *= 2;
            ResponseMessage diff = ModelResponseBuilder.Build(repositionedNeurons: new List <NeuronModel> {
                neuron1
            });

            ApplyModelDiff(diff);

            Assert.Equal(neuron1.Position, synapse.Position);

            neuron2.Position *= 3;
            diff              = ModelResponseBuilder.Build(repositionedNeurons: new List <NeuronModel> {
                neuron2
            });
            ApplyModelDiff(diff);

            Assert.Equal(neuron2.Position, neuron1.Position + synapse.TargetPosition);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Обновить входные значения.
        /// </summary>
        /// <param name="inputs">Входные значения.</param>
        public void UpdateData(List <double> inputs)
        {
            _outputNeuron = new NeuronModel(inputs, _outputNeuron.Weights)
            {
                LastWeights = _outputNeuron.LastWeights
            };

            _output = _outputNeuron.Output;
        }
Ejemplo n.º 7
0
        public void CorrectlyRepositionsNeurons()
        {
            var size    = new Vector3(10, 20, 30);
            var region  = new RegionModel(1, "foo", "bar", Vector3.Zero, size);
            var neuron1 = new NeuronModel(1, "foo", region, new Vector3(0, 0, 0));
            var neuron2 = new NeuronModel(1, "foo", region, new Vector3(1, 1, 1));

            Assert.Equal(-size / 2 + new Vector3(RegionModel.RegionMargin), neuron1.Position);
            Assert.Equal(size / 2 - new Vector3(RegionModel.RegionMargin), neuron2.Position);
        }
Ejemplo n.º 8
0
 private void ToggleNeuron(NeuronModel neuron)
 {
     if (neuron.Picked)
     {
         DeselectNeuron(neuron.RegionModel.Index, neuron.Index);
     }
     else
     {
         SelectNeuron(neuron.RegionModel.Index, neuron.Index);
     }
 }
        private void SetupRegionWithNeuron(out RegionModel addedRegion, out NeuronModel addedNeuron)
        {
            addedRegion = SetupRegion(m_applier, m_model, 1);
            addedNeuron = new NeuronModel(1, "neuronType", addedRegion, Vector3.One);

            ResponseMessage diff = ModelResponseBuilder.Build(addedNeurons: new List <NeuronModel> {
                addedNeuron
            });

            ApplyModelDiff(diff);
        }
Ejemplo n.º 10
0
        private bool TryFindNeuron(uint regionIndex, uint neuronIndex, out NeuronModel neuron)
        {
            RegionModel region;

            if (!m_simulationModel.Regions.TryGetModel(regionIndex, out region))
            {
                neuron = null;
                return(false);
            }

            return(region.Neurons.TryGetModel(neuronIndex, out neuron));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Приготовить к распознаванию.
        /// </summary>
        /// <param name="neuronIndexToWeightsValueDictionary">Словарь значений весов,
        /// где ключ - индекс нейрона, значение - веса нейрона.</param>
        public void RecognizeMode(Dictionary <int, List <double> > neuronIndexToWeightsValueDictionary)
        {
            var inputs = new List <double>();

            if (!neuronIndexToWeightsValueDictionary.TryGetValue(0, out var weights))
            {
                ErrorHelper.GetDataError();
            }

            _hiddenLayerData.ForEach(neuron => inputs.Add(neuron.Output));

            _outputNeuron = new NeuronModel(inputs, weights);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Обновление весов нейрона.
        /// </summary>
        /// <param name="neuronModel">Модель нейрона.</param>
        /// <param name="weights">Новые веса.</param>
        public static void UpdateWeights(this NeuronModel neuronModel, List <double> weights)
        {
            if (neuronModel.Weights.Count != weights.Count)
            {
                MessageBox.Show("Не соответствие по количеству весов!", "Ошибка",
                                MessageBoxButton.OK, MessageBoxImage.Error);

                return;
            }

            neuronModel.LastWeights = neuronModel.Weights;
            neuronModel.Weights     = weights;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Обновление весов нейрона.
        /// </summary>
        /// <param name="neuronModel">Модель нейрона.</param>
        /// <param name="weights">Новые веса.</param>
        public static void UpdateWeights(this NeuronModel neuronModel, List <double> weights)
        {
            if (neuronModel.Weights.Count != weights.Count)
            {
                var exception = new Exception("Несоответствие по количеству весов.");

                Console.WriteLine(BL.Constants.ConsoleMessageConstants.ERROR_MESSAGE +
                                  exception.ToString());

                Console.ReadKey();
                return;
            }

            neuronModel.LastWeights = neuronModel.Weights;
            neuronModel.Weights     = weights;
        }
Ejemplo n.º 14
0
        public void PickObject(int x, int y)
        {
            if (m_simulationModel == null)
            {
                return;
            }

            m_pickRay = PickRay.Pick(x, y, m_camera, m_control.Size, ProjectionMatrix);

            NeuronModel neuron = FindFirstNeuron(m_pickRay, m_simulationModel.Regions);

            if (neuron != null)
            {
                ToggleNeuron(neuron);
            }
        }
Ejemplo n.º 15
0
        public void OpenObserver(NeuronModel neuron, Scene scene)
        {
            ObserverDefinition definition = CreateObserverDefinition(neuron);
            // TODO(HonzaS): Factory + injection.
            var observer = new CanvasObserver(definition, Conductor.ModelProvider);

            observer.Log = Log;
            var form = new ObserverForm(this, observer);

            form.Text = $"Neuron {neuron.Index}, region {neuron.RegionModel.Index} - '{definition.Type}'";

            var handle = new ObserverHandle(observer, form, scene);

            Observers.Add(handle);
            form.Show();

            RefreshObserverRequests();
        }
        private void SetupRegionWithSynapse(out RegionModel addedRegion, out SynapseModel addedSynapse)
        {
            addedRegion = SetupRegion(m_applier, m_model, 1);

            var addedNeuron1 = new NeuronModel(1, "neuronType", addedRegion, Vector3.One);
            var addedNeuron2 = new NeuronModel(2, "neuronType", addedRegion, Vector3.UnitY);

            addedSynapse = new SynapseModel(addedRegion, addedNeuron1, addedRegion, addedNeuron2);

            ResponseMessage diff = ModelResponseBuilder.Build(
                addedNeurons: new List <NeuronModel> {
                addedNeuron1, addedNeuron2
            },
                addedSynapses: new List <SynapseModel> {
                addedSynapse
            });

            ApplyModelDiff(diff);
        }
Ejemplo n.º 17
0
        private NeuronModel FindFirstNeuron(PickRay pickRay, IEnumerable <RegionModel> regions)
        {
            float       closestDistance = float.MaxValue;
            NeuronModel closestNeuron   = null;

            foreach (RegionModel region in regions)
            {
                foreach (NeuronModel neuron in region.Neurons)
                {
                    float distance = neuron.DistanceToRayOrigin(pickRay);
                    if (distance < closestDistance)
                    {
                        closestNeuron   = neuron;
                        closestDistance = distance;
                    }
                }
            }

            return(closestNeuron);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Инициализация слоя.
        /// </summary>
        public void Initilize()
        {
            var inputs  = new List <double>();
            var weights = new List <double>();

            var emptyLastWeights = new List <double>();

            foreach (var neuron in _hiddenLayerData)
            {
                inputs.Add(neuron.Output);
                weights.Add(GetInitializedWeight());

                emptyLastWeights.Add(0d);
            }

            _outputNeuron = new NeuronModel(inputs, weights)
            {
                LastWeights = emptyLastWeights
            };
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Инициализировать выходной слой.
        /// </summary>
        public override void Initialize()
        {
            var weights      = new List <double>();
            var emptyWeights = new List <double>();

            var random = new Random();

            for (var wIndex = 0; wIndex < _inputData.Count; ++wIndex)
            {
                weights.Add(random.NextDouble());
                emptyWeights.Add(0d);
                System.Threading.Thread.Sleep(10);
            }

            _outputNeuron = new NeuronModel(_inputData, weights)
            {
                LastWeights = emptyWeights
            };

            _output = _outputNeuron.Output;
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Приготовить к распознаванию.
        /// </summary>
        /// <param name="neuronIndexToWeightsValueDictionary">Словарь значений весов,
        /// где ключ - индекс нейрона, значение - веса нейрона.</param>
        public void RecognizeMode(Dictionary <int, List <double> > neuronIndexToWeightsValueDictionary)
        {
            _hiddenLayerData = new List <NeuronModel>();
            var countOfNeuronsInHiddenLayer = (int)_convolutionalLayerData.Count / 2;

            var inputs = new List <double>();

            _convolutionalLayerData.ForEach(neuronOfConvolutionalLayer
                                            => inputs.Add(neuronOfConvolutionalLayer.Output));

            for (var index = 0; index < countOfNeuronsInHiddenLayer; ++index)
            {
                if (!neuronIndexToWeightsValueDictionary.TryGetValue(index, out var weights))
                {
                    ErrorHelper.GetDataError();
                }

                var neuron = new NeuronModel(inputs, weights);
                _hiddenLayerData.Add(neuron);
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Приготовить к распознаванию.
        /// </summary>
        /// <param name="filterCore">Ядро фильтра.</param>
        public void RecognizeMode(double[,] filterCore)
        {
            _convolutionalLayerData = new List <NeuronModel>();

            var offset = MatrixConstants.MATRIX_SIZE - MatrixConstants.FILTER_MATRIX_SIZE;
            var step   = MatrixConstants.MATRIX_SIZE - offset;

            for (var xIndex = 0; xIndex < step; ++xIndex)
            {
                for (var yIndex = 0; yIndex < step; ++yIndex)
                {
                    var inputs  = new List <double>();
                    var weights = new List <double>();

                    GetDataByFilterCore(filterCore, xIndex, yIndex, inputs, weights);
                    var neuron = new NeuronModel(inputs, weights);

                    _convolutionalLayerData.Add(neuron);
                }
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Создать нейрон по имеющимся данным.
        /// </summary>
        /// <returns>Возвращает нейрон.</returns>
        private NeuronModel CreateNeuronByData()
        {
            var inputs  = new List <double>();
            var weights = new List <double>();

            var emptyLastWeights = new List <double>();

            foreach (var neuron in _convolutionalLayerData)
            {
                inputs.Add(neuron.Output);
                weights.Add(GetInitializedWeight());

                emptyLastWeights.Add(0d);
            }

            var hiddenLayerNeuron = new NeuronModel(inputs, weights)
            {
                LastWeights = emptyLastWeights
            };

            return(hiddenLayerNeuron);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Инициализация слоя.
        /// </summary>
        public override void Initialize()
        {
            _neurons = new List <NeuronModel>();
            _output  = new List <double>();

            if (_countOfNeurons.Equals(0))
            {
                _output = _inputData;
                return;
            }

            var neurons = new List <NeuronModel>();

            for (var index = 0; index < _countOfNeurons; ++index)
            {
                var weights      = new List <double>();
                var emptyWeights = new List <double>();

                var random = new Random();

                for (var wIndex = 0; wIndex < _inputData.Count; ++wIndex)
                {
                    weights.Add(random.NextDouble());
                    emptyWeights.Add(0d);
                    System.Threading.Thread.Sleep(10);
                }

                var neuron = new NeuronModel(_inputData, weights)
                {
                    LastWeights = emptyWeights
                };

                neurons.Add(neuron);
            }

            neurons.ForEach(neuron => _output.Add(neuron.Output));
            _neurons.AddRange(neurons);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Инициализировать для прогноза.
        /// </summary>
        /// <param name="weightsDictionary">Словарь весов.</param>
        public void SwitchToForeCast(Dictionary <int, List <double> > weightsDictionary)
        {
            _neurons = new List <NeuronModel>();
            _output  = new List <double>();

            if (_countOfNeurons.Equals(0))
            {
                _output = _inputData;
                return;
            }

            var neurons = new List <NeuronModel>();

            for (var index = 0; index < _countOfNeurons; ++index)
            {
                var weights = weightsDictionary[index];
                var neuron  = new NeuronModel(_inputData, weights);

                neurons.Add(neuron);
            }

            _neurons.AddRange(neurons);
            _neurons.ForEach(neuron => _output.Add(neuron.Output));
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Обновление данных.
        /// </summary>
        /// <param name="dataSet">Датасет.</param>
        public void UpdateData(List <double> dataSet)
        {
            if (_countOfNeurons.Equals(0))
            {
                _output = dataSet;
                return;
            }

            var updatedNeurons = new List <NeuronModel>();

            foreach (var neuron in _neurons)
            {
                var newNeuron = new NeuronModel(dataSet, neuron.Weights)
                {
                    LastWeights = neuron.LastWeights
                };

                updatedNeurons.Add(neuron);
            }

            _neurons = updatedNeurons;
            _output.Clear();
            _neurons.ForEach(neuron => _output.Add(neuron.Output));
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Расчёт дельты для нейронов скрытого слоя.
 /// </summary>
 /// <param name="neuronOutput">Вывод нейрона.</param>
 /// <param name="indexInLayer">Индекс в скрытом слое.</param>
 /// <param name="outputNeuron">Нейрон выходного слоя.</param>
 /// <returns></returns>
 private double GetHiddenLayerNeuronDelta(
     double neuronOutput, int indexInLayer, NeuronModel outputNeuron) =>
 DerivativeActivationFunction(neuronOutput) *
 outputNeuron.Inputs[indexInLayer] * outputNeuron.Delta;
Ejemplo n.º 27
0
 private bool TryFindNeuron(CompositeNeuronId neuronId, out NeuronModel neuron)
 {
     return(TryFindNeuron(neuronId.RegionIndex, neuronId.NeuronIndex, out neuron));
 }
Ejemplo n.º 28
0
        private ObserverDefinition CreateObserverDefinition(NeuronModel neuron)
        {
            var definition = new ObserverDefinition(neuron.Index, neuron.RegionModel.Index, m_observerType);

            return(definition);
        }
Ejemplo n.º 29
0
        public void CloseObserver(NeuronModel neuron)
        {
            ObserverDefinition definition = CreateObserverDefinition(neuron);

            CloseObserver(definition);
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Инициализировать для прогноза.
 /// </summary>
 /// <param name="weightsDictionary">Веса.</param>
 public void SwitchToForeCast(List <double> weights)
 {
     _outputNeuron = new NeuronModel(_inputData, weights);
     _output       = _outputNeuron.Output;
 }