コード例 #1
0
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (train)
            {
                Update();
                pretrain_index++;

                if (pretrain_index % 20 == 0)
                {
                    NetworkData.SaveNetwork("autosave_pretrain");
                    pretrain.SaveAutoencoderToFile(pretrained_layer + "_autosave_autoencoder.neural");
                    if (NetworkData.val != null)
                    {
                        var val_pair = pretrain.Validation(NetworkData.val);

                        if (min_err > val_pair.Key)
                        {
                            listBox1.Items.Insert(0, "New minimum! Save at min.neural");
                            NetworkData.SaveNetwork("min");
                            pretrain.SaveAutoencoderToFile(pretrained_layer + "_autoencoder_min.neural");
                        }

                        min_err = Math.Min(min_err, val_pair.Key);
                        listBox1.Items.Insert(0, pretrained_layer + " :Val_err: " + (float)val_pair.Key);
                        listBox1.Items.Insert(0, pretrained_layer + " :Val_win: " + (float)val_pair.Value);

                        if (val_pair.Key > min_err)
                        {
                            bad_val_count++;
                            listBox1.Items.Insert(0, "Bad validation " + bad_val_count);
                            if (bad_val_count > 12)
                            {
                                listBox1.Items.Insert(0, "Stop train. Bad validation");
                                try
                                {
                                    listBox1.Items.Insert(0, "Загружаю наилучшую сеть по валидации");
                                    NetworkData.LoadNetwork("min.neural");
                                }
                                catch
                                {
                                    listBox1.Items.Insert(0, "Ошибка загрузки!");
                                }
                                if (pretrained_layer < NetworkData.network.layers.Count - 2)
                                {
                                    listBox1.Items.Insert(0, "Начинаю предобучение следующего слоя");

                                    min_err = double.MaxValue;

                                    pretrained_layer++;
                                    while (!NetworkData.network.layers[pretrained_layer].ITrained && pretrained_layer < NetworkData.network.layers.Count - 1)
                                    {
                                        pretrained_layer++;
                                    }

                                    if (pretrained_layer < NetworkData.network.layers.Count - 1)
                                    {
                                        listBox1.Items.Insert(0, "Начинаю предобучение следующего слоя");

                                        NetworkData.network.LoadJSON(File.ReadAllText("min.neural"));
                                        NetworkData.network.CompileOnlyError();
                                        pretrain = new FullConPretrain(ref NetworkData.network);
                                        pretrain.SetupPretrain(pretrained_layer);
                                        pret_task = Task <double> .Run(() => pretrain.TrainStart(NetworkData.optimizer, NetworkData.train, pretrin_batch));
                                    }
                                }
                                else
                                {
                                    pret_task = null;
                                    return;
                                }
                            }
                        }
                        else
                        {
                            bad_val_count = 0;
                        }
                    }
                }

                //var res = NetworkData.network.GetOutput(test_arr);

                var err = pretrain.TrainContinue(NetworkData.optimizer, NetworkData.train, pretrin_batch);


                //listBox1.Items.Insert(0, test_res[0, 0, 0] + " " + test_res[0, 0, 1]);
                //listBox1.Items.Insert(0, (float)res[0, 0, 0] + " " + (float)res[0, 0, 1]);
                listBox1.Items.Insert(0, pretrained_layer + " : " + (float)err);

                if (listBox1.Items.Count > 1000)
                {
                    listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
                    listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
                    listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
                    listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
                }


                Update();
            }
        }
コード例 #2
0
 void ContinuePretrain()
 {
     pretrain_task = Task.Run <double>(() => pretrain.TrainContinue(opt, NetworkData.train, 32));
 }