Esempio n. 1
0
    def init_trainer(self, args):
        if args.gpuid:
            print('Running with GPU {}.'.format(args.gpuid[0]))
            cuda.set_device(args.gpuid[0])
        else:
            print('Running with CPU.')

        if args.random_seed:
            random.seed(args.random_seed + os.getpid())
            np.random.seed(args.random_seed + os.getpid())

        schema = Schema(args.schema_path)
        scenario_db = ScenarioDB.from_dict(schema,
                                           read_json(args.scenarios_path),
                                           Scenario)
        valid_scenario_db = ScenarioDB.from_dict(
            schema, read_json(args.valid_scenarios_path), Scenario)

        # if len(args.agent_checkpoints) == 0
        # assert len(args.agent_checkpoints) <= len(args.agents)
        if len(args.agent_checkpoints) < len(args.agents):
            ckpt = [None] * 2
        else:
            ckpt = args.agent_checkpoints

        systems = [
            get_system(name, args, schema, False, ckpt[i])
            for i, name in enumerate(args.agents)
        ]

        rl_agent = 0
        system = systems[rl_agent]
        model = system.env.model
        loss = None
        # optim = build_optim(args, [model, system.env.critic], None)
        optim = {
            'model': build_optim(args, model, None),
            'critic': build_optim(args, system.env.critic, None)
        }
        optim['critic']._set_rate(0.05)

        scenarios = {
            'train': scenario_db.scenarios_list,
            'dev': valid_scenario_db.scenarios_list
        }
        from neural.a2c_trainer import RLTrainer as A2CTrainer
        trainer = A2CTrainer(systems,
                             scenarios,
                             loss,
                             optim,
                             rl_agent,
                             reward_func=args.reward,
                             cuda=(len(args.gpuid) > 0),
                             args=args)

        self.args = args
        self.trainer = trainer
        self.systems = systems
Esempio n. 2
0
    def __init__(self, use_gpu=False):
        # make args that are supposed to be passed in by command line arguments
        args = {
            "model": "lf2lf",
            "word_vec_size": 300,
            "dropout": 0.,
            "encoder_type": "rnn",
            "decoder_type": "rnn",
            "context_embedder_type": "mean",
            "global_attention": "multibank_general",
            "share_embeddings": False,
            "share_decoder_embeddings": False,
            "enc_layers": 1,
            "copy_attn": False,
            "dec_layers": 1,
            "pretrained_wordvec": "",
            "rnn_size": 300,
            "rnn_type": "LSTM",
            "enc_layers": 1,
            "num_context": 2,
            "stateful": True,
            "sample": True,
            "max_length": 10,
            "n_best": 1,
            "batch_size": 128,
            "optim": "adagrad",
            "alpha": 0.01,
            "temperature": 0.5,
            "epochs": 30,
            "report_every": 500,
        }
        if use_gpu:
            args.gpuid = 0

        # HACK: convert args from dict into object. Ex. args["epochs"]
        # becomes args.epochs
        args = type("args", (), args)

        # load price tracker
        with open(self.PRICE_TRACKER_PATH) as f:
            price_tracker = pickle.load(f)

        # load schema
        schema = Schema(self.SCHEMA_PATH)

        # load system
        self.system = PytorchNeuralSystem(args, schema, price_tracker,
                                          self.MODEL_PATH, False)

        # load scenario db
        with open(self.DATA_PATH) as f:
            raw = json.load(f)
        raw = [r["scenario"] for r in raw]  # HACK
        self.scenario_db = ScenarioDB.from_dict(schema, raw, Scenario)
Esempio n. 3
0
 def __init__(self, options, models=None):
     super(RuleBasedAgent, self).__init__(options, models=models)
     args = argparse.Namespace(
         random_seed=hash(self.options.random_seed),
         schema_path='cocoa-negotiation/fb-negotiation/data/bookhatball-schema.json',
         scenarios_path='cocoa-negotiation/fb-negotiation/data/toy-scenarios.json',
         train_examples_paths='cocoa-negotiation/fb-negotiation/data/rulebased-transcripts.json',
         train_max_examples=1,
         test_max_examples=0,
         max_turns=self.options.max_dialogue_len,
         agents=['rulebased', 'rulebased'],
         templates='cocoa-negotiation/fb-negotiation/templates.pkl',
         policy='cocoa-negotiation/fb-negotiation/model.pkl',
     )
     self.schema = Schema(args.schema_path)
     self.agent = get_system('rulebased', args, self.schema,
                             model_path='cocoa-negotiation/model.pkl')
Esempio n. 4
0
    args = parser.parse_args()

    random.seed(args.random_seed)
    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
    if args.vocab_only:
        import sys; sys.exit()

    if args.verbose:
        print("Finished loading and pre-processing data, took {:.1f} seconds".format(tm.time() - loading_timer))

    # TODO: load from checkpoint
    ckpt = None

    # Build the model
    model = build_model(model_args, args, mappings, ckpt, model_path=args.agent_checkpoint)
    tally_parameters(model)
    create_path(args.model_path)
Esempio n. 5
0
    else:
        raise ValueError(
            "Location of HTML templates should be specified in config with the key templates_dir"
        )
    if not os.path.exists(templates_dir):
        raise ValueError("Specified HTML template location doesn't exist: %s" %
                         templates_dir)

    app = create_app(debug=False, templates_dir=templates_dir)

    schema_path = args.schema_path

    if not os.path.exists(schema_path):
        raise ValueError("No schema file found at %s" % schema_path)

    schema = Schema(schema_path)
    scenarios = read_json(args.scenarios_path)
    if args.num_scenarios is not None:
        scenarios = scenarios[:args.num_scenarios]
    scenario_db = ScenarioDB.from_dict(schema, scenarios, Scenario)
    app.config['scenario_db'] = scenario_db

    if 'models' not in params.keys():
        params['models'] = {}

    if 'quit_after' not in params.keys():
        params[
            'quit_after'] = params['status_params']['chat']['num_seconds'] + 1

    if 'skip_chat_enabled' not in params.keys():
        params['skip_chat_enabled'] = False
Esempio n. 6
0
from model.manager import Manager

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--transcripts',
                        nargs='*',
                        help='JSON transcripts to extract templates')
    parser.add_argument('--max-examples', default=-1, type=int)
    parser.add_argument('--templates', help='Path to load templates')
    parser.add_argument('--policy', help='Path to load model')
    parser.add_argument('--schema-path', help='Path to schema')
    parser.add_argument(
        '--agent', help='Only consider examples with the given type of agent')
    args = parser.parse_args()

    schema = Schema(args.schema_path)
    lexicon = Lexicon(schema.values['item'])
    #templates = Templates.from_pickle(args.templates)
    templates = Templates()
    manager = Manager.from_pickle(args.policy)
    analyzer = Analyzer(lexicon)

    # TODO: skip examples
    examples = read_examples(args.transcripts, args.max_examples, Scenario)
    agent = args.agent
    if agent is not None:
        examples = [e for e in examples if agent in e.agents.values()]
    analyzer.example_stats(examples, agent=agent)
    #import sys; sys.exit()

    parsed_dialogues = []
Esempio n. 7
0
def schema():
    schema_path = 'data/negotiation/craigslist-schema.json'
    return Schema(schema_path)