Exemple #1
0
    def evaluate_other(self):
        """
        Evaluate performance of a trained network.
        This is tested on 20% of the data and will be stored in a text file.
        :return:
        """

        opt = torch.optim.RMSprop([{
            'params': self.model_NLL.bayesian_embedding,
            'lr': .001
        }])
        criterion = torch.nn.CrossEntropyLoss()

        percentage_accuracy_top1 = []

        for i, schedule in enumerate(self.schedule_array):
            if i < .8 * len(self.schedule_array):
                continue
            load_in_embedding(self.model, self.embedding_list, i)
            prediction_accuracy = 0
            for count in range(schedule[0], schedule[1] + 1):

                input_nn = self.X[count]
                truth_nn = self.Y[count]

                if torch.cuda.is_available():
                    input_nn = Variable(
                        torch.Tensor(np.asarray(input_nn).reshape(1,
                                                                  242)).cuda())
                    truth = Variable(
                        torch.Tensor(
                            np.asarray(truth_nn).reshape(1)).cuda().long())
                else:
                    input_nn = Variable(
                        torch.Tensor(np.asarray(input_nn).reshape(1, 242)))
                    truth = Variable(
                        torch.Tensor(np.asarray(truth_nn).reshape(1)))

                opt.zero_grad()
                output_nn = self.model_NLL.forward(input_nn)
                loss_nn = criterion(output_nn, truth)
                loss_nn.backward()
                opt.step()

                index = torch.argmax(output_nn).item()

                if index == truth.item():
                    prediction_accuracy += 1

            print('Prediction Accuracy: top1: ', prediction_accuracy / 20)
            store_embedding_back(self.model_NLL, self.embedding_list, i)
            print('schedule num:', i)
            percentage_accuracy_top1.append(prediction_accuracy / 20)
            self.save_performance_results(percentage_accuracy_top1)
        print(np.mean(percentage_accuracy_top1))
Exemple #2
0
    def train(self):
        """
        Trains BDT.
        Randomly samples a schedule and timestep within that schedule, produces training data using x_i - x_j
        and trains upon that.
        :return:
        """

        training_done = False
        cv_cutoff = .8 * len(self.start_of_each_set_twenty)
        loss_func = AlphaLoss()

        # variables to keep track of loss and number of tasks trained over
        running_loss_predict_tasks = 0
        num_iterations_predict_task = 0

        while not training_done:
            # sample a timestep before the cutoff for cross_validation
            rand_timestep_within_sched = np.random.randint(cv_cutoff)
            set_of_twenty = self.start_of_each_set_twenty[
                rand_timestep_within_sched]

            # truth is the same as the task chosen
            # NOTE: if initial truth > 10, it is biggest task first. Else it is smallest task first. Approximate way to keep track of
            # the current type of schedule
            truth = self.Y[set_of_twenty]

            # get the current schedule that the timestep is from
            which_schedule = find_which_schedule_this_belongs_to(
                self.schedule_array, set_of_twenty)

            # load in the embedding from the list into the network
            load_in_embedding(self.model, self.embedding_list, which_schedule)

            # find feature vector of true action taken
            phi_i_num = truth + set_of_twenty
            phi_i = self.X[phi_i_num]
            phi_i_numpy = np.asarray(phi_i)

            # iterate over pairwise comparisons
            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:  # if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_i_numpy - phi_j_numpy

                    # transform into torch variables
                    if torch.cuda.is_available():
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)).cuda())
                        P = Variable(
                            torch.Tensor([
                                1 - self.distribution_epsilon,
                                self.distribution_epsilon
                            ]).cuda())
                    else:
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)))
                        P = Variable(
                            torch.Tensor([
                                1 - self.distribution_epsilon,
                                self.distribution_epsilon
                            ]))

                    # get model output
                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)

                    # Updates embeddings and optimizer after 5000 passes through the data
                    if self.total_iterations > 5000:
                        self.opt.zero_grad()
                        self.embedding_optimizer.zero_grad()
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()
                        self.embedding_optimizer.step()
                    else:
                        self.opt.zero_grad()
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()

                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_j_numpy - phi_i_numpy

                    if torch.cuda.is_available():
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)).cuda())
                        P = Variable(
                            torch.Tensor([
                                self.distribution_epsilon,
                                1 - self.distribution_epsilon
                            ]).cuda())
                    else:
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)))
                        P = Variable(
                            torch.Tensor([
                                self.distribution_epsilon,
                                1 - self.distribution_epsilon
                            ]))

                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)

                    # Updates embeddings and optimizer after 5000 passes through the data
                    if self.total_iterations > 5000:
                        self.opt.zero_grad()
                        self.embedding_optimizer.zero_grad()
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()
                        self.embedding_optimizer.step()
                    else:
                        self.opt.zero_grad()
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()

                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

            # save the embedding back into the list
            self.embedding_list = store_embedding_back(self.model,
                                                       self.embedding_list,
                                                       which_schedule)
            # add average accuracy across pairwise comparisons to array
            self.total_loss_array.append(running_loss_predict_tasks /
                                         num_iterations_predict_task)
            num_iterations_predict_task = 0
            running_loss_predict_tasks = 0

            self.total_iterations += 1

            if self.total_iterations > 25 and self.total_iterations % 50 == 1:
                print('total iterations is', self.total_iterations)
                print('total loss (average for each 40, averaged)',
                      np.mean(self.total_loss_array[-40:]))

            if self.total_iterations > 0 and self.total_iterations % self.when_to_save == self.when_to_save - 1:
                print(self.embedding_list)
                self.save_trained_nets()

            if self.total_iterations > 8000 and np.mean(
                    self.total_loss_array[-100:]) - np.mean(
                        self.total_loss_array[-500:]) < self.covergence_epsilon:
                training_done = True
Exemple #3
0
    def evaluate(self, load_in_model=False):
        """
        Evaluate performance of a trained network tuned upon the alpha divergence loss.
        This is tested on 20% of the data and will be stored in a text file.
        Note this function is called after training convergence
        :return:
        """
        # define new optimizer that only optimizes gradient
        loss_func = AlphaLoss()
        checkpoint = self.model.state_dict().copy()
        embedding_list_copy = self.embedding_list.copy()
        prediction_accuracy = [0, 0]
        percentage_accuracy_top1 = []
        percentage_accuracy_top3 = []

        if load_in_model:
            self.model.load_state_dict(
                torch.load(
                    '/home/ghost/PycharmProjects/bayesian_prolo/saved_models/pairwise_saved_models/model_baydimtest_'
                    + str(self.bayesian_embedding_dim) +
                    '.tar')['nn_state_dict'])

        # step through the cv set
        for j in range(int(self.num_schedules * .8), self.num_schedules):
            optimizer_for_embedding = self.opt = torch.optim.SGD([{
                'params':
                self.model.bayesian_embedding,
                'lr':
                .9
            }])
            load_in_embedding(self.model, self.embedding_list, j)
            schedule_bounds = self.schedule_array[j]
            step = schedule_bounds[
                0]  # starting index of schedule within the data

            # until you complete schedule 1
            while step < schedule_bounds[1]:
                probability_matrix = np.zeros((20, 20))

                for m, counter in enumerate(range(step, step + 20)):
                    phi_i = self.X[counter]
                    phi_i_numpy = np.asarray(phi_i)

                    # for each set of twenty
                    for n, second_counter in enumerate(range(step, step + 20)):
                        # fill entire array with diagnols set to zero
                        if second_counter == counter:  # same as m = n
                            continue
                        phi_j = self.X[second_counter]
                        phi_j_numpy = np.asarray(phi_j)

                        feature_input = phi_i_numpy - phi_j_numpy

                        if torch.cuda.is_available():
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1,
                                                                   13)).cuda())

                        else:
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1, 13)))

                        # push through nets
                        preference_prob = self.model.forward(feature_input)
                        probability_matrix[m][n] = preference_prob[
                            0].data.detach()[0].item()
                        probability_matrix[n][m] = preference_prob[
                            0].data.detach()[1].item()

                # Set of twenty is completed
                column_vec = np.sum(probability_matrix, axis=1)

                # top 1
                choice = np.argmax(column_vec)

                # top 3
                _, top_three = torch.topk(torch.Tensor(column_vec), 3)

                # Then do training update loop
                truth = self.Y[step]

                # index top 1
                if choice == truth:
                    prediction_accuracy[0] += 1

                # index top 3
                if truth in top_three:
                    prediction_accuracy[1] += 1

                # forward
                phi_i_num = truth + step  # old method: set_of_twenty[0] + truth
                phi_i = self.X[phi_i_num]
                phi_i_numpy = np.asarray(phi_i)
                # iterate over pairwise comparisons
                if choice == truth:
                    pass
                else:
                    for counter in range(step, step + 20):
                        if counter == phi_i_num:  # if counter == phi_i_num:
                            continue
                        else:
                            phi_j = self.X[counter]
                            phi_j_numpy = np.asarray(phi_j)
                            feature_input = phi_i_numpy - phi_j_numpy

                            # label = add_noise_pairwise(label, self.noise_percentage)
                            if torch.cuda.is_available():
                                feature_input = Variable(
                                    torch.Tensor(feature_input.reshape(
                                        1, 13)).cuda())
                                P = Variable(
                                    torch.Tensor([
                                        1 - self.distribution_epsilon,
                                        self.distribution_epsilon
                                    ]).cuda())
                            else:
                                feature_input = Variable(
                                    torch.Tensor(feature_input.reshape(1, 13)))
                                P = Variable(
                                    torch.Tensor([
                                        1 - self.distribution_epsilon,
                                        self.distribution_epsilon
                                    ]))

                            output = self.model(feature_input)
                            loss = loss_func.forward(P, output, self.alpha)
                            # update till convergence, or under 50 iterations
                            if loss.item() > .005:
                                flag = False
                                tracker = 0
                                while not flag:
                                    optimizer_for_embedding.zero_grad()
                                    loss.backward()
                                    torch.nn.utils.clip_grad_norm_(
                                        self.model.parameters(), 0.5)
                                    optimizer_for_embedding.step()
                                    output = self.model(feature_input)
                                    loss = loss_func.forward(
                                        P, output, self.alpha)
                                    tracker += 1
                                    if tracker > 50:
                                        flag = True
                                    if loss.item() < .005:
                                        flag = True

                            else:
                                pass

                    for counter in range(step, step + 20):
                        if counter == phi_i_num:
                            continue
                        else:
                            phi_j = self.X[counter]
                            phi_j_numpy = np.asarray(phi_j)
                            feature_input = phi_j_numpy - phi_i_numpy

                            if torch.cuda.is_available():
                                feature_input = Variable(
                                    torch.Tensor(feature_input.reshape(
                                        1, 13)).cuda())
                                P = Variable(
                                    torch.Tensor([
                                        self.distribution_epsilon,
                                        1 - self.distribution_epsilon
                                    ]).cuda())
                            else:
                                feature_input = Variable(
                                    torch.Tensor(feature_input.reshape(1, 13)))
                                P = Variable(
                                    torch.Tensor([
                                        self.distribution_epsilon,
                                        1 - self.distribution_epsilon
                                    ]))

                            output = self.model(feature_input)
                            loss = loss_func.forward(P, output, self.alpha)

                            # update till convergence, or under 50 iterations
                            if loss.item() > .005:
                                flag = False
                                tracker = 0
                                while not flag:
                                    optimizer_for_embedding.zero_grad()
                                    loss.backward()
                                    torch.nn.utils.clip_grad_norm_(
                                        self.model.parameters(), 0.5)
                                    optimizer_for_embedding.step()
                                    output = self.model(feature_input)
                                    loss = loss_func.forward(
                                        P, output, self.alpha)
                                    tracker += 1
                                    if tracker > 50:
                                        flag = True
                                    if loss.item() < .005:
                                        flag = True

                            else:
                                pass

                # add average loss to array
                self.embedding_list = store_embedding_back(
                    self.model, self.embedding_list, j)
                step += 20

            # schedule finished
            print('Prediction Accuracy: top1: ', prediction_accuracy[0] / 20,
                  ' top3: ', prediction_accuracy[1] / 20)

            print('schedule num:', j)
            percentage_accuracy_top1.append(prediction_accuracy[0] / 20)
            percentage_accuracy_top3.append(prediction_accuracy[1] / 20)

            prediction_accuracy = [0, 0]
        self.save_performance_results(percentage_accuracy_top1,
                                      percentage_accuracy_top3)
        self.model.load_state_dict(checkpoint)
        self.embedding_list = embedding_list_copy
Exemple #4
0
    def evaluate_alpha(self):
        """
        Evaluate performance of a trained network.
        This is tested on 20% of the data and will be stored in a text file.
        :return:
        """

        opt = torch.optim.RMSprop([{
            'params': self.model.bayesian_embedding,
            'lr': .001
        }])
        loss_func = AlphaLoss()

        percentage_accuracy_top1 = []

        for i, schedule in enumerate(self.schedule_array):
            if i < .8 * len(self.schedule_array):
                continue
            load_in_embedding(self.model, self.embedding_list, i)
            prediction_accuracy = 0
            for count in range(schedule[0], schedule[1] + 1):

                input_nn = self.X[count]
                truth_nn = self.Y[count]

                # if torch.cuda.is_available():
                #     input_nn = Variable(torch.Tensor(np.asarray(net_input).reshape(1, 242)).cuda())
                #     truth = Variable(torch.Tensor(np.asarray(truth).reshape(1)).cuda().long())
                # else:
                #     input_nn = Variable(torch.Tensor(np.asarray(net_input).reshape(1, 242)))
                #     truth = Variable(torch.Tensor(np.asarray(truth).reshape(1)))

                # iterate over pairwise comparisons
                if torch.cuda.is_available():
                    input_nn = Variable(
                        torch.Tensor(np.asarray(input_nn).reshape(
                            1,
                            242)).cuda())  # change to 5 to increase batch size
                    P = Variable(torch.Tensor(np.ones((1, 20)))).cuda()
                    P *= self.distribution_epsilon
                    P[0][truth_nn] = 1 - 19 * self.distribution_epsilon
                    truth = Variable(
                        torch.Tensor(
                            np.asarray(truth_nn).reshape(1)).cuda().long())
                else:
                    input_nn = Variable(
                        torch.Tensor(np.asarray(input_nn).reshape(1, 242)))
                    P = Variable(
                        torch.Tensor(
                            np.ones((1, 20) * self.distribution_epsilon)))
                    P[0][truth_nn] = 1 - 19 * self.distribution_epsilon
                    truth = Variable(
                        torch.Tensor(np.asarray(truth_nn).reshape(1)).long())

                opt.zero_grad()
                output = self.model.forward(input_nn)
                loss = loss_func.forward(P, output, self.alpha)
                loss.backward()
                opt.step()

                index = torch.argmax(output).item()

                if index == truth.item():
                    prediction_accuracy += 1

            print('Prediction Accuracy: top1: ', prediction_accuracy / 20)
            store_embedding_back(self.model, self.embedding_list, i)
            print('schedule num:', i)
            percentage_accuracy_top1.append(prediction_accuracy / 20)
            self.save_performance_results(percentage_accuracy_top1)
        print(np.mean(percentage_accuracy_top1))
Exemple #5
0
    def train(self):
        """
        Trains BDT.
        Randomly samples a schedule and timestep within that schedule, produces training data using x_i - x_j
        and trains upon that.
        :return:
        """

        threshold = .1
        training_done = False
        loss_func = AlphaLoss()

        # deepening data
        deepen_data = {'samples': [], 'labels': [], 'embedding_indices': []}

        # variables to keep track of loss and number of tasks trained over
        running_loss_predict_tasks = 0
        num_iterations_predict_task = 0

        while not training_done:
            # sample a timestep before the cutoff for cross_validation
            rand_timestep_within_sched = np.random.randint(
                len(self.start_of_each_set_twenty))
            set_of_twenty = self.start_of_each_set_twenty[
                rand_timestep_within_sched]
            truth = self.Y[set_of_twenty]

            which_schedule = find_which_schedule_this_belongs_to(
                self.schedule_array, set_of_twenty)
            load_in_embedding(self.model, self.embedding_list, which_schedule)

            # find feature vector of true action taken
            phi_i_num = truth + set_of_twenty
            phi_i = self.X[phi_i_num]
            phi_i_numpy = np.asarray(phi_i)

            # iterate over pairwise comparisons
            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:  # if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_i_numpy - phi_j_numpy
                    deepen_data['samples'].append(np.array(feature_input))
                    # label = add_noise_pairwise(label, self.noise_percentage)
                    if torch.cuda.is_available():
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)).cuda())
                        P = Variable(
                            torch.Tensor([
                                1 - self.distribution_epsilon,
                                self.distribution_epsilon
                            ]).cuda())
                    else:
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)))
                        P = Variable(
                            torch.Tensor([
                                1 - self.distribution_epsilon,
                                self.distribution_epsilon
                            ]))

                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)

                    # NAN check (fix is in the bnn file)
                    if torch.isnan(loss):
                        print(self.alpha, ' :nan occurred at iteration ',
                              self.total_iterations)

                    # prepare optimizer, compute gradient, update params
                    self.opt.zero_grad()
                    if loss.item() < .001 or loss.item() > 50:
                        pass
                    else:
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()

                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

                    deepen_data['labels'].extend([0])
                    deepen_data['embedding_indices'].extend([which_schedule])

            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_j_numpy - phi_i_numpy
                    deepen_data['samples'].append(np.array(feature_input))
                    if torch.cuda.is_available():
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)).cuda())
                        P = Variable(
                            torch.Tensor([
                                self.distribution_epsilon,
                                1 - self.distribution_epsilon
                            ]).cuda())
                    else:
                        feature_input = Variable(
                            torch.Tensor(feature_input.reshape(1, 13)))
                        P = Variable(
                            torch.Tensor([
                                self.distribution_epsilon,
                                1 - self.distribution_epsilon
                            ]))

                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)
                    # if num_iterations_predict_task % 5 == 0:
                    #     print('loss is :', loss.item())
                    # clip any very high gradients

                    # prepare optimizer, compute gradient, update params
                    self.opt.zero_grad()
                    if loss.item() < .001 or loss.item() > 50:
                        pass
                    else:
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()

                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

                    deepen_data['labels'].extend([1])
                    deepen_data['embedding_indices'].extend([which_schedule])

            # add average loss to array
            # print(list(self.model.parameters()))

            self.embedding_list = store_embedding_back(self.model,
                                                       self.embedding_list,
                                                       which_schedule)
            self.total_loss_array.append(running_loss_predict_tasks /
                                         num_iterations_predict_task)
            num_iterations_predict_task = 0
            running_loss_predict_tasks = 0

            self.total_iterations += 1

            if self.total_iterations > 25 and self.total_iterations % 50 == 1:
                print('total iterations is', self.total_iterations)
                print('total loss (average for each 40, averaged)',
                      np.mean(self.total_loss_array[-40:]))

            if self.total_iterations > 0 and self.total_iterations % self.when_to_save == self.when_to_save - 1:
                self.save_trained_nets('BDDT' + str(self.num_schedules))
                threshold -= .025

            # if self.total_iterations % 500 == 499:
            #     self.model = deepen_with_embeddings(self.model, deepen_data, self.embedding_list, max_depth=self.max_depth, threshold=threshold)
            #     params = list(self.model.parameters())
            #     del params[0]
            #     self.opt = torch.optim.RMSprop([{'params': params}, {'params': self.model.bayesian_embedding, 'lr': .001}])
            #     deepen_data = {
            #         'samples': [],
            #         'labels': [],
            #         'embedding_indices': []
            #   }

            if self.total_iterations > 5000 and np.mean(
                    self.total_loss_array[-100:]) - np.mean(
                        self.total_loss_array[-500:]) < self.covergence_epsilon:
                training_done = True
Exemple #6
0
    def train(self):
        """
        Trains BDT.
        Randomly samples a schedule and timestep within that schedule, and passes in the corresponding data in an attempt to classify which task was scheduled
        :return:
        """
        criterion = torch.nn.CrossEntropyLoss()
        loss_func = AlphaLoss()
        training_done = False
        cv_cutoff = .8 * len(self.X)

        while not training_done:
            # sample a timestep before the cutoff for cross_validation
            rand_timestep_within_sched = np.random.randint(cv_cutoff)
            input_nn = self.X[rand_timestep_within_sched]
            truth_nn = self.Y[rand_timestep_within_sched]

            which_schedule = self.find_which_schedule_this_belongs_to(
                rand_timestep_within_sched)
            load_in_embedding(self.model, self.embedding_list, which_schedule)
            load_in_embedding(self.model_NLL, self.embedding_list_NLL,
                              which_schedule)

            # iterate over pairwise comparisons
            if torch.cuda.is_available():
                input_nn = Variable(
                    torch.Tensor(np.asarray(input_nn).reshape(
                        1, 242)).cuda())  # change to 5 to increase batch size
                P = Variable(torch.Tensor(np.ones((1, 20)))).cuda()
                P *= self.distribution_epsilon
                P[0][truth_nn] = 1 - 19 * self.distribution_epsilon
                truth = Variable(
                    torch.Tensor(
                        np.asarray(truth_nn).reshape(1)).cuda().long())
            else:
                input_nn = Variable(
                    torch.Tensor(np.asarray(input_nn).reshape(1, 242)))
                P = Variable(
                    torch.Tensor(np.ones((1, 20) * self.distribution_epsilon)))
                P[0][truth_nn] = 1 - 19 * self.distribution_epsilon
                truth = Variable(
                    torch.Tensor(np.asarray(truth_nn).reshape(1)).long())

            self.opt.zero_grad()
            output = self.model.forward(input_nn)
            loss = loss_func.forward(P, output, self.alpha)
            loss.backward()
            self.opt.step()

            self.opt2.zero_grad()
            output_nn = self.model_NLL.forward(input_nn)
            loss_nn = criterion(output_nn, truth)
            loss_nn.backward()
            self.opt2.step()

            self.total_loss_array.append(loss.item())
            store_embedding_back(self.model, self.embedding_list,
                                 which_schedule)
            store_embedding_back(self.model_NLL, self.embedding_list_NLL,
                                 which_schedule)
            total_iterations = len(self.total_loss_array)

            if total_iterations % 50 == 49:
                print('loss at', total_iterations, 'is', loss.item())
                print('loss_NN at', total_iterations, 'is', loss_nn.item())

            if total_iterations > 100000:
                training_done = True
Exemple #7
0
    def evaluate_on_test_data(self, load_in_model=False):
        """
        Evaluate performance of a trained network tuned upon the alpha divergence loss.

        Note this function is called after training convergence
        :return:
        """
        # define new optimizer that only optimizes gradient
        num_schedules = 75
        loss_func = AlphaLoss()
        load_directory = '/home/ghost/PycharmProjects/bayesian_prolo/scheduling_env/datasets/test/' + str(
            num_schedules) + '_inf_hetero_deadline_pairwise.pkl'

        data = pickle.load(open(load_directory, "rb"))
        X, Y, schedule_array = create_new_data(num_schedules, data)
        start_of_each_set_twenty = create_sets_of_20_from_x_for_pairwise_comparisions(
            X)

        embedding_optimizer = torch.optim.RMSprop([{
            'params': self.model.bayesian_embedding,
            'lr': .001
        }])
        embedding_list = [torch.ones(8) * 1 / 3 for _ in range(num_schedules)]

        prediction_accuracy = [0, 0]
        percentage_accuracy_top1 = []
        percentage_accuracy_top3 = []

        if load_in_model:
            self.model.load_state_dict(
                torch.load(
                    '/home/ghost/PycharmProjects/bayesian_prolo/saved_models/pairwise_saved_models/model_homog.tar'
                )['nn_state_dict'])

        for j in range(0, num_schedules):
            schedule_bounds = schedule_array[j]
            step = schedule_bounds[0]
            load_in_embedding(self.model, embedding_list, j)
            while step < schedule_bounds[1]:
                probability_matrix = np.zeros((20, 20))

                for m, counter in enumerate(range(step, step + 20)):
                    phi_i = X[counter]
                    phi_i_numpy = np.asarray(phi_i)

                    # for each set of twenty
                    for n, second_counter in enumerate(range(step, step + 20)):
                        # fill entire array with diagnols set to zero
                        if second_counter == counter:  # same as m = n
                            continue
                        phi_j = X[second_counter]
                        phi_j_numpy = np.asarray(phi_j)

                        feature_input = phi_i_numpy - phi_j_numpy

                        if torch.cuda.is_available():
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1,
                                                                   13)).cuda())

                        else:
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1, 13)))

                        # push through nets
                        preference_prob = self.model.forward(feature_input)
                        probability_matrix[m][n] = preference_prob[
                            0].data.detach()[0].item()
                        # probability_matrix[n][m] = preference_prob[0].data.detach()[1].item()

                # Set of twenty is completed
                column_vec = np.sum(probability_matrix, axis=1)

                # top 1
                choice = np.argmax(column_vec)

                # top 3
                _, top_three = torch.topk(torch.Tensor(column_vec), 3)

                # Then do training update loop
                truth = Y[step]

                # index top 1
                if choice == truth:
                    prediction_accuracy[0] += 1

                # index top 3
                if truth in top_three:
                    prediction_accuracy[1] += 1

                # forward
                phi_i_num = truth + step  # old method: set_of_twenty[0] + truth
                phi_i = X[phi_i_num]
                phi_i_numpy = np.asarray(phi_i)
                # iterate over pairwise comparisons
                for counter in range(step, step + 20):
                    if counter == phi_i_num:  # if counter == phi_i_num:
                        continue
                    else:
                        phi_j = X[counter]
                        phi_j_numpy = np.asarray(phi_j)
                        feature_input = phi_i_numpy - phi_j_numpy

                        # label = add_noise_pairwise(label, self.noise_percentage)
                        if torch.cuda.is_available():
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1,
                                                                   13)).cuda())
                            P = Variable(
                                torch.Tensor([
                                    1 - self.distribution_epsilon,
                                    self.distribution_epsilon
                                ]).cuda())
                        else:
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1, 13)))
                            P = Variable(
                                torch.Tensor([
                                    1 - self.distribution_epsilon,
                                    self.distribution_epsilon
                                ]))

                        output = self.model(feature_input)
                        loss = loss_func.forward(P, output, self.alpha)
                        # prepare optimizer, compute gradient, update params
                        if loss.item() < .001 or loss.item() > 50:
                            pass
                        else:
                            embedding_optimizer.zero_grad()
                            if loss.item() < .001 or loss.item() > 50:
                                pass
                            else:
                                loss.backward()
                                torch.nn.utils.clip_grad_norm_(
                                    self.model.parameters(), 0.5)
                                embedding_optimizer.step()

                for counter in range(step, step + 20):
                    if counter == phi_i_num:
                        continue
                    else:
                        phi_j = X[counter]
                        phi_j_numpy = np.asarray(phi_j)
                        feature_input = phi_j_numpy - phi_i_numpy

                        if torch.cuda.is_available():
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1,
                                                                   13)).cuda())
                            P = Variable(
                                torch.Tensor([
                                    self.distribution_epsilon,
                                    1 - self.distribution_epsilon
                                ]).cuda())
                        else:
                            feature_input = Variable(
                                torch.Tensor(feature_input.reshape(1, 13)))
                            P = Variable(
                                torch.Tensor([
                                    self.distribution_epsilon,
                                    1 - self.distribution_epsilon
                                ]))

                        output = self.model(feature_input)
                        loss = loss_func.forward(P, output, self.alpha)
                        # print('loss is :', loss.item())
                        # clip any very high gradients

                        # prepare optimizer, compute gradient, update params
                        if loss.item() < .001 or loss.item() > 50:
                            pass
                        else:
                            embedding_optimizer.zero_grad()
                            loss.backward()
                            torch.nn.utils.clip_grad_norm_(
                                self.model.parameters(), 0.5)
                            embedding_optimizer.step()

                # add average loss to array
                embedding_list = store_embedding_back(self.model,
                                                      embedding_list, j)
                step += 20

            # schedule finished
            print('Prediction Accuracy: top1: ', prediction_accuracy[0] / 20,
                  ' top3: ', prediction_accuracy[1] / 20)

            print('schedule num:', j)
            percentage_accuracy_top1.append(prediction_accuracy[0] / 20)
            percentage_accuracy_top3.append(prediction_accuracy[1] / 20)

            prediction_accuracy = [0, 0]
        self.save_performance_results(percentage_accuracy_top1,
                                      percentage_accuracy_top3,
                                      'inf_BDT' + str(self.num_schedules))
Exemple #8
0
    def train(self):
        """
        Trains BDT.
        Randomly samples a schedule and timestep within that schedule, produces training data using x_i - x_j
        and trains upon that.
        :return:
        """

        threshold = .1
        training_done = False
        loss_func = AlphaLoss()

        # variables to keep track of loss and number of tasks trained over
        running_loss_predict_tasks = 0
        num_iterations_predict_task = 0

        while not training_done:
            # sample a timestep before the cutoff for cross_validation
            rand_timestep_within_sched = np.random.randint(
                len(self.start_of_each_set_twenty))
            set_of_twenty = self.start_of_each_set_twenty[
                rand_timestep_within_sched]
            truth = self.Y[set_of_twenty]

            which_schedule = find_which_schedule_this_belongs_to(
                self.schedule_array, set_of_twenty)
            load_in_embedding(self.model, self.embedding_list, which_schedule)

            # find feature vector of true action taken
            phi_i_num = truth + set_of_twenty
            phi_i = self.X[phi_i_num]
            phi_i_numpy = np.asarray(phi_i)

            # iterate over pairwise comparisons
            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:  # if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_i_numpy - phi_j_numpy

                    # label = add_noise_pairwise(label, self.noise_percentage)
                    feature_input, P = transform_into_torch_vars(
                        feature_input, self.distribution_epsilon, True,
                        torch.cuda.is_available())
                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)

                    # NAN check (fix is in the bnn file)
                    if torch.isnan(loss):
                        print(self.alpha, ' :nan occurred at iteration ',
                              self.total_iterations)

                    # prepare optimizer, compute gradient, update params
                    self.opt.zero_grad()
                    self.embedding_optimizer.zero_grad()
                    if loss.item() < .001 or loss.item() > 50:
                        pass
                    else:
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()
                        self.embedding_optimizer.step()

                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_j_numpy - phi_i_numpy
                    feature_input, P = transform_into_torch_vars(
                        feature_input, self.distribution_epsilon, False,
                        torch.cuda.is_available())
                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)
                    # if num_iterations_predict_task % 5 == 0:
                    #     print('loss is :', loss.item())
                    # clip any very high gradients

                    # prepare optimizer, compute gradient, update params

                    self.opt.zero_grad()
                    self.embedding_optimizer.zero_grad()

                    if loss.item() < .001 or loss.item() > 50:
                        pass
                    else:
                        loss.backward()
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                       0.5)
                        self.opt.step()
                        self.embedding_optimizer.step()

                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

            self.embedding_list = store_embedding_back(self.model,
                                                       self.embedding_list,
                                                       which_schedule)
            self.total_loss_array.append(running_loss_predict_tasks /
                                         num_iterations_predict_task)
            num_iterations_predict_task = 0
            running_loss_predict_tasks = 0

            self.total_iterations += 1

            if self.total_iterations > 25 and self.total_iterations % 50 == 1:
                print('total iterations is', self.total_iterations)
                print('total loss (average for each 40, averaged)',
                      np.mean(self.total_loss_array[-40:]))

            if self.total_iterations > 0 and self.total_iterations % self.when_to_save == self.when_to_save - 1:
                self.save_trained_nets('BDDT' + str(self.num_schedules))
                self.evaluate_on_test_data()

            if self.total_iterations > 5000 and np.mean(
                    self.total_loss_array[-100:]) - np.mean(
                        self.total_loss_array[-500:]) < self.covergence_epsilon:
                training_done = True
Exemple #9
0
    def train(self):
        """
        Trains BDT.
        Randomly samples a schedule and timestep within that schedule, produces training data using x_i - x_j
        and trains upon that.
        :return:
        """
        # loss = nn.CrossEntropyLoss()

        training_done = False
        cv_cutoff = int(.8 * len(self.start_of_each_set_twenty))
        loss_func = AlphaLoss()

        # variables to keep track of loss and number of tasks trained over
        running_loss_predict_tasks = 0
        num_iterations_predict_task = 0
        while not training_done:
            # sample a timestep before the cutoff for cross_validation
            rand_timestep_within_sched = np.random.randint(cv_cutoff)
            set_of_twenty = self.start_of_each_set_twenty[rand_timestep_within_sched]
            truth = self.Y[set_of_twenty]

            which_schedule = self.find_which_schedule_this_belongs_to(set_of_twenty)
            load_in_embedding(self.model, self.embedding_list, which_schedule)

            # find feature vector of true action taken
            phi_i_num = truth + set_of_twenty
            phi_i = self.X[phi_i_num]
            phi_i_numpy = np.asarray(phi_i)

            # iterate over pairwise comparisons
            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:  # if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_i_numpy - phi_j_numpy

                    # label = add_noise_pairwise(label, self.noise_percentage)
                    if torch.cuda.is_available():
                        feature_input = Variable(torch.Tensor(feature_input.reshape(1, 12)).cuda())
                        P = Variable(torch.Tensor([1 - self.distribution_epsilon, self.distribution_epsilon]).cuda())
                    else:
                        feature_input = Variable(torch.Tensor(feature_input.reshape(1, 12)))
                        P = Variable(torch.Tensor([1 - self.distribution_epsilon, self.distribution_epsilon]))

                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)
                    if torch.isnan(loss):
                        print(self.alpha, ' :nan occurred at iteration ', self.total_iterations)
                        return
                        # TODO: can prob just set training to true here if network still outputs proper stuff
                    # prepare optimizer, compute gradient, update params
                    self.opt.zero_grad()
                    loss.backward()
                    torch.nn.utils.clip_grad_norm_(self.model.parameters(), 0.5)
                    self.opt.step()

                    running_loss_predict_tasks += loss.item()
                    print('after ', num_iterations_predict_task, ' the embedding has changed to :', self.model.state_dict()['bayesian_embedding'])
                    num_iterations_predict_task += 1

            for counter in range(set_of_twenty, set_of_twenty + 20):
                if counter == phi_i_num:
                    continue
                else:
                    phi_j = self.X[counter]
                    phi_j_numpy = np.asarray(phi_j)
                    feature_input = phi_j_numpy - phi_i_numpy

                    if torch.cuda.is_available():
                        feature_input = Variable(torch.Tensor(feature_input.reshape(1, 12)).cuda())
                        P = Variable(torch.Tensor([self.distribution_epsilon, 1 - self.distribution_epsilon]).cuda())
                    else:
                        feature_input = Variable(torch.Tensor(feature_input.reshape(1, 12)))
                        P = Variable(torch.Tensor([self.distribution_epsilon, 1 - self.distribution_epsilon]))

                    output = self.model(feature_input)
                    loss = loss_func.forward(P, output, self.alpha)
                    # if num_iterations_predict_task % 5 == 0:
                    #     print('loss is :', loss.item())
                    # clip any very high gradients

                    # prepare optimizer, compute gradient, update params
                    self.opt.zero_grad()
                    loss.backward()
                    torch.nn.utils.clip_grad_norm_(self.model.parameters(), 0.5)
                    self.opt.step()
                    print('after ', num_iterations_predict_task, ' the embedding has changed to :', self.model.state_dict()['bayesian_embedding'])
                    running_loss_predict_tasks += loss.item()
                    num_iterations_predict_task += 1

            # add average loss to array
            # print(list(self.model.parameters()))
            store_embedding_back(self.model, self.embedding_list, which_schedule)
            self.total_loss_array.append(running_loss_predict_tasks / num_iterations_predict_task)
            num_iterations_predict_task = 0
            running_loss_predict_tasks = 0

            self.total_iterations += 1


            if self.total_iterations > 25 and self.total_iterations % 50 == 1:
                print('total iterations is', self.total_iterations)
                print('total loss (average for each 40, averaged)', np.mean(self.total_loss_array[-40:]))
                # TODO: change running loss to actual loss

            if self.total_iterations > 0 and self.total_iterations % self.when_to_save == self.when_to_save - 1:
                # self.plot_nn()
                print(self.embedding_list)
                self.save_trained_nets()
                # self.evaluate()

            if self.total_iterations > 10000 and np.mean(self.total_loss_array[-100:]) - np.mean(
                    self.total_loss_array[-500:]) < self.covergence_epsilon:
                training_done = True
Exemple #10
0
    def train(self):
        """
        Trains PDDT.
        :return:
        """

        threshold = .05
        training_done = False
        loss_func = AlphaLoss()

        # deepening data
        deepen_data = {'samples': [], 'labels': [], 'embedding_indices': []}

        while not training_done:
            # sample a timestep before the cutoff for cross_validation
            rand_timestep_within_sched = np.random.randint(len(self.X))
            input_nn = self.X[rand_timestep_within_sched]
            truth_nn = self.Y[rand_timestep_within_sched]

            which_schedule = find_which_schedule_this_belongs_to(
                self.schedule_array, rand_timestep_within_sched)
            load_in_embedding(self.model, self.embedding_list, which_schedule)

            deepen_data['samples'].append(np.array(input_nn))
            if torch.cuda.is_available():
                input_nn = Variable(
                    torch.Tensor(np.asarray(input_nn).reshape(
                        1, 242)).cuda())  # change to 5 to increase batch size
                P = Variable(torch.Tensor(np.ones((1, 20)))).cuda()
                P *= self.distribution_epsilon
                P[0][truth_nn] = 1 - 19 * self.distribution_epsilon
                truth = Variable(
                    torch.Tensor(
                        np.asarray(truth_nn).reshape(1)).cuda().long())
            else:
                input_nn = Variable(
                    torch.Tensor(np.asarray(input_nn).reshape(1, 242)))
                P = Variable(
                    torch.Tensor(np.ones((1, 20) * self.distribution_epsilon)))
                P[0][truth_nn] = 1 - 19 * self.distribution_epsilon
                truth = Variable(
                    torch.Tensor(np.asarray(truth_nn).reshape(1)).long())

            self.opt.zero_grad()
            output = self.model.forward(input_nn)
            loss = loss_func.forward(P, output, self.alpha)
            if loss.item() < .001 or loss.item() > 30:
                pass
            else:
                loss.backward()
                torch.nn.utils.clip_grad_norm_(self.model.parameters(), 0.5)
                self.opt.step()

            deepen_data['labels'].extend([truth.item()])
            deepen_data['embedding_indices'].extend([which_schedule])

            # add average loss to array
            # print(list(self.model.parameters()))

            self.embedding_list = store_embedding_back(self.model,
                                                       self.embedding_list,
                                                       which_schedule)

            self.total_loss_array.append(loss.item())
            self.total_iterations += 1

            if self.total_iterations > 25 and self.total_iterations % 50 == 1:
                print('total iterations is', self.total_iterations)
                print('total loss (average for each 40, averaged)',
                      np.mean(self.total_loss_array[-40:]))

            if self.total_iterations > 0 and self.total_iterations % self.when_to_save == self.when_to_save - 1:
                self.save_trained_nets('BDDT' + str(self.num_schedules))
                threshold -= .1

            if self.total_iterations % 500 == 499:
                self.model = deepen_with_embeddings(
                    self.model,
                    deepen_data,
                    self.embedding_list,
                    max_depth=self.max_depth,
                    threshold=threshold /
                    len(self.model.leaf_init_information))
                params = list(self.model.parameters())
                del params[0]
                self.opt = torch.optim.RMSprop([{
                    'params': params
                }, {
                    'params': self.model.bayesian_embedding,
                    'lr': .001
                }])
                deepen_data = {
                    'samples': [],
                    'labels': [],
                    'embedding_indices': []
                }

            if self.total_iterations > 5000 and np.mean(
                    self.total_loss_array[-100:]) - np.mean(
                        self.total_loss_array[-500:]) < self.covergence_epsilon:
                training_done = True
Exemple #11
0
    def evaluate_on_test_data(self, load_in_model=False):
        """
        Evaluate performance of a trained network tuned upon the alpha divergence loss.

        Note this function is called after training convergence
        :return:
        """
        # define new optimizer that only optimizes gradient
        num_schedules = 75
        loss_func = AlphaLoss()
        load_directory = '/home/ghost/PycharmProjects/bayesian_prolo/scheduling_env/datasets/test/' + str(
            num_schedules) + '_inf_hetero_deadline_naive.pkl'

        data = pickle.load(open(load_directory, "rb"))
        X, Y, schedule_array = create_new_dataset(num_schedules=num_schedules,
                                                  data=data)
        for i, each_element in enumerate(X):
            X[i] = each_element + list(range(20))

        embedding_optimizer = torch.optim.RMSprop([{
            'params': self.model.bayesian_embedding,
            'lr': .001
        }])
        embedding_list = [torch.ones(8) * 1 / 3 for _ in range(num_schedules)]

        prediction_accuracy = [0, 0]
        percentage_accuracy_top1 = []
        percentage_accuracy_top3 = []

        if load_in_model:
            self.model.load_state_dict(
                torch.load(
                    '/home/ghost/PycharmProjects/bayesian_prolo/saved_models/pairwise_saved_models/model_homog.tar'
                )['nn_state_dict'])

        for i, schedule in enumerate(schedule_array):
            load_in_embedding(self.model, embedding_list, i)
            for count in range(schedule[0], schedule[1] + 1):

                net_input = X[count]
                truth = Y[count]

                if torch.cuda.is_available():
                    input_nn = Variable(
                        torch.Tensor(np.asarray(net_input).reshape(
                            1,
                            242)).cuda())  # change to 5 to increase batch size
                    P = Variable(torch.Tensor(np.ones((1, 20)))).cuda()
                    P *= self.distribution_epsilon
                    P[0][truth] = 1 - 19 * self.distribution_epsilon
                    truth = Variable(
                        torch.Tensor(
                            np.asarray(truth).reshape(1)).cuda().long())
                else:
                    input_nn = Variable(
                        torch.Tensor(np.asarray(net_input).reshape(1, 242)))
                    P = Variable(
                        torch.Tensor(
                            np.ones((1, 20) * self.distribution_epsilon)))
                    P[0][truth] = 1 - 19 * self.distribution_epsilon
                    truth = Variable(
                        torch.Tensor(np.asarray(truth).reshape(1)).long())

                #####forward#####
                output = self.model.forward(input_nn)
                embedding_optimizer.zero_grad()
                loss = loss_func.forward(P, output, self.alpha)
                if loss.item() < .001 or loss.item() > 30:
                    pass
                else:
                    loss.backward()
                    torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                   0.5)
                    embedding_optimizer.step()

                index = torch.argmax(output).item()

                # top 3
                _, top_three = torch.topk(output, 3)

                if index == truth.item():
                    prediction_accuracy[0] += 1

                if truth.item() in top_three.detach().cpu().tolist()[0]:
                    prediction_accuracy[1] += 1

            # add average loss to array
            embedding_list = store_embedding_back(self.model, embedding_list,
                                                  i)

            # schedule finished
            print('Prediction Accuracy: top1: ', prediction_accuracy[0] / 20,
                  ' top3: ', prediction_accuracy[1] / 20)

            print('schedule num:', i)
            percentage_accuracy_top1.append(prediction_accuracy[0] / 20)
            percentage_accuracy_top3.append(prediction_accuracy[1] / 20)

            prediction_accuracy = [0, 0]
        self.save_performance_results(percentage_accuracy_top1,
                                      percentage_accuracy_top3,
                                      'inf_BDT' + str(self.num_schedules))