static void Evaluate(Perceptron p, double from, double to, double step)
        {
            string output = "";

            for (double i = from; i < to; i += step)
            {
                double res = p.Activate(new double[] { normalize(i, inputMin, inputMax) })[0];


                output += i + ";" + inverseNormalize(res, outputMin, outputMax) + "\n";
                Console.WriteLine(i + ";" + res + "\n");
            }

            System.IO.File.WriteAllText(outputPath, output);
        }
예제 #2
0
        private static void Evaluate(Perceptron p, double from, double to, double step)
        {
            var output = "";

            for (var i = from; i < to; i += step)
            {
                var res = p.Activate(new double[] { Normalize(i, inputMin, inputMax) })[0];


                output += i + ";" + InverseNormalize(res, outputMin, outputMax) + "\n";
                Console.WriteLine(i + ";" + res + "\n");
            }

            File.WriteAllText(outputPath, output);
        }
        static void Main(string[] args)
        {
            Perceptron p;

            int[]  net_def       = new int[] { inputCount, 10, 10, outputCount };
            double learning_rate = 0.3;
            double max_error     = 0.0001;
            int    max_iter      = 1000000;

            if (!loadNetwork)
            {
                ReadData();
                p = new Perceptron(net_def);

                while (!p.Learn(input, output, learning_rate, max_error, max_iter, neuralNetworkPath, 10000))
                {
                    p = new Perceptron(net_def);
                }
            }
            else
            {
                p = Perceptron.Load(neuralNetworkPath);
            }



            //Evaluate(p, 0, 5, 0.1);


            while (true)
            {
                double[] val = new double[inputCount];
                for (int i = 0; i < inputCount; i++)
                {
                    Console.WriteLine("Inserte valor " + i + ": ");
                    val[i] = normalize(double.Parse(Console.ReadLine()), inputMin, inputMax);
                }
                double[] sal = p.Activate(val);
                for (int i = 0; i < outputCount; i++)
                {
                    Console.Write("Respuesta " + i + ": " + inverseNormalize(sal[i], outputMin, outputMax) + " ");
                }
                Console.WriteLine("");
            }
        }
        public static Perceptron LoadNetwork(string neuralNetworkPath)
        {
            Perceptron p = null;

            try
            {
                if (File.Exists(neuralNetworkPath))
                {
                    var json = File.ReadAllText(neuralNetworkPath);
                    p = JsonConvert.DeserializeObject <Perceptron>(json);
                }
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }

            return(p);


            //var fs = new FileStream(neuralNetworkPath, FileMode.Open);
            //Perceptron p = null;

            //try
            //{
            //    var formatter = new BinaryFormatter();

            //    // Deserialize the hashtable from the file and
            //    // assign the reference to the local variable.
            //    p = (Perceptron)formatter.Deserialize(fs);
            //}
            //catch (SerializationException e)
            //{
            //    Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            //    throw;
            //}
            //finally
            //{
            //    fs.Close();
            //}

            //return p;
        }
예제 #5
0
        public static void Main()
        {
            Perceptron p;

            var net_def       = new int[] { inputCount, 10, 10, outputCount };
            var learning_rate = 0.3;
            var max_error     = 0.0001;
            var max_iter      = 1000000;

            loadNetwork = File.Exists(neuralNetworkPath);

            if (!loadNetwork)
            {
                ReadData();
                p = new Perceptron(net_def);

                while (!p.Learn(input, output, learning_rate, max_error, max_iter, neuralNetworkPath, 10000))
                {
                    p = new Perceptron(net_def);
                }
            }
            else
            {
                p = Perceptron.LoadNetwork(neuralNetworkPath);
            }

            p.SaveNetwork(neuralNetworkPath);

            //Evaluate(p, 0, 5, 0.1);


            while (true)
            {
                var inputs = new double[inputCount];
                for (var i = 0; i < inputCount;)
                {
                    Console.WriteLine($"Value #{i}: ");
                    var inputValue = Console.ReadLine();

                    if (!string.IsNullOrEmpty(inputValue))
                    {
                        if (double.TryParse(inputValue, out var input))
                        {
                            var inputNormalized = Normalize(input, inputMin, inputMax);
                            Console.WriteLine($"{input:F3} => {inputNormalized:F5}");

                            inputs[i] = inputNormalized;
                            i++;
                        }
                    }
                    else
                    {
                        goto byebye;
                    }
                }

                var outputs = p.Activate(inputs);
                //for (var i = 0; i < outputCount; i++)
                //{
                var sbInputs  = new StringBuilder();
                var sbOutputs = new StringBuilder();

                foreach (var inp in inputs)
                {
                    if (sbInputs.Length > 0)
                    {
                        sbInputs.Append(", ");
                    }
                    sbInputs.Append($"{inp:F5}");
                }

                foreach (var o in outputs)
                {
                    var outValueDenormalized = InverseNormalize(o, outputMin, outputMax);

                    if (sbOutputs.Length > 0)
                    {
                        sbOutputs.Append(", ");
                    }
                    sbOutputs.Append($"{o:F3}");
                }

                Console.Write($"Inputs: {sbInputs} => Outputs: {sbOutputs}");
                //}

                Console.WriteLine("");
            }

byebye:
            Console.WriteLine("bye bye!");
        }
예제 #6
0
        static void Main(string[] args)
        {
            Perceptron p;

            if (!loadNetwork)
            {
                ReadData();
                p = new Perceptron(new int[] { inputCount, 5, 5, outputCount });

                while (!p.Learn(input, output, 0.05, 0.01, 3000000))
                {
                    p = new Perceptron(new int[] { inputCount, 5, 5, outputCount });
                }

                if (saveNetwork)
                {
                    FileStream      fs        = new FileStream(neuralNetworkPath, FileMode.Create);
                    BinaryFormatter formatter = new BinaryFormatter();
                    try
                    {
                        formatter.Serialize(fs, p);
                    }
                    catch (SerializationException e)
                    {
                        Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                        throw;
                    }
                    finally
                    {
                        fs.Close();
                    }
                }
            }
            else
            {
                FileStream fs = new FileStream(neuralNetworkPath, FileMode.Open);
                try
                {
                    BinaryFormatter formatter = new BinaryFormatter();

                    // Deserialize the hashtable from the file and
                    // assign the reference to the local variable.
                    p = (Perceptron)formatter.Deserialize(fs);
                }
                catch (SerializationException e)
                {
                    Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                    throw;
                }
                finally
                {
                    fs.Close();
                }
            }



            //Evaluate(p, 0, 5, 0.1);


            while (true)
            {
                double[] val = new double[inputCount];
                for (int i = 0; i < inputCount; i++)
                {
                    Console.WriteLine("Inserte valor " + i + ": ");
                    val[i] = normalize(double.Parse(Console.ReadLine()), inputMin, inputMax);
                }
                double[] sal = p.Activate(val);
                for (int i = 0; i < outputCount; i++)
                {
                    Console.Write("Respuesta " + i + ": " + inverseNormalize(sal[i], outputMin, outputMax) + " ");
                }
                Console.WriteLine("");
            }
        }