public double TrainContinue(Optimizer optimizer, IDataEnumerator data, int batch)
        {
            optimizer.network = autoencoder;
            if (background.layers.Count != 0)
            {
                NetworkEnumerator nEm = new NetworkEnumerator(background, data);

                double err = optimizer.TrainBatchContinue(nEm, batch, 1).Last();
                return(err);
            }
            else
            {
                double err = optimizer.TrainBatchContinue(data, batch, 1).Last();
                return(err);
            }
        }
Example #2
0
        public static KeyValuePair <double, double> Action(Network network, Optimizer optimizer, IDataEnumerator data, IDataEnumerator val, int count, int batch = 32)
        {
            DateTime start;

            start = DateTime.Now;
            int layers = network.layers.Count;

            for (int i = 0; i < layers - 1; i++)
            {
                Network background;
                Network autoencoder;

                if (!network.layers[i].ITrained)
                {
                    continue;
                }

                background = new Network();
                for (int j = 0; j < i; j++)
                {
                    background.layers.Add(network.layers[j]);
                }

                autoencoder = new Network();
                autoencoder.layers.Add(network.layers[i]);
                autoencoder.layers.Add(network.layers[i].Mirror);

                background.CompileOnlyError();
                autoencoder.CompileOnlyError();

                NetworkEnumerator nEm = new NetworkEnumerator(background, data);
                MirrorEnumerator  Mem = new MirrorEnumerator(nEm);

                optimizer.network = autoencoder;


                double err = optimizer.TrainBatch(Mem, batch, 1).Last();


                for (int k = 1; k < count; k++)
                {
                    Console.Write("{0} ", k);
                    optimizer.TrainBatchContinue(Mem, batch, 1);
                    if ((DateTime.Now - start).TotalMinutes >= 10)
                    {
                        System.IO.File.WriteAllText("autosave_" + i + "_" + k + ".neural", network.SaveJSON());
                        Console.WriteLine("Save to " + "autosave_" + i + "_" + k + ".neural");
                        start = DateTime.Now;
                    }
                }



                Console.WriteLine("Trained {0} with error {1}", i, (float)err);
            }

            return(network.GetError(val));
        }
Example #3
0
        public double TrainContinue(Optimizer optimizer, IDataEnumerator data, int batch)
        {
            NetworkEnumerator nEm = new NetworkEnumerator(background, data);
            MirrorEnumerator  Mem = new MirrorEnumerator(nEm);

            optimizer.network = autoencoder;
            double err = optimizer.TrainBatchContinue(Mem, batch, 1).Last();

            return(err);
        }
Example #4
0
        public void Learn(int batch, int steps_per_epochs, int epochs = 1)
        {
            optimizer.Init(batch);
            for (int ep = 0; ep < epochs; ep++)
            {
                var    err      = optimizer.TrainBatchContinue(train_data, batch, steps_per_epochs);
                double err_mean = 0;
                for (int i = 0; i < err.Length; i++)
                {
                    err_mean += err[i];
                }
                err_mean /= err.Length;
                Console.WriteLine("Epoch error: {0}", err_mean);
                metrics_values["err"] = err_mean;
                double[] m_vals = optimizer.network.GetMeanMetrics(train_data, metrics);
                for (int i = 0; i < metrics.Count; i++)
                {
                    Console.WriteLine("Epoch {0}: {1}", metrics[i].Name, m_vals[i]);

                    metrics_values[metrics[i].Name] = m_vals[i];
                }

                if (val_data != null)
                {
                    var val_err = optimizer.network.GetError(val_data);
                    Console.WriteLine("Validation error: {0}", val_err.Key);
                    metrics_values["val_err"] = val_err.Key;
                    m_vals = optimizer.network.GetMeanMetrics(val_data, metrics);
                    for (int i = 0; i < metrics.Count; i++)
                    {
                        Console.WriteLine("Validation {0}: {1}", metrics[i].Name, m_vals[i]);
                        metrics_values["val_" + metrics[i].Name] = m_vals[i];
                    }
                }

                for (int i = 0; i < callbacks.Count; i++)
                {
                    callbacks[i].Action(this);
                }
            }
        }
        public KeyValuePair <double, double> TrainBatch(Optimizer optimizer, IDataEnumerator data, IDataEnumerator val, int count, int batch = 32)
        {
            DateTime          start = DateTime.Now;
            NetworkEnumerator nEm   = new NetworkEnumerator(background, data);

            optimizer.network = autoencoder;

            optimizer.TrainBatch(nEm, batch, 1).Last();

            for (int k = 1; k < count; k++)
            {
                optimizer.TrainBatchContinue(nEm, batch, 1);
                if ((DateTime.Now - start).TotalMinutes >= 10)
                {
                    System.IO.File.WriteAllText("autosave_" + k + ".neural", network.SaveJSON());
                    start = DateTime.Now;
                }
            }

            return(autoencoder.GetError(val));
        }