コード例 #1
0
    def __init__(self):
        """ Constructor for RuleInformBot class. """
        SysPolicy.__init__(self)

        self.cur_inform_slot_id = 0
        self.cur_request_slot_id = 0
        self.domains = ['Taxi']
コード例 #2
0
    def __init__(self,
                 archive_file=DEFAULT_ARCHIVE_FILE,
                 cuda_device=DEFAULT_CUDA_DEVICE,
                 model_file=None):
        """ Constructor for NLU class. """
        SysPolicy.__init__(self)

        check_for_gpu(cuda_device)

        if not os.path.isfile(archive_file):
            if not model_file:
                raise Exception("No model for MILU is specified!")
            archive_file = cached_path(model_file)

        archive = load_archive(archive_file, cuda_device=cuda_device)
        dataset_reader_params = archive.config["dataset_reader"]
        self.dataset_reader = DatasetReader.from_params(dataset_reader_params)
        self.action_vocab = self.dataset_reader.action_vocab
        self.state_encoder = self.dataset_reader.state_encoder
        self.model = archive.model
        self.model.eval()
コード例 #3
0
    def __init__(self, archive_file=DEFAULT_ARCHIVE_FILE, model_file=None):
        SysPolicy.__init__(self)

        if not os.path.isfile(archive_file):
            if not model_file:
                raise Exception("No model for Sequicity is specified!")
            archive_file = cached_path(model_file)
        model_dir = os.path.dirname(os.path.abspath(__file__))
        if not os.path.exists(os.path.join(model_dir, 'data')):
            archive = zipfile.ZipFile(archive_file, 'r')
            archive.extractall(model_dir)

        cfg.init_handler('tsdf-multiwoz')

        torch.manual_seed(cfg.seed)
        torch.cuda.manual_seed(cfg.seed)
        random.seed(cfg.seed)
        np.random.seed(cfg.seed)
        self.m = Model('multiwoz')
        self.m.count_params()
        self.m.load_model()
        self.reset()
コード例 #4
0
    def __init__(self,
                 archive_file=DEFAULT_ARCHIVE_FILE,
                 cuda_device=DEFAULT_CUDA_DEVICE,
                 model_file=None):
        SysPolicy.__init__(self)

        if not os.path.isfile(archive_file):
            if not model_file:
                raise Exception("No model for LaRL is specified!")
            archive_file = cached_path(model_file)

        temp_path = tempfile.mkdtemp()
        zip_ref = zipfile.ZipFile(archive_file, 'r')
        zip_ref.extractall(temp_path)
        zip_ref.close()

        self.prev_state = init_state()
        self.prev_active_domain = None

        domain_name = 'object_division'
        domain_info = domain.get_domain(domain_name)

        data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
        train_data_path = os.path.join(data_path, 'norm-multi-woz', 'train_dials.json')
        if not os.path.exists(train_data_path):
            zipped_file = os.path.join(data_path, 'norm-multi-woz.zip')
            archive = zipfile.ZipFile(zipped_file, 'r')
            archive.extractall(data_path)

        norm_multiwoz_path = os.path.join(data_path, 'norm-multi-woz')
        with open(os.path.join(norm_multiwoz_path, 'input_lang.index2word.json')) as f:
            self.input_lang_index2word = json.load(f)
        with open(os.path.join(norm_multiwoz_path, 'input_lang.word2index.json')) as f:
            self.input_lang_word2index = json.load(f)
        with open(os.path.join(norm_multiwoz_path, 'output_lang.index2word.json')) as f:
            self.output_lang_index2word = json.load(f)
        with open(os.path.join(norm_multiwoz_path, 'output_lang.word2index.json')) as f:
            self.output_lang_word2index = json.load(f)

        config = Pack(
            seed=10,
            train_path=train_data_path,
            max_vocab_size=1000,
            last_n_model=5,
            max_utt_len=50,
            max_dec_len=50,
            backward_size=2,
            batch_size=1,
            use_gpu=True,
            op='adam',
            init_lr=0.001,
            l2_norm=1e-05,
            momentum=0.0,
            grad_clip=5.0,
            dropout=0.5,
            max_epoch=100,
            embed_size=100,
            num_layers=1,
            utt_rnn_cell='gru',
            utt_cell_size=300,
            bi_utt_cell=True,
            enc_use_attn=True,
            dec_use_attn=True,
            dec_rnn_cell='lstm',
            dec_cell_size=300,
            dec_attn_mode='cat',
            y_size=10,
            k_size=20,
            beta=0.001,
            simple_posterior=True,
            contextual_posterior=True,
            use_mi=False,
            use_pr=True,
            use_diversity=False,
            #
            beam_size=20,
            fix_batch=True,
            fix_train_batch=False,
            avg_type='word',
            print_step=300,
            ckpt_step=1416,
            improve_threshold=0.996,
            patient_increase=2.0,
            save_model=True,
            early_stop=False,
            gen_type='greedy',
            preview_batch_num=None,
            k=domain_info.input_length(),
            init_range=0.1,
            pretrain_folder='2019-09-20-21-43-06-sl_cat',
            forward_only=False
        )

        config.use_gpu = config.use_gpu and torch.cuda.is_available()
        self.corpus = corpora_inference.NormMultiWozCorpus(config)
        self.model = SysPerfectBD2Cat(self.corpus, config)
        self.config = config
        if config.use_gpu:
            self.model.load_state_dict(torch.load(
                os.path.join(temp_path, 'larl_model/best-model')))
            self.model.cuda()
        else:
            self.model.load_state_dict(torch.load(os.path.join(
                temp_path, 'larl_model/best-model'), map_location=lambda storage, loc: storage))
        self.model.eval()
        self.dic = pickle.load(
            open(os.path.join(temp_path, 'larl_model/svdic.pkl'), 'rb'))
コード例 #5
0
    def __init__(self,
                 model='gpt2_v1',
                 model_checkpoint='./models/v1',
                 max_history=15,
                 device='cuda',
                 no_sample=False,
                 max_length=40,
                 min_length=1,
                 seed=42,
                 temperature=0.9,
                 top_k=0,
                 top_p=0.8):

        SysPolicy.__init__(self)
        if not os.path.isdir("./models"):
            download_model_from_googledrive(
                file_id='14bVEODjyoSV6yZbrzEz4NA8kXMuPbLJH',
                dest_path='./models/models.zip')
        self.model_checkpoint = model_checkpoint
        self.max_history = max_history
        self.max_length = max_length
        self.min_length = min_length
        self.temperature = temperature
        self.top_k = top_k
        self.top_p = top_p
        self.no_sample = no_sample
        self.device = device
        self.seed = seed
        self.domains = [
            'hotel', 'restaurant', 'train', 'taxi', 'attraction', 'police',
            'hospital'
        ]
        self.cs_mapping = {
            'restaurant': ['food', 'pricerange', 'name', 'area'],
            'hospital': ['department', 'phone'],
            'hotel': [
                'name', 'area', 'parking', 'pricerange', 'stars', 'internet',
                'type'
            ],
            'attraction': ['type', 'name', 'area'],
            'train':
            ['leaveat', 'destination', 'day', 'arriveby', 'departure'],
            'taxi': ['leaveat', 'destination', 'departure', 'arriveby'],
            'police': []
        }
        dia_act = open('./data/multiwoz/dialog_act_slot.txt', 'r')
        f = dia_act.read().split('\n')
        self.dia_act_dict = {}
        key = ""
        for i, c in enumerate(f):
            if i == 0: continue  # User Dialog Act case
            t = c.split('\t')
            if len(t) == 1:
                key = t[0].lower()
                self.dia_act_dict[key] = []
            else:
                self.dia_act_dict[key].append(t[-1].strip().lower())
        random.seed(self.seed)
        torch.random.manual_seed(seed)
        torch.cuda.manual_seed(seed)
        self.cur_dom = ''
        self.prev_dom = ''
        self.prev_cs = []

        self.model_name = model

        tokenizer_class = GPT2Tokenizer
        model_class = GPT2DoubleHeadsModel
        self.model = model_class.from_pretrained(self.model_checkpoint)

        if 'v1' in self.model_name:
            self.SPECIAL_TOKENS = SPECIAL_TOKENS_V1
        else:
            self.SPECIAL_TOKENS = SPECIAL_TOKENS_V4

        if "gpt2" in self.model_name:
            self.tokenizer = tokenizer_class.from_pretrained(
                model_checkpoint, unk_token='<|unkwn|>')
            SPECIAL_TOKENS_DICT = {}
            for st in self.SPECIAL_TOKENS:
                SPECIAL_TOKENS_DICT[st] = st
            self.tokenizer.add_special_tokens(SPECIAL_TOKENS_DICT)
            self.model.resize_token_embeddings(len(self.tokenizer))

        else:
            self.tokenizer = tokenizer_class.from_pretrained(model_checkpoint)
            self.tokenizer.set_special_tokens(self.SPECIAL_TOKENS)
            self.model.set_num_special_tokens(len(self.SPECIAL_TOKENS))
            for s in self.SPECIAL_TOKENS:
                self.tokenizer.nlp.tokenizer.add_special_case(s, [{ORTH: s}])
        self.model.to(self.device)
        self.model.eval()
        self.count = 0
        self.reset()
コード例 #6
0
 def __init__(self):
     SysPolicy.__init__(self)
     self.last_state = {}