public void AddDataAfterEducate(NeuralNetworkObj neuralNetworkObj, Dictionary <double[], double[]> prepareData) { data.Add("#########################################"); data.Add("#########################################"); int id = 1; foreach (var entity in prepareData) { double[] inputVector = entity.Key; double[] expectedResultVector = entity.Value; neuralNetworkObj.Clear(); neuralNetworkObj.SetInputVector(inputVector); neuralNetworkObj.Process(); double[] resultVector = GetResultVector(neuralNetworkObj); data.Add($"ID: {id}"); data.Add($"Epoch: {neuralNetworkObj.GetCountEpochPassed()} (After educate)"); data.Add($"Error: {this.errorMethod.GetError(expectedResultVector, resultVector).ToString()}"); data.Add("Expected Result:"); foreach (var item in expectedResultVector) { data.Add(item.ToString()); } data.Add("Result:"); foreach (var item in resultVector) { data.Add(item.ToString()); } data.Add("#########################################"); id++; } }
public void Save(NeuralNetworkObj neuralNetworkObj) { BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream($"{this.pathToFileStorage}\\{neuralNetworkObj.Name}.dat", FileMode.Create)) { formatter.Serialize(fs, neuralNetworkObj); } }
public void GenerateRandom() { Factory neuralNetworkFactory = new Factory(); NeuralNetworkObj neuralNetwork = neuralNetworkFactory.CreateWithRandomWeight(this.GetNetworkName(), this.GetCountNeuronsInLayer()); Manager neuralNetworkManager = new Manager(); neuralNetworkManager.Save(neuralNetwork); }
private Synapse GetSynapse(NeuralNetworkObj neuralNetworkObj, int currentLayerId, int neuronId, int nextNeuronId) { foreach (Synapse synapse in neuralNetworkObj.GetLayerById(currentLayerId).GetNeuronById(neuronId).GetSynapses()) { if (synapse.NeuronObj.Id == nextNeuronId) { return(synapse); } } return(null); }
private void networkComboBox_SelectionChangeCommitted(object sender, EventArgs e) { string networkName = networkComboBox.SelectedItem.ToString(); Manager neuralNetworkManager = new Manager(); this.neuralNetwork = neuralNetworkManager.Get(networkName); epochPassedLabel.Text = $"Epoch passed: {this.neuralNetwork.GetCountEpochPassed()}"; charRecognizerGroupBox.Enabled = true; }
public NeuralNetworkObj GetNeuralNetwork() { if (this.neuralNetworkObj != null) { return(this.neuralNetworkObj); } Manager neuralNetworkManager = new Manager(); this.neuralNetworkObj = neuralNetworkManager.Get(this.GetNetworkName()); return(this.neuralNetworkObj); }
private double[] GetResultVector(NeuralNetworkObj neuralNetworkObj) { double[] result = new double[neuralNetworkObj.GetLastLayer().GetCountNeurons()]; List <NeuronObj> neurons = neuralNetworkObj.GetLastLayer().GetListNeurons(); for (int i = 0; i < neurons.Count; i++) { result[i] = neurons[i].GetOutputData(); } return(result); }
private List <EducationLayer> GetEducationNetwork(NeuralNetworkObj neuralNetworkObj) { if (this._educationNetwork != null) { return(this._educationNetwork); } List <EducationLayer> result = new List <EducationLayer>(); foreach (Layer layer in neuralNetworkObj.GetListLayers()) { EducationLayer educationLayer = new EducationLayer(layer.Id); foreach (NeuronObj neuron in layer.GetListNeurons()) { educationLayer.AddNeuron(new EducationNeuron(neuron)); } result.Add(educationLayer); } for (int educationLayerId = 0; educationLayerId < result.Count - 1; educationLayerId++) { EducationLayer educationLayer = result[educationLayerId]; EducationLayer nextEducationLayer = result[educationLayerId + 1]; foreach (EducationNeuron educationNeuron in educationLayer.GetListNeurons()) { foreach (EducationNeuron nextLayerEducationNeuron in nextEducationLayer.GetListNeurons()) { Synapse synapse = this.GetSynapse( neuralNetworkObj, educationLayer.Id, educationNeuron.NeuronObj.Id, nextLayerEducationNeuron.NeuronObj.Id ); if (synapse == null) { throw new Exception("Something goes wrong with id layers."); } educationNeuron.AddEducationSynapse(new EducationSynapse(nextLayerEducationNeuron, synapse)); } } } this._educationNetwork = result; return(this._educationNetwork); }
public NumberRecognizerNeuralNetwork(NeuralNetworkObj neuralNetwork) { this.name = neuralNetwork.Name; this.SetNeuralNetworkObj(neuralNetwork); neuronsInLayer = new int[neuralNetwork.GetCountLayers()]; int index = 0; foreach (Layer layer in neuralNetwork.GetListLayers()) { this.neuronsInLayer[index] = layer.GetCountNeurons(); index++; } this.countNeuronsInFirstLayer = neuralNetwork.GetFirstLayer().GetCountNeurons(); }
public NeuralNetworkObj CreateWithRandomWeight(string name, int[] countNeuronsInLayer) { NeuralNetworkObj neuralNetworkObj = new NeuralNetworkObj(name); for (int layerId = 0; layerId < countNeuronsInLayer.Length; layerId++) { Layer layer = new Layer(layerId); if (layerId == 0) { layer.SetPositionFirst(); } else if (layerId == countNeuronsInLayer.Length - 1) { layer.SetPositionLast(); } for (int neuronId = 0; neuronId < countNeuronsInLayer[layerId]; neuronId++) { NeuronObj neuronObj = new NeuronObj(neuronId); layer.AddNeuron(neuronObj); } neuralNetworkObj.AddLayer(layer); } Random rand = new Random(); List <Layer> layers = neuralNetworkObj.GetListLayers(); for (int layerId = 0; layerId < layers.Count - 1; layerId++) { Layer layer = layers[layerId]; Layer nextLayer = layers[layerId + 1]; foreach (NeuronObj neuron in layer.GetListNeurons()) { foreach (NeuronObj nextLayerNeuron in nextLayer.GetListNeurons()) { double weight = Convert.ToDouble(rand.Next(-100, 100)) / 100; neuron.AddSynapse(new Synapse(nextLayerNeuron, weight)); } } } return(neuralNetworkObj); }
public void SaveReport(NeuralNetworkObj neuralNetworkObj) { var dirPath = $"{this.pathToFileStorage}\\REPORT_{neuralNetworkObj.Name}"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } using (StreamWriter sw = new StreamWriter($"{dirPath}\\report_{neuralNetworkObj.GetCountEpochPassed()}.txt", false, System.Text.Encoding.Unicode)) { foreach (var text in data) { sw.WriteLine(text); } } this.data = new List <string>(); }
public int GetAnswer(double[] inputVector) { if (inputVector.Length != INPUT_VECTOR_LENGTH) { throw new Exception("Invalid input vector length."); } NeuralNetworkObj neuralNetwork = this.GetNeuralNetwork(); neuralNetwork.SetInputVector(inputVector); neuralNetwork.Process(); if (neuralNetwork.GetLastLayer().GetNeuronById(1).GetOutputData() >= MIN_CORRECT_ANSWER) { return(1); } return(0); }
private void educateNetworkButton_Click(object sender, EventArgs e) { UncertaintyPropagationMethod uncertaintyPropagationMethod = new UncertaintyPropagationMethod(LEARNING_RATE); NumberRecognizerNeuralNetwork numberRecognizerNeuralNetwork = new NumberRecognizerNeuralNetwork(this.neuralNetwork); MachineLearning.NeuralNetwork.Report.Manager reportManager = new MachineLearning.NeuralNetwork.Report.Manager(); NeuralNetworkObj neuralNetworkObj = numberRecognizerNeuralNetwork.GetNeuralNetwork(); var prepareData = this.GetPrepareData(neuralNetworkObj.GetLastLayer().GetCountNeurons()); int countEpoch = Convert.ToInt16(educateNetworkNumericUpDown.Value); for (int epoch = 0; epoch < countEpoch; epoch++) { reportManager.AddDataBeforeEducate(neuralNetworkObj, prepareData); for (int iteration = 0; iteration < COUNT_ITERATION; iteration++) { foreach (var entity in prepareData) { double[] inputVector = entity.Key; double[] expectedResultVector = entity.Value; neuralNetworkObj = uncertaintyPropagationMethod.GetTaughtNeuralNetwork( numberRecognizerNeuralNetwork.GetNeuralNetwork(), inputVector, expectedResultVector ); } } educateNetworkProgressBar.Value = Convert.ToInt16((epoch + 1.0) / countEpoch * 100); reportManager.AddDataAfterEducate(neuralNetworkObj, prepareData); reportManager.SaveReport(neuralNetworkObj); neuralNetworkObj.EpochPassed(); } numberRecognizerNeuralNetwork.UpdateNeuralNetwork(neuralNetworkObj); }
public NeuralNetworkObj GetTaughtNeuralNetwork(NeuralNetworkObj neuralNetworkObj, double[] inputVector, double[] expectedResultVector) { double[] resultVector = this.GetResultVector(neuralNetworkObj, inputVector); List <EducationLayer> educationNetwork = this.GetEducationNetwork(neuralNetworkObj); for (int educationLayerId = educationNetwork.Count - 1; educationLayerId >= 0; educationLayerId--) { EducationLayer educationLayer = educationNetwork[educationLayerId]; foreach (EducationNeuron educationNeuron in educationLayer.GetListNeurons()) { NeuronObj neuron = educationNeuron.NeuronObj; if (neuron.IsInLastLayer()) { //deltaWeight0 = (OUT(ideal) - OUT(actual)) * f'(IN) educationNeuron.WeightDelta = (expectedResultVector[neuron.Id] - resultVector[neuron.Id]) * activationFunc.GetDerivativeValue(neuron.GetInputData()); } else { //deltaWeightH = f'(IN) * sum(Wi * delataWeighti) double sumWeightDeltaInNextLayer = 0; foreach (EducationSynapse educationSynapse in educationNeuron.GetEducationSynapses()) { sumWeightDeltaInNextLayer += educationSynapse.Synapse.Weight * educationSynapse.EducationNeuron.WeightDelta; } educationNeuron.WeightDelta = this.activationFunc.GetDerivativeValue(neuron.GetInputData()) * sumWeightDeltaInNextLayer; foreach (EducationSynapse educationSynapse in educationNeuron.GetEducationSynapses()) { //grad(A, B) = deltaWeight(B) * OUT(A) double grad = educationSynapse.EducationNeuron.WeightDelta * educationNeuron.NeuronObj.GetOutputData(); educationSynapse.Synapse.Weight += this.learningRate * grad;//todo moment a*deltaWi } } } } return(neuralNetworkObj); }
public int GetNumberFromImgVector(double[] inputVector) { if (inputVector.Length != this.countNeuronsInFirstLayer) { throw new Exception("Invalid input vector length."); } NeuralNetworkObj neuralNetworkObj = this.GetNeuralNetwork(); neuralNetworkObj.Clear(); neuralNetworkObj.SetInputVector(inputVector); neuralNetworkObj.Process(); foreach (NeuronObj neuron in neuralNetworkObj.GetLastLayer().GetListNeurons()) { if (neuron.GetOutputData() > MINIMUM_OUTPUT_DATA_FOR_SUCCESS_RESULT) { return(neuron.Id); } } return(-1); }
public void UpdateNeuralNetwork(NeuralNetworkObj neuralNetwork) { Manager neuralNetworkManager = new Manager(); neuralNetworkManager.Save(neuralNetwork); }
protected void SetNeuralNetworkObj(NeuralNetworkObj neuralNetworkObj) { this.neuralNetworkObj = neuralNetworkObj; }