Exemplo 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
Exemplo 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)
Exemplo n.º 3
0
def get_data_generator(args, model_args, mappings, schema):
    from preprocess import DataGenerator, Preprocessor
    from cocoa.core.scenario_db import ScenarioDB
    from cocoa.core.mutualfriends.lexicon import Lexicon
    from cocoa.core.dataset import read_dataset
    from cocoa.core.util import read_json
    import time

    scenario_db = ScenarioDB.from_dict(schema, read_json(args.scenarios_path))
    dataset = read_dataset(scenario_db, args)
    print 'Building lexicon...'
    start = time.time()
    lexicon = Lexicon(schema, args.learned_lex, stop_words=args.stop_words)
    print '%.2f s' % (time.time() - start)

    # Dataset
    use_kb = False if model_args.model == 'encdec' else True
    copy = True if model_args.model == 'attn-copy-encdec' else False
    if model_args.model == 'attn-copy-encdec':
        model_args.entity_target_form = 'graph'
    preprocessor = Preprocessor(schema, lexicon,
                                model_args.entity_encoding_form,
                                model_args.entity_decoding_form,
                                model_args.entity_target_form,
                                model_args.prepend)
    if args.test:
        model_args.dropout = 0
        data_generator = DataGenerator(None, None, dataset.test_examples,
                                       preprocessor, schema,
                                       model_args.num_items, mappings, use_kb,
                                       copy)
    else:
        data_generator = DataGenerator(dataset.train_examples,
                                       dataset.test_examples, None,
                                       preprocessor, schema,
                                       model_args.num_items, mappings, use_kb,
                                       copy)

    return data_generator
Exemplo n.º 4
0
                        action='store_true',
                        help='Whether or not to have verbose prints')
    parser.add_argument('--valid-scenarios-path',
                        help='Output path for the validation scenarios')
    cocoa.options.add_scenario_arguments(parser)
    options.add_system_arguments(parser)
    options.add_rl_arguments(parser)
    options.add_model_arguments(parser)
    args = parser.parse_args()

    if args.random_seed:
        random.seed(args.random_seed)
        np.random.seed(args.random_seed)

    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)

    assert len(args.agent_checkpoints) <= len(args.agents)
    systems = [
        get_system(name, args, schema, False, args.agent_checkpoints[i])
        for i, name in enumerate(args.agents)
    ]

    rl_agent = 0
    system = systems[rl_agent]
    model = system.env.model
    loss = make_loss(args, model, system.mappings['tgt_vocab'])
    optim = build_optim(args, model, None)
Exemplo n.º 5
0
    scenario_list = []
    price_unit = 1  # Just use the real price
    scenario_generator = generate_scenario(schema, base_price, price_unit,
                                           args.discounts, listings)
    for i, s in enumerate(scenario_generator):
        if i % 100 == 0:
            print i
        if i < args.skip:
            continue
        if len(scenario_list) == args.num_scenarios:
            break
        scenario_list.append(s)
    if len(scenario_list) < args.num_scenarios:
        print 'Not enough listings: {} scenarios generated.'.format(
            len(scenario_list))
    scenario_db = ScenarioDB(scenario_list)
    write_json(scenario_db.to_dict(), args.scenarios_path)

    for i in range(min(10, len(scenario_db.scenarios_list))):
        print '---------------------------------------------------------------------------------------------'
        print '---------------------------------------------------------------------------------------------'
        scenario = scenario_db.scenarios_list[i]
        print "Scenario id: %s" % scenario.uuid
        for agent in (0, 1):
            kb = scenario.kbs[agent]
            kb.dump()

    num_listings_per_category = defaultdict(int)
    for s in scenario_list:
        cat = s.kbs[0].facts['item']['Category']
        num_listings_per_category[cat] += 1
Exemplo n.º 6
0
def scenarios(schema):
    scenarios_path = 'data/negotiation/craigslist-scenarios.json'
    scenario_db = ScenarioDB.from_dict(schema, read_json(scenarios_path))
    return scenario_db