Ejemplo n.º 1
0
    def __init__(self, schema, lexicon, model_path, fact_check, decoding, timed_session=False, consecutive_entity=True, realizer=None):
        super(NeuralSystem, self).__init__()
        self.schema = schema
        self.lexicon = lexicon
        self.timed_session = timed_session
        self.consecutive_entity = consecutive_entity

        # Load arguments
        args_path = os.path.join(model_path, 'config.json')
        config = read_json(args_path)
        config['batch_size'] = 1
        config['gpu'] = 0  # Don't need GPU for batch_size=1
        config['decoding'] = decoding
        args = argparse.Namespace(**config)

        mappings_path = os.path.join(model_path, 'vocab.pkl')
        mappings = read_pickle(mappings_path)
        vocab = mappings['vocab']

        # TODO: different models have the same key now
        args.dropout = 0
        logstats.add_args('model_args', args)
        model = build_model(schema, mappings, args)

        # Tensorflow config
        if args.gpu == 0:
            print 'GPU is disabled'
            config = tf.ConfigProto(device_count = {'GPU': 0})
        else:
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction = 0.5, allow_growth=True)
            config = tf.ConfigProto(device_count = {'GPU': 1}, gpu_options=gpu_options)

        # NOTE: need to close the session when done
        tf_session = tf.Session(config=config)
        tf.initialize_all_variables().run(session=tf_session)

        # Load TF model parameters
        ckpt = tf.train.get_checkpoint_state(model_path+'-best')
        assert ckpt, 'No checkpoint found'
        assert ckpt.model_checkpoint_path, 'No model path found in checkpoint'
        saver = tf.train.Saver()
        saver.restore(tf_session, ckpt.model_checkpoint_path)

        self.model_name = args.model
        if self.model_name == 'attn-copy-encdec':
            args.entity_target_form = 'graph'
            copy = True
        else:
            copy = False
        preprocessor = Preprocessor(schema, lexicon, args.entity_encoding_form, args.entity_decoding_form, args.entity_target_form, args.prepend)
        textint_map = TextIntMap(vocab, mappings['entity'], preprocessor)

        Env = namedtuple('Env', ['model', 'tf_session', 'preprocessor', 'vocab', 'copy', 'textint_map', 'stop_symbol', 'remove_symbols', 'max_len', 'evaluator', 'prepend', 'consecutive_entity', 'realizer'])
        self.env = Env(model, tf_session, preprocessor, mappings['vocab'], copy, textint_map, stop_symbol=vocab.to_ind(markers.EOS), remove_symbols=map(vocab.to_ind, (markers.EOS, markers.PAD)), max_len=20, evaluator=FactEvaluator() if fact_check else None, prepend=args.prepend, consecutive_entity=self.consecutive_entity, realizer=realizer)
Ejemplo n.º 2
0
    def __init__(self, schema, price_tracker, retriever, model_path, mappings, timed_session=False):
        super(NeuralRankerSystem, self).__init__()
        self.schema = schema
        self.price_tracker = price_tracker
        self.timed_session = timed_session

        # Load arguments
        args_path = os.path.join(model_path, 'config.json')
        config = read_json(args_path)
        # TODO: handle this properly
        config['batch_size'] = 1
        config['pretrained_wordvec'] = None
        args = argparse.Namespace(**config)

        mappings_path = os.path.join(mappings, 'vocab.pkl')
        mappings = read_pickle(mappings_path)
        vocab = mappings['vocab']

        logstats.add_args('model_args', args)
        model = build_model(schema, mappings, None, args)

        # Tensorflow config
        if args.gpu == 0:
            print 'GPU is disabled'
            config = tf.ConfigProto(device_count = {'GPU': 0})
        else:
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction = 0.5, allow_growth=True)
            config = tf.ConfigProto(device_count = {'GPU': 1}, gpu_options=gpu_options)
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # NOTE: need to close the session when done
        tf_session = tf.Session(config=config)
        tf_session.run(tf.global_variables_initializer())

        # Load TF model parameters
        ckpt = tf.train.get_checkpoint_state(model_path+'-best')
        assert ckpt, 'No checkpoint found'
        assert ckpt.model_checkpoint_path, 'No model path found in checkpoint'
        saver = tf.train.Saver()
        saver.restore(tf_session, ckpt.model_checkpoint_path)

        preprocessor = Preprocessor(schema, price_tracker, 'canonical', 'canonical', 'canonical')
        textint_map = TextIntMap(vocab, preprocessor)

        int_markers = SpecialSymbols(*[mappings['vocab'].to_ind(m) for m in markers])
        model_config = {'retrieve': True}
        batcher = DialogueBatcherFactory.get_dialogue_batcher(model_config, int_markers=int_markers, slot_filling=False, kb_pad=mappings['kb_vocab'].to_ind(markers.PAD))

        StreamingDialogue.textint_map = textint_map
        StreamingDialogue.num_context = args.num_context
        StreamingDialogue.mappings = mappings

        Env = namedtuple('Env', ['ranker', 'retriever', 'tf_session', 'preprocessor', 'mappings', 'textint_map', 'batcher'])
        self.env = Env(model, retriever, tf_session, preprocessor, mappings, textint_map, batcher)
Ejemplo n.º 3
0
                        default=False,
                        action='store_true',
                        help='Test using the best model on dev set')
    parser.add_argument('--verbose',
                        default=False,
                        action='store_true',
                        help='More prints')
    options.add_data_generator_arguments(parser)
    options.add_model_arguments(parser)
    cocoa.options.add_trainer_arguments(parser)
    args = parser.parse_args()

    random.seed(args.random_seed)
    create_path(args.stats_file)
    logstats.init(args.stats_file, args.verbose)
    logstats.add_args('config', args)
    model_args = args

    if torch.cuda.is_available() and not args.gpuid:
        print("WARNING: You have a CUDA device, should run with -gpuid 0")

    if args.gpuid:
        cuda.set_device(args.gpuid[0])
        if args.random_seed > 0:
            torch.cuda.manual_seed(args.random_seed)

    loading_timer = tm.time()

    schema = Schema(model_args.schema_path, None)
    data_generator = get_data_generator(args, model_args, schema)
    mappings = data_generator.mappings
Ejemplo n.º 4
0
    def __init__(self,
                 schema,
                 price_tracker,
                 model_path,
                 mappings_path,
                 decoding,
                 index=None,
                 num_candidates=20,
                 retriever_context_len=2,
                 timed_session=False):
        super(NeuralSystem, self).__init__()
        self.schema = schema
        self.price_tracker = price_tracker
        self.timed_session = timed_session

        # Load arguments
        args_path = os.path.join(model_path, 'config.json')
        config = read_json(args_path)
        config['batch_size'] = 1
        config['gpu'] = 0  # Don't need GPU for batch_size=1
        config['decoding'] = decoding
        config['pretrained_wordvec'] = None
        args = argparse.Namespace(**config)

        vocab_path = os.path.join(mappings_path, 'vocab.pkl')
        mappings = read_pickle(vocab_path)
        vocab = mappings['vocab']

        # TODO: different models have the same key now
        args.dropout = 0
        logstats.add_args('model_args', args)
        model = build_model(schema, mappings, None, args)

        # Tensorflow config
        if args.gpu == 0:
            print 'GPU is disabled'
            config = tf.ConfigProto(device_count={'GPU': 0})
        else:
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5,
                                        allow_growth=True)
            config = tf.ConfigProto(device_count={'GPU': 1},
                                    gpu_options=gpu_options)

        # NOTE: need to close the session when done
        tf_session = tf.Session(config=config)
        tf.initialize_all_variables().run(session=tf_session)

        # Load TF model parameters
        ckpt = tf.train.get_checkpoint_state(model_path + '-best')
        assert ckpt, 'No checkpoint found'
        assert ckpt.model_checkpoint_path, 'No model path found in checkpoint'
        saver = tf.train.Saver()
        saver.restore(tf_session, ckpt.model_checkpoint_path)

        # Model config tells data generator which batcher to use
        model_config = {}
        if args.retrieve or args.model in ('ir', 'selector'):
            model_config['retrieve'] = True
        if args.predict_price:
            model_config['price'] = True

        self.model_name = args.model
        preprocessor = Preprocessor(schema, price_tracker,
                                    args.entity_encoding_form,
                                    args.entity_decoding_form,
                                    args.entity_target_form)
        textint_map = TextIntMap(vocab, preprocessor)
        int_markers = SpecialSymbols(
            *[mappings['vocab'].to_ind(m) for m in markers])
        dialogue_batcher = DialogueBatcherFactory.get_dialogue_batcher(
            model_config,
            int_markers=int_markers,
            slot_filling=False,
            kb_pad=mappings['kb_vocab'].to_ind(markers.PAD))

        # Retriever
        if args.model == 'selector':
            retriever = Retriever(index,
                                  context_size=retriever_context_len,
                                  num_candidates=num_candidates)
        else:
            retriever = None

        #TODO: class variable is not a good way to do this
        Dialogue.mappings = mappings
        Dialogue.textint_map = textint_map
        Dialogue.preprocessor = preprocessor
        Dialogue.num_context = args.num_context

        Env = namedtuple('Env', [
            'model', 'tf_session', 'preprocessor', 'vocab', 'textint_map',
            'stop_symbol', 'remove_symbols', 'max_len', 'dialogue_batcher',
            'retriever'
        ])
        self.env = Env(model,
                       tf_session,
                       preprocessor,
                       mappings['vocab'],
                       textint_map,
                       stop_symbol=vocab.to_ind(markers.EOS),
                       remove_symbols=map(vocab.to_ind,
                                          (markers.EOS, markers.PAD)),
                       max_len=20,
                       dialogue_batcher=dialogue_batcher,
                       retriever=retriever)
Ejemplo n.º 5
0
    def __init__(self, args, schema, lexicon, model_path, timed):
        super(PytorchNeuralSystem, self).__init__()
        self.schema = schema
        self.lexicon = lexicon
        self.timed_session = timed

        # TODO: do we need the dummy parser?
        dummy_parser = argparse.ArgumentParser(description='duh')
        options.add_model_arguments(dummy_parser)
        options.add_data_generator_arguments(dummy_parser)
        dummy_args = dummy_parser.parse_known_args([])[0]

        # Load the model.
        mappings, model, model_args = model_builder.load_test_model(
            model_path, args, dummy_args.__dict__)
        logstats.add_args('model_args', model_args)
        self.model_name = model_args.model
        utterance_vocab = mappings['utterance_vocab']
        kb_vocab = mappings['kb_vocab']
        self.mappings = mappings

        text_generator = get_generator(model, utterance_vocab,
                                       Scorer(args.alpha), args, model_args)
        builder = UtteranceBuilder(utterance_vocab, args.n_best, has_tgt=True)

        preprocessor = Preprocessor(schema, lexicon,
                                    model_args.entity_encoding_form,
                                    model_args.entity_decoding_form,
                                    model_args.entity_target_form)
        textint_map = TextIntMap(utterance_vocab, preprocessor)
        remove_symbols = map(utterance_vocab.to_ind,
                             (markers.EOS, markers.PAD))
        use_cuda = use_gpu(args)

        dialogue_batcher = DialogueBatcherFactory.get_dialogue_batcher(
            model=self.model_name,
            kb_pad=None,
            mappings=mappings,
            num_context=model_args.num_context)

        #TODO: class variable is not a good way to do this
        Dialogue.preprocessor = preprocessor
        Dialogue.textint_map = textint_map
        Dialogue.mappings = mappings
        Dialogue.num_context = model_args.num_context

        Env = namedtuple('Env', [
            'model', 'utterance_vocab', 'kb_vocab', 'preprocessor',
            'textint_map', 'stop_symbol', 'remove_symbols', 'gt_prefix',
            'max_len', 'dialogue_batcher', 'cuda', 'dialogue_generator',
            'utterance_builder', 'model_args'
        ])
        self.env = Env(model,
                       utterance_vocab,
                       kb_vocab,
                       preprocessor,
                       textint_map,
                       stop_symbol=utterance_vocab.to_ind(markers.EOS),
                       remove_symbols=remove_symbols,
                       gt_prefix=1,
                       max_len=20,
                       dialogue_batcher=dialogue_batcher,
                       cuda=use_cuda,
                       dialogue_generator=text_generator,
                       utterance_builder=builder,
                       model_args=model_args)