示例#1
0
def train_main(part, train_loader, train_loader_check, test_loader, args):
    """
      Conducts main training procedure for particular part of k-fold splitting.
      *Iterates over the set of hyperparameters.
      *After training "scans" all the thresholds, generating the dimension values [d1,d2,d3,d4,d5] of particles. 
      *Calls an object of DataStore class to save the results and the best network states.
    """
    device = args.device
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    records = pd.DataFrame(columns=['Part_Num','Learning_rate', 'Layer_size', 'best_val(MSE)',\
                                    'last_val(MARE)', 'stopping epoch', 'counter',\
                                    'positive num', 'all'])
    # sets of learning rates and numbers of nodes in layers.
    lr_arr = np.array([0.0001, 0.001, 0.005, 0.01, 0.05, 0.1]).reshape(-1, 1)
    N_size = np.array([100, 200, 300, 400, 500, 600]).reshape(-1, 1)

    for i, LR in enumerate(lr_arr):
        for j, LS in enumerate(N_size):
            Learning_rate = LR[0]
            Layer_size = LS[0]

            # Data storing object
            ds = DataStore(part, Learning_rate, Layer_size)
            # Initialize
            loss_main = nn.MSELoss()
            loss_secondaty = MARE
            model, optimizer, train_step, counter = params_init(
                Layer_size, Learning_rate, loss_main)  #parameters
            complexity = model_complexity(model)
            print("%-10s\n %-15s %-4.6f \n %-15s %-4.2f\n %10s "%("////////////////////////",\
                                                           "Learning Rate:", Learning_rate,\
                                                           "Layer Size:",    Layer_size,\
                                                           "////////////////////////"))
            # #Train                                       #number of trainable parameters
            bestmodel, best_val_error_main, best_val_error_secondary, counter, epoch = train_procedure(model, \
                                                                  train_step,loss_main,
                                                                  loss_secondaty, \
                                                                  train_loader, train_loader_check, \
                                                                  test_loader,\
                                                                  args)#training
            #save the model
            ds.net_saver(bestmodel)
            # "Scanning" over the constant(threshold) scattering power values.
            vals_arr, threshold_arr = decrease(bestmodel)
            val_temp, threshold_temp = extract_positive(
                vals_arr, threshold_arr)
            # save thresholds and dimensions' values of a "scan" in the form: [d1, d2, d3, d4, d5]
            ds.dimensions_saver(val_temp, threshold_temp)

            new_row = {'Part_Num': part,
                      'Learning_rate': Learning_rate, \
                      'Layer_size': Layer_size,\
                      'best_val(MSE)': best_val_error_main,\
                      'last_val(MARE)': best_val_error_secondary,
                      'stopping epoch': epoch, \
                      'counter': counter,\
                      'positive num':val_temp.shape[0] ,\
                      'all': vals_arr.shape[0]}
            records = records.append(new_row, ignore_index=True)
    ds.records_saver(records)