def train_network(self, data_dir, epochs, batch_size, weight_decay, lr,
                      output_dir):

        self.cuda()
        self.train(True)

        data = zarr.open(data_dir, 'r')

        train_data = data['train_data']
        labels = data['labels']

        n_batches = round(train_data.shape[0] / batch_size)
        opt = optim.Adam(self.parameters(), weight_decay=weight_decay, lr=lr)
        loss_f = nn.CrossEntropyLoss()
        scheduler = optim.lr_scheduler.StepLR(opt, step_size=5, gamma=0.5)

        losses = []

        for epoch in range(epochs):

            start = time.time()
            scheduler.step()
            avg_loss = numpy.zeros((1, ))
            n_samples = 1

            for idx in range(n_batches):

                opt.zero_grad()
                train_batch = train_data[idx * batch_size:idx * batch_size +
                                         batch_size]
                label_batch = labels[idx * batch_size:idx * batch_size +
                                     batch_size]
                train_batch = Variable(torch.LongTensor(train_batch)).cuda()
                label_batch = Variable(torch.LongTensor(label_batch),
                                       requires_grad=False).cuda()
                output = self(train_batch)
                loss = loss_f(output, label_batch)
                loss.backward()
                opt.step()
                n_samples += 1
                avg_loss += numpy.round(loss.cpu().data.numpy(), 3)

            end = time.time()
            avg_loss /= n_samples
            losses.append(avg_loss)
            logger.info(
                'Epoch {0}, Average Loss {1}, Minutes spent {2}'.format(
                    epoch + 1, round(avg_loss.data[0], 4),
                    round((end - start) / 60., 2)))

        Plotter.plot_training(epochs=epochs,
                              losses=losses,
                              embedding_dim=self.n_hidden,
                              output=output_dir)
Example #2
0
 def plot_behavior_payoff(self, results, imageFilePath):
     """Plots a graph that depicts the behavior and the
     payoff of the players/agents from 'results' to one
     or more image files, taking 'imageFilePath' as base name.
     """        
     plotter = Plotter(imageFilePath, self.image_file_types, "right")
     plotter.beginDoublePlot("Period", 
             "Percentage in total subject population", 
             "Payoffs in MUs", 
             (0, self.chronicles.world.maxRounds), 
             (0,100), (30,60))    
     data = PC([results[FREE_RIDERS], results[HIGH_CONTRIBUTORS]])
     plotter.barPlot([FREE_RIDERS, HIGH_CONTRIBUTORS], data, yaxis = "left")               
     plotter.linePlot([PAYOFF_HC], [results[PAYOFF_HC]], yaxis = "right")
     plotter.linePlot([PAYOFF_FR], [results[PAYOFF_FR]], yaxis = "right")
     plotter.endPlot()
Example #3
0
 def plot_agent_payoff(self, stats, imageFilePath, deviation = None):
     """Plots the agent statistics for either a single agent or for a group
     of agents."""
     plotter = Plotter(imageFilePath, self.image_file_types, "right")        
     plotter.beginDoublePlot("Period", 
             "Percentage total agent class", 
             "Payoffs in MUs", 
             (0, self.chronicles.world.maxRounds), 
             (0,100), None)
     data = PC([stats[AG_SI], stats[AG_SFI]])
     plotter.barPlot([AG_SI, AG_SFI], data, yaxis = "left")
     if deviation: 
         error = deviation[AG_PAYOFF]
     else:
         error = None
     plotter.linePlot([AG_PAYOFF], stats[AG_PAYOFF], error , "right")
     plotter.endPlot()
Example #4
0
 def plot_agent_contrib(self, stats, imageFilePath, deviation = None):
     """Plots the contributions, sanctions and received punishements
     or commendations of an agent."""
     plotter = Plotter(imageFilePath, self.image_file_types, "right")
     plotter.beginDoublePlot("Period",
             "Coercions received", 
             "Contribs/Sanctions in percent of endowment", 
             (0, self.chronicles.world.maxRounds), 
             None, (0,100))
     if deviation:
         error = [deviation[AG_PUNISH], deviation[AG_COMMEND]]
     else:
         error = None   
     plotter.barPlot([AG_PUNISH, AG_COMMEND],
                     [stats[AG_PUNISH], stats[AG_COMMEND]],
                     errorBars = None, yaxis = "left") # graph gets too confusiung if errorBars are plotted here
     if deviation:
         error = PC([deviation[AG_CONTRIB], deviation[AG_SANCT]])
     else:
         error = None
     plotter.linePlot([AG_CONTRIB, AG_SANCT], 
                      PC([stats[AG_CONTRIB], stats[AG_SANCT]]), 
                      errorBars = error, yaxis = "right")
     plotter.endPlot()
Example #5
0
 def plot_payoff_punishment(self, results, imageFilePath):
     """Plots a graph that depicts the payoff and number of punishers
     and non-punishers from the 'results' to one or more image files
     at 'imageFilePath'.
     """    
     plotter = Plotter(imageFilePath, self.image_file_types, "right",
                       condense_factor = 5)
     plotter.beginDoublePlot("Periods", 
             "Percentage of high contributers in SI", 
             "Payoffs in MUs", 
             (0, self.chronicles.world.maxRounds), 
             (0,100), (30,60))
     data = PC([results[PUNISH_HC], results[NO_PUNISH_HC]])
     plotter.barPlot([PUNISH_HC, NO_PUNISH_HC], data, yaxis = "left")        
     plotter.linePlot([PAYOFF_NOP_HC], [results[PAYOFF_NOP_HC]], yaxis = "right")            
     plotter.linePlot([PAYOFF_P_HC], [results[PAYOFF_P_HC]], yaxis = "right")
     plotter.endPlot()
Example #6
0
 def plot_choice_contrib(self, results, imageFilePath):
     """Plots a graph that depcits the institution choice and the
     contributions of the players/agents from 'results' to one
     or more image files, taking 'imageFilePath' as base name.
     """
     plotter = Plotter(imageFilePath, self.image_file_types, "right")
     plotter.beginDoublePlot("Period",
             "Percentage in total subject population", 
             "Contribution in percent of endowment", 
             (0, self.chronicles.world.maxRounds), 
             (0,100), (0,100))    
     data = PC([results[SI_MEMBERS], results[SFI_MEMBERS]])
     plotter.barPlot([SI_MEMBERS, SFI_MEMBERS], data, yaxis = "left")               
     plotter.linePlot([AV_CONTRIB_SI], PC([results[AV_CONTRIB_SI]]), yaxis = "right")
     plotter.linePlot([AV_CONTRIB_SFI], PC([results[AV_CONTRIB_SFI]]), yaxis = "right")
     plotter.endPlot()
Example #7
0
    test_is_signal = test_dataframe.loc[:, "event_id"] == 0
    new_masses = np.random.choice(masses, size=np.sum(~test_is_signal), replace=True)
#    new_masses = np.array(test_dataframe.loc[test_is_signal, "true_mass"].sample(n=np.sum(~test_is_signal), replace=True))
    test_dataframe.loc[~test_is_signal, "true_mass"] = new_masses

    training_variables = ["ldgTrkPtFrac", "deltaPhiBjetMet", "deltaPhiTauMet", "deltaPhiTauBjet",
                               "MET", "tauPt", "bjetPt", "TransverseMass", "true_mass"]
    preprocess_modes = ["MinMaxScale", "MinMaxScale", "MinMaxScale", "MinMaxScale",
                        "MinMaxScale", "MinMaxScale", "MinMaxScale", "MinMaxScale", "MinMaxScale"]

    if only_make_comparisons:
        dnns = ["model_for_even", "model_for_odds"]
        labels = ["even", "odd"]
        paths_to_dnn = ["./SavedModels/" + x for x in dnns]
        plotter = Plotter(test_dataframe, None)
        plotter.compareDnns(paths_to_dnn, training_variables, labels)
        sys.exit(0)


    # training_variables = ["ldgTrkPtFrac", "deltaPhiBjetMet", "deltaPhiTauMet", "deltaPhiTauBjet",
    #                            "MET", "tauPt", "bjetPt", "TransverseMass"]
    # preprocess_modes = ["MinMaxScale", "MinMaxScale", "MinMaxScale", "MinMaxScale",
    #                     "MinMaxScale", "MinMaxScale", "MinMaxScale", "MinMaxScale"]

    preprocessor = Preprocessor(training_variables, preprocess_modes)
    dataframe = preprocessor._clean_inputs(dataframe, ["ldgTrkPtFrac", "TransverseMass"], [0.0, 0.0], [1.0, None])
    test_dataframe = preprocessor._clean_inputs(test_dataframe, ["ldgTrkPtFrac", "TransverseMass"], [0.0, 0.0], [1.0, None])

    is_signal = (dataframe.loc[:, "event_id"] == 0)
    signal = dataframe.loc[is_signal, :]