Beispiel #1
0
    def build_models(self):
        rl_config = copy.deepcopy(self.rl_config)
        rl_config.checkpoint = None  

        print('Building Q network')
        if rl_config.model in VariationalModels:
            self.q_net = VariationalSolver(
                rl_config, None, self.eval_data, vocab=self.vocab, 
                is_train=False)
        else:
            self.q_net = Solver(
                rl_config, None, self.eval_data, vocab=self.vocab, 
                is_train=False)
        self.q_net.build()
        self.load_q_network()

        print('Building prior network')
        if self.prior_config.model in VariationalModels:
            self.pretrained_prior = VariationalSolver(
                self.prior_config, None, self.eval_data, vocab=self.vocab, 
                is_train=False)
        else:
            self.pretrained_prior = Solver(
                self.prior_config, None, self.eval_data, vocab=self.vocab, 
                is_train=False)
        self.pretrained_prior.build()
       
        # Freeze the weights so they stay constant
        self.pretrained_prior.model.eval()
        for params in self.pretrained_prior.model.parameters():
            params.requires_grad = False
        self.q_net.model.eval()
        for params in self.q_net.model.parameters():
            params.requires_grad = False
Beispiel #2
0
    def build_models(self):
        config = copy.deepcopy(self.config)

        # If loading RL checkpoint, ensure it doesn't try to load the ckpt
        # through Solver
        if self.config.load_rl_ckpt:
            config.checkpoint = None

        if self.config.model in VariationalModels:
            self.q_net = VariationalSolver(config,
                                           None,
                                           self.eval_data,
                                           vocab=self.vocab,
                                           is_train=True)
            self.target_q_net = VariationalSolver(config,
                                                  None,
                                                  self.eval_data,
                                                  vocab=self.vocab,
                                                  is_train=True)
        else:
            self.q_net = Solver(config,
                                None,
                                self.eval_data,
                                vocab=self.vocab,
                                is_train=True)
            self.target_q_net = Solver(config,
                                       None,
                                       self.eval_data,
                                       vocab=self.vocab,
                                       is_train=True)
        print('Building Q network')
        self.q_net.build()

        print('\nBuilding Target Q network')
        self.target_q_net.build()

        if self.config.model in VariationalModels:
            self.pretrained_prior = VariationalSolver(self.config,
                                                      None,
                                                      self.eval_data,
                                                      vocab=self.vocab,
                                                      is_train=True)
        else:
            self.pretrained_prior = Solver(self.config,
                                           None,
                                           self.eval_data,
                                           vocab=self.vocab,
                                           is_train=True)
        print('Building prior network')
        self.pretrained_prior.build()

        # Freeze the weights of the prior so it stays constant
        self.pretrained_prior.model.eval()
        for params in self.pretrained_prior.model.parameters():
            params.requires_grad = False

        print('Successfully initialized Q networks')
        self.t = 0
Beispiel #3
0
def main(config):
    # For fast training.
    # Create directories if not exist.
    if not os.path.exists(config.model_save_dir):
        os.makedirs(config.model_save_dir)
    if not os.path.exists(config.sample_dir):
        os.makedirs(config.sample_dir)
    if not os.path.exists(config.result_dir):
        os.makedirs(config.result_dir)

    # Data loader.
    dloader = data_loader(
        config.data_dir,
        batch_size=config.batch_size,
        mode=config.mode,
        num_workers=config.num_workers,
    )

    # Solver for training and testing StarGAN.
    solver = Solver(dloader, config)

    if config.mode == "train":
        solver.train()

    elif config.mode == "test":
        solver.test()
Beispiel #4
0
    def _solve_puzzle(self):

        self._label["text"] = "Working"
        self._window.update()

        try:

            board = Board()

            for row in range(Board.BOARD_SIZE):
                for col in range(Board.BOARD_SIZE):
                    str_val = self._inputs[row][col].get()
                    if str_val == "":
                        int_val = 0
                    else:
                        int_val = int(str_val)
                    board.set_cell_value(row, col, int_val)

            result = Solver().solve(board)

            if not result.success:
                self._label["text"] = "Failed"
                return

            self._label["text"] = "Success"
            self._paint_board(result.solution)

        except:
            self._label["text"] = "Error"
Beispiel #5
0
    def load_words(self, file_names):
        self.__words = FileReader().read(file_names[0])

        label = Label(text="\n".join(self.__words))
        self.body_widget.add_widget(label)

        self.dismiss_popup()

        self.__solver = Solver()
Beispiel #6
0
def main():

    # Parameters for training
    parameters = p.default_parameters()
    parameters[p.PREFIX] = 'imitation_bn_false'
    parameters[p.TFRECORDS] = 'hm/axial'
    parameters[p.LOSS_FN] = Loss.BDICE
    parameters[p.LR] = 0.001
    parameters[p.NUM_CLASSES] = 5
    parameters[p.OPTIMISER] = Optimiser.ADAM
    parameters[p.TRAIN_BATCH] = 16
    parameters[p.VAL_BATCH] = 16
    parameters[p.PATIENCE] = 50
    parameters[p.NETWORK] = Network
    # parameters[p.NETWORK]      =  ViewAggregation
    p.validate(parameters)

    # Create folder for the new model
    parameters[p.MODEL_PATH] = misc.new_checkpoint_path(
        prefix=parameters[p.PREFIX], tfr=parameters[p.TFRECORDS])

    # Load TFRecords
    tfrm = TFRecordsManager()
    tfrecord_path = misc.get_tfrecords_path() + f"/{parameters[p.TFRECORDS]}/"
    dataset = tfrm.load_datasets(tfrecord_path, parameters[p.TRAIN_BATCH],
                                 parameters[p.VAL_BATCH])

    network = parameters[p.NETWORK](num_classes=parameters[p.NUM_CLASSES])
    solver = Solver(network, parameters)
    epoch_metrics = dict()

    for epoch in range(1000):

        for mode in dataset:
            epoch_metrics[mode] = solver.run_epoch(dataset[mode], mode)

        best_val_loss = solver.best_val_loss
        val_loss = epoch_metrics['val']['imitation_output_loss']
        print(
            f'ValLoss:[{val_loss}] BestValLoss:[{best_val_loss}] EST:[{solver.early_stopping_tick}]',
            flush=True)

        for name in ['base', 'imitation', 'label']:
            amount = epoch_metrics["val"][f'{name}_output_loss']
            print(f'{name}_output_loss:', amount, flush=True)

        print(f'imitation_loss:',
              epoch_metrics["val"]['imitation_loss'],
              flush=True)
        print()

        if solver.early_stopping_tick > parameters[p.PATIENCE]:
            break
Beispiel #7
0
    def load_geometry(self, file_names):
        self.body_widget.clear_widgets()

        self.__geometry_present = FileReader().read(file_names[0])
        self.__geometry_graph = GeometryParser().parse(self.__geometry_present)

        self.__geometry_widget = GeometryWidget()
        self.__geometry_widget.show_geometry(self.__geometry_present)
        self.body_widget.add_widget(self.__geometry_widget)

        self.dismiss_popup()

        self.__solver = Solver()
Beispiel #8
0
    def __init__(self, reversed_mode, **kwargs):
        super().__init__(**kwargs)
        self.__popup = None
        self.__geometry_widget = None

        self.__geometry_graph = None
        self.__geometry_present = None
        self.__words = None
        self.__reversed_mode = reversed_mode

        self.__solver = Solver()
        self.__same_letters_checker = SameLettersChecker()

        self.load = None
Beispiel #9
0
    def test_solver(self):
        geometry = GeometryParser().parse(self.__geometry_present)
        solution = Solver().solve(geometry, self.__words, reversed_mode=False)
        check_solution = {}

        for node, word in solution.items():
            check_solution[node.id] = word

        expected_geometry = self.create_expected_geometry()
        expected_solution = {
            expected_geometry.next_node().id: "hello",
            expected_geometry.next_node().id: "hunter"
        }

        self.assertEqual(len(check_solution), len(expected_solution))
        self.assertDictEqual(check_solution, expected_solution)
Beispiel #10
0
    def solve(self):
        nodes, edges = self.get_map()

        world = World(nodes, self.cost, edges=edges)

        ants = 20
        solver = Solver(ant_count=ants)
        solution = solver.solve(world)

        print("Nodes: ", len(nodes))
        print("Number of ants: ", ants)
        print("Best distance: ", solution.distance)

        print("Path: ")
        path = solution.tour
        for i in range(len(path)):
            print("\t" + str(i + 1) + ": ", path[i][3])
Beispiel #11
0
    def build(self):

        start = time.time()

        s = Solver(self.__board, self.__init_actions, False)

        #s.solve()

        board = s.get_board()

        end = time.time()

        print(end - start)

        s.print_taken_steps()

        return BoardView(self.__board)
Beispiel #12
0
def main():

    # Parameters for training
    parameters = p.default_parameters()
    parameters[p.PREFIX]       = 'hm_aug_BDICE_50'
    parameters[p.TFRECORDS]    = 'hm_aug/axial'
    parameters[p.LOSS_FN]      =  Loss.BDICE
    parameters[p.LR]           =  0.001
    parameters[p.NUM_CLASSES]  =  5
    parameters[p.OPTIMISER]    =  Optimiser.ADAM
    parameters[p.TRAIN_BATCH]  =  10
    parameters[p.VAL_BATCH]    =  30
    parameters[p.PATIENCE]     =  50
    parameters[p.NETWORK]      =  CDFNet
    # parameters[p.NETWORK]      =  ViewAggregation
    p.validate(parameters)

    # Create folder for the new model
    parameters[p.MODEL_PATH] = misc.new_checkpoint_path(prefix=parameters[p.PREFIX], tfr=parameters[p.TFRECORDS])

    # Load TFRecords
    tfrm = TFRecordsManager()
    tfrecord_path = misc.get_tfrecords_path() + f"/{parameters[p.TFRECORDS]}/"
    dataset = tfrm.load_datasets(tfrecord_path, parameters[p.TRAIN_BATCH], parameters[p.VAL_BATCH])

    network = parameters[p.NETWORK](num_classes=parameters[p.NUM_CLASSES])
    solver = Solver(network, parameters)
    epoch_metrics = dict()

    for epoch in range(1000):

        for mode in dataset:
            epoch_metrics[mode] = solver.run_epoch(dataset[mode], mode)

        best_val_loss = solver.best_val_loss
        val_loss = epoch_metrics['val']['loss']
        print(f'ValLoss:[{val_loss}] BestValLoss:[{best_val_loss}] EST:[{solver.early_stopping_tick}]', flush=True)
        if solver.early_stopping_tick > parameters[p.PATIENCE]:
            break

    if type(parameters[p.NETWORK]()) == CDFNet:
        # Run predictions for dataset
        model_folder = parameters[p.MODEL_PATH].split('/')[-2]
        predict(model_folder, parameters[p.TFRECORDS], prefix=parameters[p.PREFIX])
Beispiel #13
0
def console_mode_solve():
    file_reader = FileReader()
    geometry_present = file_reader.read(args.geometry)

    height = len(geometry_present)
    if height < 1:
        print("Incorrect geometry format. Try main.py -h or --help",
              file=sys.stderr)
        exit(1)

    width = len(geometry_present[0])

    geometry = GeometryParser().parse(geometry_present)
    words = file_reader.read(args.words)
    solution = Solver().solve(geometry, words, args.reversed)
    present = Presenter().get_present(width,
                                      height,
                                      solution,
                                      filled_grid=False)
    for line in present:
        print("".join(line))
Beispiel #14
0
        sentences=load_pickle(config.sentences_path),
        conversation_length=load_pickle(config.conversation_length_path),
        sentence_length=load_pickle(config.sentence_length_path),
        vocab=vocab,
        batch_size=config.batch_size,
        emojis=emotion_sentences,
        infersent=infersent_sentences)

    if config.model in VariationalModels:
        solver = VariationalSolver(config,
                                   None,
                                   data_loader,
                                   vocab=vocab,
                                   is_train=False)
    else:
        solver = Solver(config, None, data_loader, vocab=vocab, is_train=False)

    solver.build()

    self_play_buffer = pd.DataFrame()

    for i in range(kwargs.sample_conversations):
        messages = solver.self_play(
            conversation_length=kwargs.conversation_length,
            max_sentence_length=kwargs.max_sentence_length,
            max_conversation_length=kwargs.max_conversation_length,
            sample_by=kwargs.sample_by)
        responses = messages[1:] + ['']
        conv_df = pd.DataFrame()
        conv_df['Message'] = messages
        conv_df['Response'] = responses
Beispiel #15
0
 def __init__(self, gui):
     self.solver = Solver()
     self.gui = gui
     self.gui.controller = self
Beispiel #16
0
    def __init__(self,
                 id,
                 name,
                 checkpoint_path,
                 max_conversation_length=5,
                 max_sentence_length=30,
                 is_test_bot=False,
                 rl=False,
                 safe_mode=True):
        """
        All chatbots should extend this class and be registered with the @registerbot decorator
        :param id: An id string, must be unique!
        :param name: A user-friendly string shown to the end user to identify the chatbot. Should be unique.
        :param checkpoint_path: Directory where the trained model checkpoint is saved.
        :param max_conversation_length: Maximum number of conversation turns to condition on.
        :param max_sentence_length: Maximum number of tokens per sentence.
        :param is_test_bot: If True, this bot it can be chosen from the list of
            bots you see at /dialogadmins screen, but will never be randomly
            assigned to users landing on the home page.
        """
        self.id = id
        self.name = name
        self.checkpoint_path = checkpoint_path
        self.max_conversation_length = max_conversation_length
        self.max_sentence_length = max_sentence_length
        self.is_test_bot = is_test_bot
        self.safe_mode = safe_mode

        print("\n\nCreating chatbot", name)

        self.config = get_config_from_dir(checkpoint_path,
                                          mode='test',
                                          load_rl_ckpt=rl)
        self.config.beam_size = 5

        print('Loading Vocabulary...')
        self.vocab = Vocab()
        self.vocab.load(self.config.word2id_path, self.config.id2word_path)
        print(f'Vocabulary size: {self.vocab.vocab_size}')

        self.config.vocab_size = self.vocab.vocab_size

        # If checkpoint is for an emotion model, load that pickle file
        emotion_sentences = None
        if self.config.emotion:
            emotion_sentences = load_pickle(self.config.emojis_path)

        # Load infersent embeddings if necessary
        infersent_sentences = None
        if self.config.infersent:
            print('Loading infersent sentence embeddings...')
            infersent_sentences = load_pickle(self.config.infersent_path)
            embedding_size = infersent_sentences[0][0].shape[0]
            self.config.infersent_output_size = embedding_size

        self.data_loader = get_loader(
            sentences=load_pickle(self.config.sentences_path),
            conversation_length=load_pickle(
                self.config.conversation_length_path),
            sentence_length=load_pickle(self.config.sentence_length_path),
            vocab=self.vocab,
            batch_size=self.config.batch_size,
            emojis=emotion_sentences)

        if self.config.model in VariationalModels:
            self.solver = VariationalSolver(self.config,
                                            None,
                                            self.data_loader,
                                            vocab=self.vocab,
                                            is_train=False)
        elif self.config.model == 'Transformer':
            self.solver = ParlAISolver(self.config)
        else:
            self.solver = Solver(self.config,
                                 None,
                                 self.data_loader,
                                 vocab=self.vocab,
                                 is_train=False)

        self.solver.build()