https://drive.google.com/file/d/1aBe2xUlWBaujdwvllRa2QQSUeeKLwTSr/view?usp=drive_link
xxxxxxxxxximport os
os.environ['OMP_NUM_THREADS'] = '1'import argparseimport torchfrom src.env import create_train_envfrom src.model import ActorCriticimport torch.nn.functional as F
def get_args(): parser = argparse.ArgumentParser( """Implementation of model described in the paper: Asynchronous Methods for Deep Reinforcement Learning for Super Mario Bros""") parser.add_argument("--world", type=int, default=4) parser.add_argument("--stage", type=int, default=1) parser.add_argument("--action_type", type=str, default="complex") parser.add_argument("--saved_path", type=str, default="trained_models") parser.add_argument("--output_path", type=str, default="output") args = parser.parse_args() return args
def test(opt): torch.manual_seed(123) env, num_states, num_actions = create_train_env(opt.world, opt.stage, opt.action_type, "{}/video_{}_{}.mp4".format(opt.output_path, opt.world, opt.stage)) model = ActorCritic(num_states, num_actions) if torch.cuda.is_available(): model.load_state_dict(torch.load("{}/a3c_super_mario_bros_{}_{}".format(opt.saved_path, opt.world, opt.stage))) model.cuda() else: model.load_state_dict(torch.load("{}/a3c_super_mario_bros_{}_{}".format(opt.saved_path, opt.world, opt.stage), map_location=lambda storage, loc: storage)) model.eval() state = torch.from_numpy(env.reset()) done = True while True: if done: h_0 = torch.zeros((1, 512), dtype=torch.float) c_0 = torch.zeros((1, 512), dtype=torch.float) env.reset() else: h_0 = h_0.detach() c_0 = c_0.detach() if torch.cuda.is_available(): h_0 = h_0.cuda() c_0 = c_0.cuda() state = state.cuda()
logits, value, h_0, c_0 = model(state, h_0, c_0) policy = F.softmax(logits, dim=1) action = torch.argmax(policy).item() action = int(action) state, reward, done, info = env.step(action) state = torch.from_numpy(state) env.render() if info["flag_get"]: print("World {} stage {} completed".format(opt.world, opt.stage)) break
if __name__ == "__main__": opt = get_args() test(opt)
我们非常希望这篇文章能为你提供所需的帮助。你的反馈和建议对我们来说都是宝贵的资源。如果你有任何想法或问题,别犹豫,请在下方评论区留言。同时,如果你觉得这篇文章有用,欢迎分享给你的朋友们。你的参与促使我们前行,期待与你一起学习和成长。
如果这篇文章对您有帮助,也可以赞赏博主喝杯茶~