/// <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(); }
/// <summary> /// Получение и обновление дельты свёрточного слоя. /// </summary> /// <param name="hiddenLayer">Скрытый слой.</param> /// <param name="convolutionalLayer">Свёрточный слой.</param> private void ConvolutionalToHiddenDeltasWork(HiddenLayer hiddenLayer, ConvolutionalLayer convolutionalLayer) { var convolutionalLayerNeurons = convolutionalLayer.GetLayerNeurons(); var convolutionalLayerOutputs = new List <double>(); convolutionalLayerNeurons.ForEach(neuron => convolutionalLayerOutputs.Add(neuron.Output)); var convolutionalLayerDeltas = GetConvolutionalLayerDeltas(convolutionalLayerOutputs, hiddenLayer.GetLayerNeurons()); convolutionalLayer.UpdateDeltas(convolutionalLayerDeltas); }
/// <summary> /// Выполнить вычисления для ядра фильтра. /// </summary> /// <param name="convolutionalLayer">Свёрточный слой.</param> /// <param name="inputLayer">Входной слой.</param> private void FilterCoreWork(ConvolutionalLayer convolutionalLayer, InputLayer inputLayer) { var inputLayerNeurons = inputLayer.GetLayerNeurons(); var convolutionalLayerDeltas = new List <double>(); convolutionalLayer.GetLayerNeurons().ForEach(neuron => convolutionalLayerDeltas.Add(neuron.Delta)); var filterCoreMatrixDeltas = GetFilterCoreDeltasMatrix( inputLayerNeurons, convolutionalLayerDeltas); var filterCoreMatrixGradients = GetFilterCoreGradientMatrix( filterCoreMatrixDeltas, inputLayerNeurons); UpdateCore(filterCoreMatrixGradients); }
/// <summary> /// Получение градиента между нейронами свёрточного слоя и скрытого, и обновление весов. /// </summary> /// <param name="hiddenLayer">Скрытый слой.</param> /// <param name="convolutionalLayer">Свёрточный слой.</param> private void ConvolutionalToHiddenWeightsWork(HiddenLayer hiddenLayer, ConvolutionalLayer convolutionalLayer) { var convolutionalLayerNeurons = convolutionalLayer.GetLayerNeurons(); var convolutionalLayerOutputs = new List <double>(); var hiddenLayerNeurons = hiddenLayer.GetLayerNeurons(); var hiddenLayerDeltas = new List <double>(); convolutionalLayerNeurons.ForEach(neuron => convolutionalLayerOutputs.Add(neuron.Output)); hiddenLayerNeurons.ForEach(neuron => hiddenLayerDeltas.Add(neuron.Delta)); var convolutionalNeuronToGradientsDictionary = GetConvolutionalToHiddenGradients( convolutionalLayerOutputs, hiddenLayerDeltas); UpdateConvolutionalToHiddenWeights(convolutionalNeuronToGradientsDictionary, hiddenLayer); }
/// <summary> /// Инициализировать нейронную сеть. /// </summary> public void InitializeNetwork() { var inputLayer = new InputLayer(_imageData); inputLayer.Initialize(); var convolutionalLayer = new ConvolutionalLayer(inputLayer.GetLayerNeurons()); convolutionalLayer.RecognizeMode(FilterCoreModel.GetCore); var hiddenLayer = new HiddenLayer(convolutionalLayer.GetLayerNeurons()); hiddenLayer.RecognizeMode(_weightsInHiddenLayer); var outputLayer = new OutputLayer(hiddenLayer.GetLayerNeurons()); outputLayer.RecognizeMode(_weightsInOutputLayer); ToRecognizeImage(outputLayer.GetOutputNeuron().Output); }
/// <summary> /// Обновить слои. /// </summary> /// <param name="outputLayer">Выходной слой.</param> /// <param name="hiddenLayer">Скрытый слой.</param> /// <param name="convolutionalLayer">Свёрточный слой.</param> /// <param name="inputLayer">Входной слой.</param> /// <param name="currentIteration">Текущая итерация.</param> private void LayersUpdate(OutputLayer outputLayer, HiddenLayer hiddenLayer, ConvolutionalLayer convolutionalLayer, InputLayer inputLayer, int currentIteration) { inputLayer.UpdateInputData(_dataSets[currentIteration]); convolutionalLayer.UpdateData(FilterCoreModel.GetCore, inputLayer.GetLayerNeurons()); var inputsToHiddenLayer = new List <double>(); convolutionalLayer.GetLayerNeurons().ForEach(neuron => inputsToHiddenLayer.Add(neuron.Output)); hiddenLayer.UpdateNeuronsInputs(inputsToHiddenLayer); var inputsToOutputLayer = new List <double>(); hiddenLayer.GetLayerNeurons().ForEach(neuron => inputsToOutputLayer.Add(neuron.Output)); outputLayer.UpdateNeuronInputs(inputsToOutputLayer); }