def main_loop(self): evaluator = Evaluator(stats=False) # Set game to online model. env, args = load_env(os.environ, evaluator=evaluator, overrides=dict( num_games=1, batchsize=1, num_games_per_thread=1, greedy=True, T=1, additional_labels="aug_code,move_idx")) GC = env["game"].initialize() model = env["model_loaders"][0].load_model(GC.params) mi = ModelInterface() mi.add_model("model", model) mi.add_model("actor", model, copy=True, cuda=args.gpu is not None, gpu_id=args.gpu) mi["model"].eval() mi["actor"].eval() self.evaluator = evaluator self.last_move_idx = None def human_actor(batch): print("In human_actor") return self.prompt("DF> ", batch) def actor(batch): return self.actor(batch) def train(batch): self.prompt("DF Train> ", batch) evaluator.setup(sampler=env["sampler"], mi=mi) GC.reg_callback_if_exists("actor", actor) GC.reg_callback_if_exists("human_actor", human_actor) GC.reg_callback_if_exists("train", train) GC.Start() evaluator.episode_start(0) while True: GC.Run() if self.exit: break GC.Stop()
def main_loop(self): evaluator = Evaluator(stats=False) # Set game to online model. env, args = load_env(os.environ, evaluator=evaluator, overrides=dict(num_games=1, batchsize=1, num_games_per_thread=1, greedy=True, T=1, additional_labels="aug_code,move_idx")) GC = env["game"].initialize() model = env["model_loaders"][0].load_model(GC.params) mi = ModelInterface() mi.add_model("model", model) mi.add_model("actor", model, copy=True, cuda=args.gpu is not None, gpu_id=args.gpu) mi["model"].eval() mi["actor"].eval() self.evaluator = evaluator self.last_move_idx = None def human_actor(batch): print("In human_actor") return self.prompt("DF> ", batch) def actor(batch): return self.actor(batch) def train(batch): self.prompt("DF Train> ", batch) evaluator.setup(sampler=env["sampler"], mi=mi) GC.reg_callback_if_exists("actor", actor) GC.reg_callback_if_exists("human_actor", human_actor) GC.reg_callback_if_exists("train", train) GC.Start() evaluator.episode_start(0) while True: GC.Run() if self.exit: break GC.Stop()
model = env["model_loaders"][0].load_model(GC.params) env["mi"].add_model("actor", model, cuda=not args.gpu is None, gpu_id=args.gpu) env["mi"]["actor"].eval() def actor(batch): reply = evaluator.actor(batch) ''' s = batch["s"][0][0] seq = batch["seq"][0][0] for i in range(s.size(0)): print("[seq=%d][c=%d]: %s" % (seq, i, str(s[i]))) print("[seq=%d]: %s" % (seq, str(reply["pi"][0]))) print("[seq=%d]: %s" % (seq, str(reply["a"][0]))) ''' eval_iters.stats.feed_batch(batch) return reply evaluator.setup(sampler=env["sampler"], mi=env["mi"]) GC.reg_callback("actor", actor) GC.Start() evaluator.episode_start(0) for n in eval_iters.iters(): GC.Run() GC.Stop()
model = model_loader.load_model(GC.params) mi = ModelInterface() mi.add_model("model", model, optim_params={ "lr" : 0.001}) mi.add_model("actor", model, copy=True, cuda=True, gpu_id=args.gpu) def actor(batch): reply = evaluator.actor(batch) ''' s = batch["s"][0][0] seq = batch["seq"][0][0] for i in range(s.size(0)): print("[seq=%d][c=%d]: %s" % (seq, i, str(s[i]))) print("[seq=%d]: %s" % (seq, str(reply["pi"][0]))) print("[seq=%d]: %s" % (seq, str(reply["a"][0]))) ''' eval_iters.stats.feed_batch(batch) return reply evaluator.setup(sampler=sampler, mi=mi) GC.reg_callback("actor", actor) GC.Start() evaluator.episode_start(0) for n in eval_iters.iters(): GC.Run() GC.Stop()
GC = env["game"].initialize() model = env["model_loaders"][0].load_model(GC.params) mi = ModelInterface() mi.add_model("model", model, optim_params={"lr": 0.001}) mi.add_model("actor", model, copy=True, cuda=True, gpu_id=args.gpu) def actor(batch): reply = evaluator.actor(batch) ''' s = batch["s"][0][0] seq = batch["seq"][0][0] for i in range(s.size(0)): print("[seq=%d][c=%d]: %s" % (seq, i, str(s[i]))) print("[seq=%d]: %s" % (seq, str(reply["pi"][0]))) print("[seq=%d]: %s" % (seq, str(reply["a"][0]))) ''' eval_iters.stats.feed_batch(batch) return reply evaluator.setup(sampler=env["sampler"], mi=mi) GC.reg_callback("actor", actor) GC.Start() evaluator.episode_start(0) for n in eval_iters.iters(): GC.Run() GC.Stop()
from rlpytorch import load_env, Evaluator, ModelInterface, ArgsProvider, EvalIters if __name__ == '__main__': evaluator = Evaluator(stats=False) # Set game to online model. env, args = load_env(os.environ, evaluator=evaluator, overrides=dict(mode="selfplay", T=1)) GC = env["game"].initialize() model = env["model_loaders"][0].load_model(GC.params) mi = ModelInterface() mi.add_model("model", model) mi.add_model("actor", model, copy=True, cuda=args.gpu is not None, gpu_id=args.gpu) mi["model"].eval() mi["actor"].eval() evaluator.setup(mi=mi) total_batchsize = 0 total_sel_batchsize = 0 def actor(batch): global total_batchsize, total_sel_batchsize reply = evaluator.actor(batch) total_sel_batchsize += batch.batchsize total_batchsize += batch.max_batchsize if total_sel_batchsize >= 5000: print("Batch usage: %d/%d (%.2f%%)" % (total_sel_batchsize, total_batchsize, 100.0 * total_sel_batchsize / total_batchsize)) total_sel_batchsize = 0 total_batchsize = 0
return dict(reduced_s=output["h"].data) def reduced_forward(batch): b = batch.hist(0) output = mi["actor"].transition(b["reduced_s"], b["a"]) return dict(reduced_next_s=output["hf"].data) def reduced_predict(batch): b = batch.hist(0) output = mi["actor"].decision(b["reduced_s"]) return dict(pi=output["pi"].data, V=output["V"].data) def actor(batch): return evaluator.actor(batch) evaluator.setup(mi=mi, sampler=env["sampler"]) if GC.reg_has_callback("actor"): GC.reg_callback("actor", actor) GC.reg_callback("reduced_predict", reduced_predict) GC.reg_callback("reduced_forward", reduced_forward) GC.reg_callback("reduced_project", reduced_project) evaluator.episode_start(0) GC.Start() for n in eval_iters.iters(): GC.Run() GC.Stop()
return dict(reduced_s=output["h"].data) def reduced_forward(batch): b = batch.hist(0) output = mi["actor"].transition(b["reduced_s"], b["a"]) return dict(reduced_next_s=output["hf"].data) def reduced_predict(batch): b = batch.hist(0) output = mi["actor"].decision(b["reduced_s"]) return dict(pi=output["pi"].data, V=output["V"].data) def actor(batch): return evaluator.actor(batch) evaluator.setup(mi=mi, sampler=env["sampler"]) if GC.reg_has_callback("actor"): GC.reg_callback("actor", actor) GC.reg_callback("reduced_predict", reduced_predict) GC.reg_callback("reduced_forward", reduced_forward) GC.reg_callback("reduced_project", reduced_project) evaluator.episode_start(0) GC.Start() for n in eval_iters.iters(): GC.Run() GC.Stop()
GC = env["game"].initialize() model = env["model_loaders"][0].load_model(GC.params) env["mi"].add_model("actor", model, cuda=not args.gpu is None, gpu_id=args.gpu) env["mi"]["actor"].eval() def actor(batch): reply = evaluator.actor(batch) ''' s = batch["s"][0][0] seq = batch["seq"][0][0] for i in range(s.size(0)): print("[seq=%d][c=%d]: %s" % (seq, i, str(s[i]))) print("[seq=%d]: %s" % (seq, str(reply["pi"][0]))) print("[seq=%d]: %s" % (seq, str(reply["a"][0]))) ''' eval_iters.stats.feed_batch(batch) return reply evaluator.setup(sampler=env["sampler"], mi=env["mi"]) GC.reg_callback("actor", actor) GC.Start() evaluator.episode_start(0) for n in eval_iters.iters(): GC.Run() GC.Stop()