public void TestSOM() { // create the training set IMLDataSet training = new BasicMLDataSet( SOMInput, null); // Create the neural network. var network = new SOMNetwork(4, 2) { Weights = new Matrix(MatrixArray) }; var train = new BasicTrainSOM(network, 0.4, training, new NeighborhoodSingle()) { ForceWinner = true }; int iteration = 0; for (iteration = 0; iteration <= 100; iteration++) { train.Iteration(); } IMLData data1 = new BasicMLData( SOMInput[0]); IMLData data2 = new BasicMLData( SOMInput[1]); int result1 = network.Classify(data1); int result2 = network.Classify(data2); Assert.IsTrue(result1 != result2); }
public static List <Color> FromImageSOM(string filepath, int color_samples, int palette_size) { WriteableBitmap w = new WriteableBitmap(new BitmapImage(new Uri(filepath))); byte[] pixels = new byte[(long)w.PixelHeight * (long)w.PixelWidth * (long)w.Format.BitsPerPixel / (long)8]; w.CopyPixels(pixels, w.PixelWidth * w.Format.BitsPerPixel / 8, 0); var image_width = w.PixelWidth; var image_height = w.PixelHeight; var s = new SOMNetwork(3, color_samples); var t = 1.0; for (var i = 0; i < 10000; i++) { var cx = ThreadSafeRandom.Next(image_width); var cy = ThreadSafeRandom.Next(image_height); double[] c = new double[3]; c[0] = pixels[4 * (cy * image_width + cx) + 2]; c[1] = pixels[4 * (cy * image_width + cx) + 1]; c[2] = pixels[4 * (cy * image_width + cx) + 0]; s.Train(c, t); t -= 1 / 10000.0; } return(ColorPalette.CreatePalette( s.Neurons.Select(i => Color.FromArgb(255, (byte)(i.weights[0]), (byte)(i.weights[1]), (byte)(i.weights[2]))).Distinct().ToList(), palette_size)); }
private SOMNetwork CreateNetwork() { var result = new SOMNetwork(3, WIDTH * HEIGHT); result.Reset(); return(result); }
private SOMNetwork CreateNetwork() { SOMNetwork result = new SOMNetwork(3, SOMColors.WIDTH * SOMColors.HEIGHT); result.Reset(); return(result); }
private void btnBeginTraining_Click(object sender, EventArgs e) { if (!this.training) { int inputCount = OCRForm.DOWNSAMPLE_HEIGHT * OCRForm.DOWNSAMPLE_WIDTH; this.trainingSet = new BasicMLDataSet(); foreach (char ch in this.letterData.Keys) { BasicMLData item = new BasicMLData(inputCount); bool[] data = this.letterData[ch]; for (int i = 0; i < inputCount; i++) { item[i] = data[i] ? 0.5 : -0.5; } this.trainingSet.Add(item); } this.network = new SOMNetwork(this.downsampled.Length, this.letterData.Count); this.network.Reset(); this.training = true; Thread thread = new Thread(TrainNetwork); thread.Start(); } else { this.training = false; } }
public void Fit(List <List <double> > xs) { SOMNetwork n = (SOMNetwork)this.network; var som = (SOM)algorithm; int epoch_executed = 0; while (epoch_executed < this.epoch) { SOMSetting setting = GetSettingByEPoch(epoch_executed); for (var index = 0; index < xs.Count; index++) { var x_vector = xs[index]; som.LearningRate = setting.LearningRate; som.Radius = setting.Radius; som.Process(x_vector, null); } //network.Display(); //Console.ReadKey(); epoch_executed++; Console.WriteLine(string.Format("SYS: epoch {0} done.", epoch_executed)); } }
/// <summary> /// Generate the RSOM network. /// </summary> public IMLMethod Generate() { var som = new SOMNetwork(_inputNeurons, _outputNeurons); som.Reset(); return(som); }
public Simulation CreateSimulation() { var activationFunction = CreateActivationFunction(); var neuron = CreateNeuron(activationFunction); INeuralNetwork neuralNetwork = null; if (Network == "NeuralNetwork") { var weightInitializer = new WeightInitializer(Weights.Min, Weights.Max); neuralNetwork = new NeuralNetwork(neuron, Inputs, Outputs, weightInitializer, HiddenNeurons); } else if (Network == "SOMNetwork") { neuralNetwork = new SOMNetwork(neuron, Inputs, Outputs, MaxEpoch); } var simulation = new Simulation(neuralNetwork, true); simulation.ValidationData = ValidationData; simulation.ImagesDisturbanceProbability = ImagesDisturbanceProbability; simulation.ImageDisturbanceMaxDifference = ImageDisturbanceMaxDifference; simulation.MaxEpoch = MaxEpoch; simulation.Config = this; return(simulation); }
public void TestPersistSerial() { SOMNetwork network = Create(); SerializeObject.Save(SERIAL_FILENAME.ToString(), network); SOMNetwork network2 = (SOMNetwork)SerializeObject.Load(SERIAL_FILENAME.ToString()); Validate(network2); }
public void TestPersistEG() { SOMNetwork network = Create(); EncogDirectoryPersistence.SaveObject(EG_FILENAME, network); SOMNetwork network2 = (SOMNetwork)EncogDirectoryPersistence.LoadObject(EG_FILENAME); Validate(network2); }
/// <summary> /// Construct the object. /// </summary> /// /// <param name="network">The network to train.</param> /// <param name="training">The training data.</param> public SOMClusterCopyTraining(SOMNetwork network, IMLDataSet training) : base(TrainingImplementationType.OnePass) { _network = network; Training = training; if (_network.OutputCount < training.Count) { throw new NeuralNetworkError( "To use cluster copy training you must have at least as many output neurons as training elements."); } }
/// <summary> /// Calculate the output of the SOM, for each output neuron. Typically, /// you will use the classify method instead of calling this method. /// </summary> /// <param name="som">The SOM to use.</param> /// <param name="input">The input.</param> /// <returns>The output.</returns> private static IMLData Compute(SOMNetwork som, IMLData input) { var result = new BasicMLData(som.OutputCount); for (int i = 0; i < som.OutputCount; i++) { Matrix optr = som.Weights.GetRow(i); Matrix inputMatrix = Matrix.CreateRowMatrix(input); result[i] = MatrixMath.DotProduct(inputMatrix, optr); } return(result); }
/// <summary> /// Create an instance of competitive training. /// </summary> /// <param name="network">The network to train.</param> /// <param name="learningRate">The learning rate, how much to apply per iteration.</param> /// <param name="training">The training set (unsupervised).</param> /// <param name="neighborhood">The neighborhood function to use.</param> public BasicTrainSOM(SOMNetwork network, double learningRate, IMLDataSet training, INeighborhoodFunction neighborhood) : base(TrainingImplementationType.Iterative) { _neighborhood = neighborhood; Training = training; LearningRate = learningRate; _network = network; _inputNeuronCount = network.InputCount; _outputNeuronCount = network.OutputCount; ForceWinner = false; // setup the correction matrix _correctionMatrix = new Matrix(_outputNeuronCount, _inputNeuronCount); // create the BMU class _bmuUtil = new BestMatchingUnit(network); }
public SOMColors() { InitializeComponent(); this.network = CreateNetwork(); this.gaussian = new NeighborhoodRBF(RBFEnum.Gaussian, SOMColors.WIDTH, SOMColors.HEIGHT); this.train = new BasicTrainSOM(this.network, 0.01, null, gaussian); train.ForceWinner = false; samples = new List <IMLData>(); for (int i = 0; i < 15; i++) { IMLData data = new BasicMLData(3); data.Data[0] = RangeRandomizer.Randomize(-1, 1); data.Data[1] = RangeRandomizer.Randomize(-1, 1); data.Data[2] = RangeRandomizer.Randomize(-1, 1); samples.Add(data); } this.train.SetAutoDecay(100, 0.8, 0.003, 30, 5); }
private void Do() { network = CreateNetwork(); gaussian = new NeighborhoodRBF(RBFEnum.Gaussian, WIDTH, HEIGHT); train = new BasicTrainSOM(network, 0.01, null, gaussian); train.ForceWinner = false; samples = new List <IMLData>(); for (int i = 0; i < 200; i++) { var data = new BasicMLData(3); data.Data[0] = RangeRandomizer.Randomize(-1, 1); data.Data[1] = RangeRandomizer.Randomize(-1, 1); data.Data[2] = RangeRandomizer.Randomize(-1, 1); samples.Add(data); } train.SetAutoDecay(100, 0.8, 0.003, 30, 5); iteration = 0; updateTimer.Enabled = true; }
/// <summary> /// Construct a BestMatchingUnit class. The training class must be provided. /// </summary> /// /// <param name="som">The SOM to evaluate.</param> public BestMatchingUnit(SOMNetwork som) { _som = som; }
static void Main(string[] args) { BasicMLDataSet data_training = new BasicMLDataSet(); Random rdn = new Random(); //////////////////////////////////////////////////////////////////////////// //simulação de dados por arquivo: var neuralFile = File.ReadAllLines(@"C:\Users\bredi\Desktop\TCC\TCC\neural_1.txt"); List <string> NeuralList = new List <string>(neuralFile); double[][] entradafull = new double[NeuralList.Count][]; double[][] saidafull = new double[NeuralList.Count][]; int i = 0; foreach (var item in NeuralList) { var t = item.Split(new string[] { "::" }, StringSplitOptions.None); double[] entrada = new double[] { //System.Convert.ToDouble(t[0]),//hora System.Convert.ToDouble(t[1]), //tempA System.Convert.ToDouble(t[2]) //setA //System.Convert.ToDouble(t[3]),//tempB //System.Convert.ToDouble(t[4])//setB }; entradafull[i] = entrada; /*double a = System.Convert.ToDouble(t[5]); * if (a == 1) * a = 0.5f; * else * a = 0.5f; * double b = System.Convert.ToDouble(t[6]); * if (b == 1) * b = 0.5f; * else if (b == 0) * b = 0.5f;*/ double[] saida = new double[] { System.Convert.ToDouble(t[5])//saidaA //System.Convert.ToDouble(t[6])//saidaB }; saidafull[i] = saida; i++; data_training.Add(new BasicMLData(entrada), null); } //IMLDataSet data_training = new BasicMLDataSet(entradafull, saidafull);//ANTIGO COM SAIDA ////////////////////////////////////////////////////////// int N_entradas = 2; int tamanho_X = 100; //100 int tamanho_Y = 100; //100 int N_saidas = tamanho_X * tamanho_Y; int interacoesPlanejada = 1000; int vizinho_inicial = 50;//50 int vizinho_final = 1; double rate_inicial = 1; double rate_final = 0.1; //Criação de rede SOM.(número de entradas, número de saídas) SOMNetwork network = new SOMNetwork(N_entradas, N_saidas); network.Reset(); //Criação da função de ativação.(função gaussiana 2D, largura da rede, altura da rede) NeighborhoodRBF gaussian = new NeighborhoodRBF(RBFEnum.MexicanHat, tamanho_X, tamanho_Y); //(rede neural, taxa de aprendizado, conjunto de treinamento, função de vizinhança) BasicTrainSOM train = new BasicTrainSOM(network, 0.01, null, gaussian); train.ForceWinner = false; train.SetAutoDecay(interacoesPlanejada, rate_inicial, rate_final, vizinho_inicial, vizinho_final); //TREINAMENTO RANDOMICO: for (int decay = 0; decay < interacoesPlanejada; decay++) { var idx = int.Parse(Math.Round(rdn.NextDouble() * saidafull.Length).ToString()) - 1; if (idx == -1) { idx = 0; } var data = data_training[idx].Input; train.TrainPattern(data); train.AutoDecay(); Console.WriteLine(string.Format("Epoch {0}, Rate: {1}, Radius: {2}, Error: {3}", decay, train.LearningRate, train.Neighborhood.Radius, train.Error)); } /*for (int tx = 0; tx < interacoesPlanejada; tx++) * { * train.Iteration(); * train.AutoDecay(); * Console.WriteLine(string.Format("Epoch {0}, Rate: {1}, Radius: {2}, Error: {3}", i, train.LearningRate, train.Neighborhood.Radius, train.Error)); * }*/ ////////////////////////////////////////////////////////// //arquivo visual////////////////////////////////////////////////////////// string[,] arrayprint = new string[tamanho_X, tamanho_Y]; for (int x = 0; x < tamanho_X; x++) { for (int y = 0; y < tamanho_Y; y++) { arrayprint[x, y] = " "; } } /*for (int TempA = 15; TempA < 25; TempA++) * { * for (int SetA = 15; SetA < 25; SetA++) * { * for (int TempB = 15; TempB < 25; TempB++) * { * for (int SetB = 15; SetB < 25; SetB++) * { * BasicMLData dataentradateste = new BasicMLData(new double[] { TempA, SetA, TempB, SetB }); * var retorno = network.Classify(dataentradateste); * //Console.WriteLine(retorno + " ||| SetA: " + SetA + " | TempA: " + TempA + " ||| SetB: " + 20 + " | TempB: " + 0); * var tuple = convertToXY(retorno, tamanho_X, tamanho_Y); * var array_v = arrayprint[tuple.Item1, tuple.Item2]; * if(array_v == " ") * { * string saida = ""; * if(TempA >= SetA) * saida += "a"; * else if(TempA < SetA) * saida += "A"; * else * saida += "#"; * * if (TempB >= SetB) * saida += "b"; * else if (TempB < SetB) * saida += "B"; * else * saida += "#"; * * arrayprint[tuple.Item1, tuple.Item2] = saida; * } * } * } * * } * }*/ List <int> Lista_0 = new List <int>(); List <int> Lista_1 = new List <int>(); for (int TempA = -49; TempA < 50; TempA++) { for (int SetA = -49; SetA < 50; SetA++) { BasicMLData dataentradateste = new BasicMLData(new double[] { Normalizacao.Norm_Temp(TempA), Normalizacao.Norm_Temp(SetA) }); var retorno = network.Classify(dataentradateste); //Console.WriteLine(retorno + " ||| SetA: " + SetA + " | TempA: " + TempA + " ||| SetB: " + 20 + " | TempB: " + 0); var tuple = convertToXY(retorno, tamanho_X, tamanho_Y); var array_v = arrayprint[tuple.Item1, tuple.Item2]; if (array_v == " ") { string saida = " "; if (TempA >= SetA) { if (Lista_1.Contains(retorno)) { saida += "#"; } else { Lista_0.Add(retorno); saida += "0"; } } else if (TempA < SetA) { if (Lista_0.Contains(retorno)) { saida += "#"; } else { Lista_1.Add(retorno); saida += "1"; } } else { saida += "#"; } arrayprint[tuple.Item1, tuple.Item2] = saida; } } } StringBuilder fileContents = new StringBuilder(); for (int x = 0; x < tamanho_X; x++) { for (int y = 0; y < tamanho_Y; y++) { fileContents.Append(arrayprint[x, y]); } fileContents.AppendLine("|"); } File.WriteAllText(@"C:\Users\bredi\Documents\mapaneural.txt", fileContents.ToString()); ////////////////////////////////////////////////////////// ////salvar network: string path = Path.Combine(@"C:\Users\bredi\Desktop\TCC\TCC", "redeneural" + DateTime.Now.Ticks + ".txt"); if (File.Exists(path)) { File.Delete(path); } FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write); PersistSOM persistSOM = new PersistSOM(); persistSOM.Save(fs, network); fs.Close(); ////////////////////////////////////////////////////////// //testes////////////////////////////////////////////////////////// DateTime datahora_atual = DateTime.MinValue; do { DateTime datahora = Simulation.Memory.Get().dmDateTime.DataHora; var Dados_D = Simulation.Input.Termostato_D(); var Dados_E = Simulation.Input.Termostato_E(); if (datahora >= datahora_atual.AddSeconds(.5)) { datahora_atual = datahora; //double hora = Normalizacao.Norm_DataHoraSeg(datahora); //BasicMLData dataentradateste = new BasicMLData(new double[] { hora, TempA, SetA, TempB, SetB }); //BasicMLData dataentradateste = new BasicMLData(new double[] { TempA, SetA, TempB, SetB }); BasicMLData dataentradateste = new BasicMLData(new double[] { Dados_D.TemperaturaNormalizado, Dados_D.SetPointNormalizado }); var retorno = network.Winner(dataentradateste); if (Lista_0.Contains(retorno)) { //desligar Simulation.Output.DesligarAquecedor_D(); Simulation.Output.DesligarAquecedor_E(); Console.WriteLine(retorno + " | OFF | "); } else if (Lista_1.Contains(retorno)) { //ligar Simulation.Output.LigarAquecedor_D(); Simulation.Output.LigarAquecedor_E(); Console.WriteLine(retorno + " | ON | "); } else { Console.WriteLine(retorno + " | OUT | "); } } }while (true); }
/// <summary> /// Calculate the output of the SOM, for each output neuron. Typically, /// you will use the classify method instead of calling this method. /// </summary> /// <param name="som">The SOM to use.</param> /// <param name="input">The input.</param> /// <returns>The output.</returns> private static IMLData Compute(SOMNetwork som, IMLData input) { IMLData result = new BasicMLData(som.OutputCount); for (int i = 0; i < som.OutputCount; i++) { Matrix optr = som.Weights.GetRow(i); Matrix inputMatrix = Matrix.CreateRowMatrix(input.Data); result[i] = MatrixMath.DotProduct(inputMatrix, optr); } return result; }
public SOMTrainer(SOMNetwork network, int epoch, SOM algorithm, List <SOMSetting> setting) : base(network, epoch, algorithm) { this.setting = setting; }
/// <summary> /// Construct the object. /// </summary> /// /// <param name="network">The network to train.</param> /// <param name="training">The training data.</param> public SOMClusterCopyTraining(SOMNetwork network, IMLDataSet training) : base(TrainingImplementationType.OnePass) { _network = network; Training = training; }
private SOMNetwork Create() { SOMNetwork network = new SOMNetwork(4, 2); return(network); }
public SOM(SOMNetwork network) { this.network = network; }
private void Validate(SOMNetwork network) { Assert.AreEqual(4, network.InputCount); Assert.AreEqual(2, network.OutputCount); Assert.AreEqual(8, network.Weights.ToPackedArray().Length); }
public static void Demo() { SOMNetwork network = SOMNetwork.Create(9, 3); network.Display(); var dataSet = DataGenerator.GenerateDataSet3(); var som = new SOM(network); List <SOMSetting> setting = new List <SOMSetting>(); setting.Add(new SOMSetting() { FromEpoch = 0, ToEpoch = 500, LearningRate = 0.8, Radius = 2 }); setting.Add(new SOMSetting() { FromEpoch = 501, ToEpoch = 1000, LearningRate = 0.4, Radius = 1 }); SOMTrainer trainer = new SOMTrainer(network, 1000, som, setting); //Normalizer2 normalizer = new Normalizer2(); Normalizer2 normalizer = new Normalizer2(); normalizer.Fit(dataSet.XList); var normalizedX = normalizer.Normalize(dataSet.XList); trainer.Fit(normalizedX); List <List <double> > convertedX = trainer.GetOutputs(normalizedX); List <List <double> > convertedX2 = trainer.GetOutputs2(normalizedX); network.Display(); Console.WriteLine("OLD VECTORS===>"); Utils.DisplayListList(dataSet.XList); Console.WriteLine("OLD VECTORS(NORMALIZED)===>"); Utils.DisplayListList(normalizedX); Console.WriteLine("NEW CLUSTERED VECTORS===>"); Utils.DisplayListList(convertedX); Console.WriteLine("NEW CLUSTERED VECTORS(DISTANCE)===>"); Utils.DisplayListList(convertedX2); StringBuilder sb_x = new StringBuilder(); StringBuilder sb_y = new StringBuilder(); foreach (var l in convertedX2) { var maxIndex = l.IndexOf(l.Max()); for (var x = 0; x < network.OutputNeuronCountPerDim; x++) { for (var y = 0; y < network.OutputNeuronCountPerDim; y++) { if (x * network.OutputNeuronCountPerDim + y == maxIndex) { sb_x.Append(x + ","); sb_y.Append(y + ","); Console.Write("(" + x + "," + y + "),"); } } } Console.WriteLine(); } StringBuilder sb = new StringBuilder(); sb.AppendLine("import matplotlib.pyplot as plt"); sb.AppendLine("x1=[" + sb_x.ToString().TrimEnd(",".ToCharArray()) + "]"); sb.AppendLine("y1=[" + sb_y.ToString().TrimEnd(",".ToCharArray()) + "]"); sb.AppendLine("plt.plot(x1,y1,'b^')"); sb.AppendLine("plt.show()"); var file = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "display.py"); File.WriteAllText(file, sb.ToString()); Console.WriteLine("saved to path: " + file); System.Diagnostics.Process.Start("C:\\ProgramData\\Anaconda3\\envs\\keras\\python.exe", "\"" + file + "\""); }