Example #1
0
    def train(self):
        self.check_data()
        self.init_paths()
        train_inputs, train_outputs = self.prepare_data(
            self.train_inputs, self.train_outputs)
        other_inputs, other_outputs = {}, {}
        for k in self.other_inputs:
            i, o = self.prepare_data(self.other_inputs[k],
                                     self.other_outputs[k])
            other_inputs[k] = i
            other_outputs[k] = o

        optimizer = optim.Adam(self.model.parameters(),
                               lr=self.learning_rate,
                               weight_decay=self.weight_decay)
        closure = self.make_closure(optimizer, train_inputs, train_outputs)

        # for i in tqdm(range(self.epochs)):
        for i in range(self.epochs):
            start_time_str = utils.now()
            start_time = time.time()

            train_loss = optimizer.step(closure)

            run_validate = i == 0 or (i + 1) % self.save_epochs == 0
            if run_validate:
                for name in other_inputs:
                    loss, accuracy = get_performance(model=self.model,
                                                     x=other_inputs[name],
                                                     y=other_outputs[name],
                                                     num_iters=self.num_iters,
                                                     no_grad=True)
                    self.other_losses[name].append(float(loss))
                    self.other_accuracies[name].append(accuracy)

            if (i + 1) % self.save_epochs == 0:
                filename = f"{self.filepath}/checkpoints/epoch_{i+1}.json"
                utils.save(filename, self)

            end_time_str = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
            end_time = time.time()
            runtime = end_time - start_time
            self.times.append({
                'start_time': start_time_str,
                'end_time': end_time_str,
                'runtime': runtime
            })

            log_performance('train', i, train_loss,
                            np.mean(self.train_accuracies[-1][:, -1]))
            if run_validate:
                for name in sorted(self.other_inputs):
                    log_performance(
                        name, i, self.other_losses[name][-1],
                        np.mean(self.other_accuracies[name][-1][:, -1]))

        filename = f"{self.filepath}/train_log.json"
        utils.save(filename, self)
Example #2
0
                                           x_grid=x_grid_batch,
                                           x_prob=x_prob_batch,
                                           y=y_batch,
                                           no_grad=False,
                                           num_iters=hp['num_iters'])
        loss.backward()
        total_loss += loss

    train_losses.append(float(total_loss))
    epoch_accuracies.append(accuracies)
    train_accuracies.append(np.concatenate(epoch_accuracies))
    return total_loss


for i in tqdm(range(hp['epochs'])):
    start_time_str = utils.now()
    start_time = time.time()

    train_loss = optimizer.step(closure)

    run_validate = i == 0 or (i + 1) % hp['valid_epochs'] == 0
    if run_validate:
        for name in other_x_grid:
            loss, accuracy = get_performance(model=model,
                                             x_grid=other_x_grid[name],
                                             x_prob=other_x_prob[name],
                                             y=other_y[name],
                                             num_iters=hp['num_iters'],
                                             no_grad=True)
            other_losses[name].append(float(loss))
            other_accuracies[name].append(accuracy)