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
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)
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
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 if 'end_survey' not in params.keys(): params['end_survey'] = 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)
def scenarios(schema): scenarios_path = 'data/negotiation/craigslist-scenarios.json' scenario_db = ScenarioDB.from_dict(schema, read_json(scenarios_path)) return scenario_db