Exemplo n.º 1
0
    def run_near(self, model, num_iter):

        train_config = {
            'lr': self.learning_rate,
            'neural_epochs': self.neural_epochs,
            'symbolic_epochs': self.symbolic_epochs,
            'optimizer': optim.Adam,
            'lossfxn': nn.CrossEntropyLoss(),  #todo
            'evalfxn': label_correctness,
            'num_labels': self.num_labels
        }

        # Initialize program graph starting from trained NN
        program_graph = ProgramGraph(model,
                                     DSL_DICT,
                                     CUSTOM_EDGE_COSTS,
                                     self.input_type,
                                     self.output_type,
                                     self.input_size,
                                     self.output_size,
                                     self.max_num_units,
                                     self.min_num_units,
                                     self.max_num_children,
                                     self.max_depth,
                                     self.penalty,
                                     ite_beta=self.ite_beta)

        # Initialize algorithm
        algorithm = ASTAR_NEAR(frontier_capacity=self.frontier_capacity)
        best_programs = algorithm.run(program_graph, self.batched_trainset,
                                      self.validset, train_config, self.device)

        if self.algorithm == "rnn":
            # special case for RNN baseline
            best_program = best_programs
        else:
            # Print all best programs found
            log_and_print("\n")
            log_and_print("BEST programs found:")
            for item in best_programs:
                print_program_dict(item)
            best_program = best_programs[-1]["program"]

        # Save best program
        self.program_path = os.path.join(self.save_path,
                                         "program_%d.p" % num_iter)
        pickle.dump(best_program, open(self.program_path, "wb"))
Exemplo n.º 2
0
    def neural_h(self):
        data = self.base_program.submodules
        l = [] #populate AST
        traverse(data,l)
        train_config = {
            'lr' : self.learning_rate,
            'neural_epochs' : 15,
            'symbolic_epochs' : self.symbolic_epochs,
            'optimizer' : optim.Adam,
            'lossfxn' : nn.CrossEntropyLoss(weight=self.loss_weight), #todo
            'evalfxn' : label_correctness,
            'num_labels' : self.num_labels
        }

        best_node_ind = 0
        best_score = 0
        for hole_node_ind in range(1,len(l)):

            hole_node = l[hole_node_ind]
            subprogram_str = print_program(hole_node[0])
            if subprogram_str.count('(') > self.max_depth:
                continue

            near_input_type = hole_node[0].input_type
            near_output_type = hole_node[0].output_type
            near_input_size = hole_node[0].input_size
            near_output_size = hole_node[0].output_size

            # Initialize program graph starting from trained NN
            program_graph = ProgramGraph(DSL_DICT, CUSTOM_EDGE_COSTS, near_input_type, near_output_type, near_input_size, near_output_size,
                self.max_num_units, self.min_num_units, self.max_num_children, 0, self.penalty, ite_beta=self.ite_beta) ## max_depth 0

            # Initialize algorithm
            algorithm = ASTAR_NEAR(frontier_capacity=0)
            score, new_prog, losses = algorithm.run_init(self.timestamp, self.base_program_name, hole_node_ind,
                program_graph, self.batched_trainset, self.validset, train_config, self.device)
            subprogram_str = print_program(hole_node[0])
            log_and_print("Subprogram to replace: %s"% subprogram_str)
            if score > best_score:
                best_node_ind = hole_node_ind
                best_score = score
                log_and_print("New best: RNN Heuristic score at Node %d: %f\n" %( hole_node_ind, score))
            else: 
                log_and_print("RNN Heuristic score at Node %d: %f\n" %( hole_node_ind, score))
        return best_node_ind
Exemplo n.º 3
0
    def train_more_epochs(self,program_to_train): 
        log_and_print("starting training more epochs")
        train_config = {
            'lr' : self.learning_rate,
            'neural_epochs' : 20,
            'symbolic_epochs' : 20,
            'optimizer' : optim.Adam,
            'lossfxn' : nn.CrossEntropyLoss(weight=self.loss_weight), #todo
            'evalfxn' : label_correctness,
            'num_labels' : self.num_labels
        }
        near_input_type = self.base_program.input_type
        near_output_type = self.base_program.output_type
        near_input_size = self.base_program.input_size
        near_output_size = self.base_program.output_size
        
        # Initialize program graph starting from trained NN
        program_graph = ProgramGraph(DSL_DICT, CUSTOM_EDGE_COSTS, near_input_type, near_output_type, near_input_size, near_output_size,
            self.max_num_units, self.min_num_units, self.max_num_children, self.max_depth, self.penalty, ite_beta=self.ite_beta)

        # Initialize algorithm
        algorithm = ASTAR_NEAR(frontier_capacity=self.frontier_capacity)
        best_programs = algorithm.run_train_longer(self.timestamp, program_to_train, self.hole_node_ind,
            program_graph, self.batched_trainset, self.validset, train_config, self.device)
        best_program_str = []
        # Print all best programs found
        log_and_print("\n")
        # log_and_print("BEST programs found:")
        for item in best_programs:
            program_struct = print_program(item["program"], ignore_constants=True)
            program_info = " score {:.4f} ".format(item["score"])
            best_program_str.append((program_struct, program_info))
            print(best_program_str)
            # print_program_dict(item)
        best_program = best_programs[-1]["program"]

        with torch.no_grad():
            test_input, test_output = map(list, zip(*self.testset))
            true_vals = torch.flatten(torch.stack(test_output)).float().to(self.device)	
            predicted_vals = self.process_batch(best_program, test_input, self.output_type, self.output_size, self.device)
            
            metric, additional_params = label_correctness(predicted_vals, true_vals, num_labels=self.num_labels)
        
        pickle.dump(best_program, open(program_to_train + ".p", "wb")) #overfit??
        log_and_print("end training more epochs")
Exemplo n.º 4
0
    def neural_h(self):
        data = self.base_program.submodules
        l = [] #populate AST
        traverse(data,l)
        train_config = {
            'lr' : self.learning_rate,
            'neural_epochs' : self.neural_epochs,
            'symbolic_epochs' : self.symbolic_epochs,
            'optimizer' : optim.Adam,
            'lossfxn' : nn.CrossEntropyLoss(weight=self.loss_weight), #todo
            'evalfxn' : label_correctness,
            'num_labels' : self.num_labels
        }

        scores = [self.base_program_name]
        for hole_node_ind in range(len(l)):

            hole_node = l[hole_node_ind]
            near_input_type = hole_node[0].input_type
            near_output_type = hole_node[0].output_type
            near_input_size = hole_node[0].input_size
            near_output_size = hole_node[0].output_size

            # Initialize program graph starting from trained NN
            program_graph = ProgramGraph(DSL_DICT, CUSTOM_EDGE_COSTS, near_input_type, near_output_type, near_input_size, near_output_size,
                self.max_num_units, self.min_num_units, self.max_num_children, 0, self.penalty, ite_beta=self.ite_beta) ## max_depth 0

            # Initialize algorithm
            algorithm = ASTAR_NEAR(frontier_capacity=0)
            score, new_prog, losses = algorithm.run_init(self.timestamp, self.base_program_name, hole_node_ind,
                program_graph, self.batched_trainset, self.validset, train_config, self.device)
            subprogram_str = print_program(hole_node[0])
            test_score = self.evaluate_neurosymb(new_prog)
            stats_arr = [subprogram_str, hole_node[1], score,test_score]
            stats_arr.extend(losses)
            scores.append(stats_arr)
            # scores.append()
            # h_file = os.path.join(self.save_path, "neursym_%d.p"%hole_node_ind)
            # pickle.dump(new_prog, open(h_file, "wb"))

        h_file = os.path.join(self.save_path, "neurh.csv")
        with open(h_file, "w", newline="") as f:
            writer = csv.writer(f)
            writer.writerows(scores)
Exemplo n.º 5
0
    def run_near(self): 
        # print(self.device)
        train_config = {
            'lr' : self.learning_rate,
            'neural_epochs' : self.neural_epochs,
            'symbolic_epochs' : self.symbolic_epochs,
            'optimizer' : optim.Adam,
            'lossfxn' : nn.CrossEntropyLoss(weight=self.loss_weight), #todo
            'evalfxn' : label_correctness,
            'num_labels' : self.num_labels
        }


        near_input_type = self.hole_node[0].input_type
        near_output_type = self.hole_node[0].output_type
        near_input_size = self.hole_node[0].input_size
        near_output_size = self.hole_node[0].output_size
        

        # Initialize program graph starting from trained NN
        program_graph = ProgramGraph(DSL_DICT, CUSTOM_EDGE_COSTS, near_input_type, near_output_type, near_input_size, near_output_size,
            self.max_num_units, self.min_num_units, self.max_num_children, self.max_depth, self.penalty, ite_beta=self.ite_beta)

        # Initialize algorithm
        algorithm = ASTAR_NEAR(frontier_capacity=self.frontier_capacity)
        best_programs = algorithm.run(self.timestamp, self.base_program_name, self.hole_node_ind,
            program_graph, self.batched_trainset, self.validset, train_config, self.device)
        best_program_str = []
        if self.algorithm == "rnn":
            # special case for RNN baseline
            best_program = best_programs
        else:
            # Print all best programs found
            log_and_print("\n")
            log_and_print("BEST programs found:")
            for item in best_programs:
                program_struct = print_program(item["program"], ignore_constants=True)
                program_info = "struct_cost {:.4f} | score {:.4f} | path_cost {:.4f} | time {:.4f}".format(
                    item["struct_cost"], item["score"], item["path_cost"], item["time"])
                best_program_str.append((program_struct, program_info))
                print_program_dict(item)
            best_program = best_programs[-1]["program"]

        
        # Save best programs
        f = open(os.path.join(self.save_path, "best_programs.txt"),"w")
        f.write( str(best_program_str) )
        f.close()

        self.program_path = os.path.join(self.save_path, "subprogram.p")
        pickle.dump(best_program, open(self.program_path, "wb"))

        self.full_path = os.path.join(self.save_path, "fullprogram.p")

        if self.device == 'cpu':
            base_program = CPU_Unpickler(open("%s.p" % self.base_program_name, "rb")).load()
        else:
            base_program = pickle.load(open("%s.p" % self.base_program_name, "rb"))

        curr_level = 0
        l = []
        traverse(base_program.submodules,l)
        curr_program = base_program.submodules
        change_key(base_program.submodules, [], self.hole_node_ind, best_program.submodules["program"])
        pickle.dump(base_program, open(self.full_path, "wb"))


        # Save parameters
        f = open(os.path.join(self.save_path, "parameters.txt"),"w")

        parameters = ['input_type', 'output_type', 'input_size', 'output_size', 'num_labels', 'neural_units', 'max_num_units', 
            'min_num_units', 'max_num_children', 'max_depth', 'penalty', 'ite_beta', 'train_valid_split', 'normalize', 'batch_size', 
            'learning_rate', 'neural_epochs', 'symbolic_epochs', 'lossfxn', 'class_weights', 'num_iter', 'num_f_epochs', 'algorithm', 
            'frontier_capacity', 'initial_depth', 'performance_multiplier', 'depth_bias', 'exponent_bias', 'num_mc_samples', 'max_num_programs', 
            'population_size', 'selection_size', 'num_gens', 'total_eval', 'mutation_prob', 'max_enum_depth', 'exp_id', 'base_program_name', 'hole_node_ind']
        for p in parameters:
            f.write( p + ': ' + str(self.__dict__[p]) + '\n' )
        f.close()
Exemplo n.º 6
0
        'lossfxn': lossfxn,
        'evalfxn': label_correctness,
        'num_labels': args.num_labels
    }

    # Initialize logging
    init_logging(save_path)
    log_and_print("Starting experiment {}\n".format(full_exp_name))

    # Initialize program graph
    program_graph = ProgramGraph(DSL_DICT,
                                 CUSTOM_EDGE_COSTS,
                                 args.input_type,
                                 args.output_type,
                                 args.input_size,
                                 args.output_size,
                                 args.max_num_units,
                                 args.min_num_units,
                                 args.max_num_children,
                                 args.max_depth,
                                 args.penalty,
                                 ite_beta=args.ite_beta)

    # Initialize algorithm
    if args.algorithm == "astar-near":
        algorithm = ASTAR_NEAR(frontier_capacity=args.frontier_capacity)
    elif args.algorithm == "iddfs-near":
        algorithm = IDDFS_NEAR(
            frontier_capacity=args.frontier_capacity,
            initial_depth=args.initial_depth,
            performance_multiplier=args.performance_multiplier,
            depth_bias=args.depth_bias,