train.py 6.6 KB
Newer Older
F
fuyw 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import cv2
import gym
import os
import threading
import torch
import parl

import numpy as np
from tqdm import tqdm
from parl.utils import tensorboard, logger
from parl.algorithms import DQN, DDQN

from agent import AtariAgent
from atari_wrapper import FireResetEnv, FrameStack, LimitLength, MapState
from model import AtariModel
from replay_memory import ReplayMemory, Experience
from utils import get_player

MEMORY_SIZE = int(1e6)
MEMORY_WARMUP_SIZE = MEMORY_SIZE // 20
IMAGE_SIZE = (84, 84)
CONTEXT_LEN = 4
FRAME_SKIP = 4
UPDATE_FREQ = 4
GAMMA = 0.99


def run_train_episode(env, agent, rpm):
    total_reward = 0
    all_cost = []
    state = env.reset()
    steps = 0
    while True:
        steps += 1
        context = rpm.recent_state()
        context.append(state)
        context = np.stack(context, axis=0)
        action = agent.sample(context)
        next_state, reward, isOver, _ = env.step(action)
        rpm.append(Experience(state, action, reward, isOver))
        if rpm.size() > MEMORY_WARMUP_SIZE:
            if steps % UPDATE_FREQ == 0:
                batch_all_state, batch_action, batch_reward, batch_isOver = rpm.sample_batch(
                    args.batch_size)
                batch_state = batch_all_state[:, :CONTEXT_LEN, :, :]
                batch_next_state = batch_all_state[:, 1:, :, :]
                cost = agent.learn(batch_state, batch_action, batch_reward,
                                   batch_next_state, batch_isOver)
                all_cost.append(cost)
        total_reward += reward
        state = next_state
        if isOver:
            mean_loss = np.mean(all_cost) if all_cost else None
            return total_reward, steps, mean_loss


def run_evaluate_episode(env, agent):
    state = env.reset()
    total_reward = 0
    while True:
        pred_Q = agent.predict(state)
        action = pred_Q.max(1)[1].item()
        state, reward, isOver, _ = env.step(action)
        total_reward += reward
        if isOver:
            return total_reward


def get_fixed_states(rpm, batch_size):
    states = []
    for _ in range(3):
        batch_all_state = rpm.sample_batch(batch_size)[0]
        batch_state = batch_all_state[:, :CONTEXT_LEN, :, :]
        states.append(batch_state)
    fixed_states = np.concatenate(states, axis=0)
    return fixed_states


def evaluate_fixed_Q(agent, states):
    with torch.no_grad():
        max_pred_Q = agent.alg.model(states).max(1)[0].mean()
    return max_pred_Q.item()


def get_grad_norm(model):
    total_norm = 0
    for p in model.parameters():
        if p.grad is not None:
            param_norm = p.grad.data.norm(2)
            total_norm += param_norm.item()**2
    total_norm = total_norm**(1. / 2)
    return total_norm


def main():
    env = get_player(
        args.rom, image_size=IMAGE_SIZE, train=True, frame_skip=FRAME_SKIP)
    test_env = get_player(
        args.rom,
        image_size=IMAGE_SIZE,
        frame_skip=FRAME_SKIP,
        context_len=CONTEXT_LEN)
    rpm = ReplayMemory(MEMORY_SIZE, IMAGE_SIZE, CONTEXT_LEN)
    act_dim = env.action_space.n
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = AtariModel(CONTEXT_LEN, act_dim, args.algo)
    if args.algo in ['DQN', 'Dueling']:
        algorithm = DQN(model, gamma=GAMMA, lr=args.lr)
    elif args.algo is 'Double':
        algorithm = DDQN(model, gamma=GAMMA, lr=args.lr)
    agent = AtariAgent(algorithm, act_dim=act_dim)

    with tqdm(
            total=MEMORY_WARMUP_SIZE, desc='[Replay Memory Warm Up]') as pbar:
        while rpm.size() < MEMORY_WARMUP_SIZE:
            total_reward, steps, _ = run_train_episode(env, agent, rpm)
            pbar.update(steps)

    # Get fixed states to check value function.
    fixed_states = get_fixed_states(rpm, args.batch_size)
    fixed_states = torch.tensor(fixed_states, dtype=torch.float, device=device)

    # train
    test_flag = 0
    total_steps = 0

    with tqdm(total=args.train_total_steps, desc='[Training Model]') as pbar:
        while total_steps < args.train_total_steps:
            total_reward, steps, loss = run_train_episode(env, agent, rpm)
            total_steps += steps
            pbar.update(steps)
            if total_steps // args.test_every_steps >= test_flag:
                while total_steps // args.test_every_steps >= test_flag:
                    test_flag += 1

                eval_rewards = []
                for _ in range(3):
                    eval_rewards.append(run_evaluate_episode(test_env, agent))

                tensorboard.add_scalar('dqn/eval', np.mean(eval_rewards),
                                       total_steps)
                tensorboard.add_scalar('dqn/score', total_reward, total_steps)
                tensorboard.add_scalar('dqn/loss', loss, total_steps)
                tensorboard.add_scalar('dqn/exploration', agent.exploration,
                                       total_steps)
                tensorboard.add_scalar('dqn/Q value',
                                       evaluate_fixed_Q(agent, fixed_states),
                                       total_steps)
                tensorboard.add_scalar('dqn/grad_norm',
                                       get_grad_norm(agent.alg.model),
                                       total_steps)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--rom', default='rom_files/breakout.bin')
    parser.add_argument(
        '--batch_size', type=int, default=32, help='batch size for training')
    parser.add_argument('--lr', default=3e-4, help='learning_rate')
    parser.add_argument('--algo', default='DQN', help='DQN/Double/Dueling DQN')
    parser.add_argument(
        '--train_total_steps',
        type=int,
        default=int(1e7),
        help='maximum environmental steps of games')
    parser.add_argument(
        '--test_every_steps',
        type=int,
        default=int(1e5),
        help='the step interval between two consecutive evaluations')
    args = parser.parse_args()
    rom_name = args.rom.split('/')[-1].split('.')[0]
    logger.set_dir(os.path.join('./train_log', rom_name))
    main()