Esempio n. 1
0
def main():
    args = argparser()

    args.clip_rewards = False
    env = make_atari(args.env)
    env = wrap_atari_dqn(env, args)

    seed = args.seed + 1122
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    model = DuelingDQN(env)
    model.load_state_dict(torch.load('model.pth', map_location='cpu'))

    episode_reward, episode_length = 0, 0
    state = env.reset()
    while True:
        if args.render:
            env.render()
        action, _ = model.act(torch.FloatTensor(np.array(state)), 0.)
        next_state, reward, done, _ = env.step(action)

        state = next_state
        episode_reward += reward
        episode_length += 1

        if done:
            state = env.reset()
            print("Episode Length / Reward: {} / {}".format(
                episode_length, episode_reward))
            episode_reward = 0
            episode_length = 0
Esempio n. 2
0
def exploration(args, actor_id, param_queue):
    writer = SummaryWriter(comment="-{}-eval".format(args.env))

    args.clip_rewards = False
    args.episode_life = False
    env = make_atari(args.env)
    env = wrap_atari_dqn(env, args)

    seed = args.seed + actor_id
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    model = DuelingDQN(env, args)

    param = param_queue.get(block=True)
    model.load_state_dict(param)
    param = None
    print("Received First Parameter!")

    episode_reward, episode_length, episode_idx = 0, 0, 0
    state = env.reset()
    tb_dict = {k: [] for k in ['episode_reward', 'episode_length']}
    while True:
        action, _ = model.act(torch.FloatTensor(np.array(state)), 0.)
        next_state, reward, done, _ = env.step(action)

        state = next_state
        episode_reward += reward
        episode_length += 1

        if done or episode_length == args.max_episode_length:
            state = env.reset()
            tb_dict["episode_reward"].append(episode_reward)
            tb_dict["episode_length"].append(episode_length)
            episode_reward = 0
            episode_length = 0
            episode_idx += 1
            param = param_queue.get()
            model.load_state_dict(param)
            print(f"{datetime.now()} Updated Parameter..")

            if (episode_idx *
                    args.num_envs_per_worker) % args.tb_interval == 0:
                writer.add_scalar('evaluator/episode_reward_mean',
                                  np.mean(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/episode_reward_max',
                                  np.max(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/episode_reward_min',
                                  np.min(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/episode_reward_std',
                                  np.std(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/episode_length_mean',
                                  np.mean(tb_dict['episode_length']),
                                  episode_idx)
                tb_dict['episode_reward'].clear()
                tb_dict['episode_length'].clear()
Esempio n. 3
0
def exploration(args, actor_id, n_actors, param_queue, send_queue,
                req_param_queue):
    writer = SummaryWriter(comment="-{}-actor{}".format(args.env, actor_id))

    env = make_atari(args.env)
    env = wrap_atari_dqn(env, args)

    seed = args.seed + actor_id
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    model = DuelingDQN(env)
    epsilon = args.eps_base**(1 + actor_id / (n_actors - 1) * args.eps_alpha)
    storage = BatchStorage(args.n_steps, args.gamma)
    req_param_queue.put(True)
    param = param_queue.get(block=True)
    model.load_state_dict(param)
    param = None
    print("Received First Parameter!")

    episode_reward, episode_length, episode_idx, actor_idx = 0, 0, 0, 0
    state = env.reset()
    while True:
        action, q_values = model.act(torch.FloatTensor(np.array(state)),
                                     epsilon)
        next_state, reward, done, _ = env.step(action)
        com_state = zlib.compress(np.array(state).tobytes())
        storage.add(com_state, reward, action, done, q_values)

        state = next_state
        episode_reward += reward
        episode_length += 1
        actor_idx += 1

        if done or episode_length == args.max_episode_length:
            state = env.reset()
            writer.add_scalar("actor/episode_reward", episode_reward,
                              episode_idx)
            writer.add_scalar("actor/episode_length", episode_length,
                              episode_idx)
            episode_reward = 0
            episode_length = 0
            episode_idx += 1

        if actor_idx % args.update_interval == 0:
            try:
                req_param_queue.put(True)
                param = param_queue.get(block=True)
                model.load_state_dict(param)
                print("Updated Parameter..")
            except queue.Empty:
                pass

        if len(storage) == args.send_interval:
            batch, prios = storage.make_batch()
            send_queue.put((batch, prios))
            batch, prios = None, None
            storage.reset()
Esempio n. 4
0
def main():
    learner_ip = get_environ()
    args = argparser()

    writer = SummaryWriter(comment="-{}-eval".format(args.env))

    ctx = zmq.Context()
    param_socket = ctx.socket(zmq.SUB)
    param_socket.setsockopt(zmq.SUBSCRIBE, b'')
    param_socket.setsockopt(zmq.CONFLATE, 1)
    param_socket.connect('tcp://{}:52001'.format(learner_ip))

    env = make_atari(args.env)
    env = wrap_atari_dqn(env, args)

    seed = args.seed + 1122
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    model = DuelingDQN(env)

    data = param_socket.recv(copy=False)
    param = pickle.loads(data)
    model.load_state_dict(param)
    print("Loaded first parameter from learner")

    episode_reward, episode_length, episode_idx = 0, 0, 0
    state = env.reset()
    while True:
        if args.render:
            env.render()
        action, _ = model.act(torch.FloatTensor(np.array(state)), 0.01)
        next_state, reward, done, _ = env.step(action)

        state = next_state
        episode_reward += reward
        episode_length += 1

        if done:
            state = env.reset()
            writer.add_scalar("eval/episode_reward", episode_reward,
                              episode_idx)
            writer.add_scalar("eval/episode_length", episode_length,
                              episode_idx)
            episode_reward = 0
            episode_length = 0
            episode_idx += 1

            if episode_idx % args.eval_update_interval == 0:
                data = param_socket.recv(copy=False)
                param = pickle.loads(data)
                model.load_state_dict(param)
Esempio n. 5
0
def exploration_eval(args, actor_id, param_queue):
    writer = SummaryWriter(comment="-{}-eval".format(args.env))

    args.clip_rewards = False
    env = make_atari(args.env)
    env = wrap_atari_dqn(env, args)

    seed = args.seed + actor_id
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    model = DuelingDQN(env)

    param = param_queue.get(block=True)
    model.load_state_dict(param)
    param = None
    print("Received First Parameter!")

    episode_reward, episode_length, episode_idx = 0, 0, 0
    state = env.reset()
    while True:
        action, _ = model.act(torch.FloatTensor(np.array(state)), 0.)
        next_state, reward, done, _ = env.step(action)

        state = next_state
        episode_reward += reward
        episode_length += 1

        if done or episode_length == args.max_episode_length:
            state = env.reset()
            writer.add_scalar("evaluator/episode_reward", episode_reward,
                              episode_idx)
            writer.add_scalar("evaluator/episode_length", episode_length,
                              episode_idx)
            episode_reward = 0
            episode_length = 0
            episode_idx += 1
            param = param_queue.get()
            model.load_state_dict(param)
            print("Updated Parameter..")
Esempio n. 6
0
class DuelingAgent():
    """Interacts with and learns from the environment."""
    def __init__(self, state_size, action_size, seed):
        """Initialize an Agent object.
        
        Params
        ======
            state_size (int): dimension of each state
            action_size (int): dimension of each action
            seed (int): random seed
        """
        self.state_size = state_size
        self.action_size = action_size
        self.seed = random.seed(seed)

        # Q-Network
        self.qnetwork_local = DuelingDQN(state_size, action_size,
                                         seed).to(device)
        self.qnetwork_target = DuelingDQN(state_size, action_size,
                                          seed).to(device)
        self.optimizer = optim.Adam(self.qnetwork_local.parameters(), lr=LR)

        # Replay memory
        self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, seed)
        # Initialize time step (for updating every UPDATE_EVERY steps)
        self.t_step = 0

    def step(self, state, action, reward, next_state, done):
        # Save experience in replay memory
        self.memory.add(state, action, reward, next_state, done)

        # Learn every UPDATE_EVERY time steps.
        self.t_step = (self.t_step + 1) % UPDATE_EVERY
        if self.t_step == 0:
            # If enough samples are available in memory, get random subset and learn
            if len(self.memory) > BATCH_SIZE:
                experiences = self.memory.sample()
                self.learn(experiences, GAMMA)

    def act(self, state, eps=0.):
        """Returns actions for given state as per current policy.
        
        Params
        ======
            state (array_like): current state
            eps (float): epsilon, for epsilon-greedy action selection
        """
        state = torch.from_numpy(state).float().unsqueeze(0).to(device)
        self.qnetwork_local.eval()
        with torch.no_grad():
            action_values = self.qnetwork_local.act(state)
        self.qnetwork_local.train()

        # Epsilon-greedy action selection
        if random.random() > eps:
            return np.argmax(action_values.cpu().data.numpy())
        else:
            return random.choice(np.arange(self.action_size))

    def learn(self, experiences, gamma):
        """Update value parameters using given batch of experience tuples.
        Params
        ======
            experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples 
            gamma (float): discount factor
        """
        states, actions, rewards, next_states, dones = experiences

        # Get max predicted Q values (for next states) from target model
        Q_targets_next = self.qnetwork_target(next_states).detach().max(
            1)[0].unsqueeze(1)
        # Compute Q targets for current states
        Q_targets = rewards + (gamma * Q_targets_next * (1 - dones))

        # Get expected Q values from local model
        Q_expected = self.qnetwork_local(states).gather(1, actions)

        # Compute loss
        loss = F.mse_loss(Q_expected, Q_targets)
        if random.uniform(0, 1) > 0.99:
            print(loss)
        # Minimize the loss
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        # ------------------- update target network ------------------- #
        self.soft_update(self.qnetwork_local, self.qnetwork_target, TAU)

    def soft_update(self, local_model, target_model, tau):
        """Soft update model parameters.
        θ_target = τ*θ_local + (1 - τ)*θ_target
        Params
        ======
            local_model (PyTorch model): weights will be copied from
            target_model (PyTorch model): weights will be copied to
            tau (float): interpolation parameter 
        """
        for target_param, local_param in zip(target_model.parameters(),
                                             local_model.parameters()):
            target_param.data.copy_(tau * local_param.data +
                                    (1.0 - tau) * target_param.data)
Esempio n. 7
0
class train_DQN():
    def __init__(self, env_id, max_step = 1e5, prior_alpha = 0.6, prior_beta_start = 0.4, 
                    epsilon_start = 1.0, epsilon_final = 0.01, epsilon_decay = 500,
                    batch_size = 32, gamma = 0.99, target_update_interval=1000, save_interval = 1e4,
                    ):
        self.prior_beta_start = prior_beta_start
        self.max_step = int(max_step)
        self.batch_size = batch_size
        self.gamma = gamma
        self.target_update_interval = target_update_interval
        self.save_interval = save_interval


        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.env = gym.make(env_id)
        self.model = DuelingDQN(self.env).to(self.device)
        self.target_model = DuelingDQN(self.env).to(self.device)
        self.target_model.load_state_dict(self.model.state_dict())
        self.replay_buffer = PrioritizedReplayBuffer(100000,alpha=prior_alpha)
        self.optimizer = optim.Adam(self.model.parameters())
        self.writer = SummaryWriter(comment="-{}-learner".format(self.env.unwrapped.spec.id))


        # decay function
        self.scheduler = optim.lr_scheduler.StepLR(self.optimizer,step_size=1000,gamma=0.99)
        self.beta_by_frame = lambda frame_idx: min(1.0, self.prior_beta_start + frame_idx * (1.0 - self.prior_beta_start) / 1000)
        self.epsilon_by_frame = lambda frame_idx: epsilon_final + (epsilon_start - epsilon_final) * math.exp(-1. * frame_idx / epsilon_decay)
        
    def update_target(self,current_model, target_model):
        target_model.load_state_dict(current_model.state_dict())
    def compute_td_loss(self,batch_size, beta):
        state, action, reward, next_state, done, weights, indices  = self.replay_buffer.sample(batch_size, beta) 

        state      = torch.FloatTensor(state).to(self.device)
        next_state = torch.FloatTensor(next_state).to(self.device)
        action     = torch.LongTensor(action).to(self.device)
        reward     = torch.FloatTensor(reward).to(self.device)
        done       = torch.FloatTensor(done).to(self.device)
        weights    = torch.FloatTensor(weights).to(self.device)
        batch = (state, action, reward, next_state, done, weights)

        # q_values      = self.model(state)
        # next_q_values = self.target_model(next_state)

        # q_value          = q_values.gather(1, action.unsqueeze(1)).squeeze(1)
        # next_q_value     = next_q_values.max(1)[0]
        # expected_q_value = reward + self.gamma * next_q_value * (1 - done)
        
        # td_error = torch.abs(expected_q_value.detach() - q_value)
        # loss  = (td_error).pow(2) * weights
        # prios = loss+1e-5#0.9 * torch.max(td_error)+(1-0.9)*td_error
        # loss  = loss.mean()
        loss, prios = utils.compute_loss(self.model,self.target_model, batch,1)
            
        self.optimizer.zero_grad()
        loss.backward()
        self.scheduler.step()
        self.replay_buffer.update_priorities(indices, prios)
        self.optimizer.step()
        return loss    
    def train(self):
        losses = []
        all_rewards = []
        episode_reward = 0
        episode_idx = 0
        episode_length = 0
        state = self.env.reset()
        for frame_idx in range(self.max_step):
            epsilon = self.epsilon_by_frame(frame_idx)
            action,_ = self.model.act(torch.FloatTensor((state)).to(self.device), epsilon)
            next_state, reward, done, _ = self.env.step(action)
            self.replay_buffer.add(state, action, reward, next_state, done)
            
            state = next_state
            episode_reward += reward
            
            episode_length += 1
            if done:
                state = self.env.reset()
                all_rewards.append(episode_reward)
                self.writer.add_scalar("actor/episode_reward", episode_reward, episode_idx)
                self.writer.add_scalar("actor/episode_length", episode_length, episode_idx)
                # print("episode: ",episode_idx, " reward: ", episode_reward)
                episode_reward = 0
                episode_length = 0
                episode_idx += 1
                
            if len(self.replay_buffer) > self.batch_size:
                beta = self.beta_by_frame(frame_idx)
                loss = self.compute_td_loss(self.batch_size, beta)
                losses.append(loss.item())
                self.writer.add_scalar("learner/loss", loss, frame_idx)
                
            if frame_idx % self.target_update_interval == 0:
                print("update target...")
                self.update_target(self.model, self.target_model)

            if frame_idx % self.save_interval == 0 or frame_idx == self.max_step-1:
                print("save model...")
                self.save_model(frame_idx)

            
    def save_model(self, idx):
        torch.save(self.model.state_dict(), "./model{}.pth".format(idx))
    def load_model(self,idx):
         with open("model{}.pth".format(idx), "rb") as f:
                print("loading weights_{}".format(idx))
                self.model.load_state_dict(torch.load(f,map_location="cpu"))
Esempio n. 8
0
def evaluator(args):
    comm = global_dict['comm_world']
    writer = SummaryWriter(log_dir=os.path.join(args['log_dir'], 'eval'))

    args['clip_rewards'] = False
    args['episode_life'] = False
    env = make_atari(args['env'])
    env = wrap_atari_dqn(env, args)

    seed = args['seed'] - 1
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    torch.set_num_threads(1)
    model = DuelingDQN(env, args)

    recv_param_buf = bytearray(100 * 1024 * 1024)
    comm.Send(b'', dest=global_dict['rank_learner'])
    comm.Recv(buf=recv_param_buf, source=global_dict['rank_learner'])
    param = pickle.loads(recv_param_buf)
    model.load_state_dict(param)

    episode_reward, episode_length, episode_idx = 0, 0, 0
    state = env.reset()
    tb_dict = {k: [] for k in ['episode_reward', 'episode_length']}
    while True:
        action, _ = model.act(torch.FloatTensor(np.array(state)), 0.)
        next_state, reward, done, _ = env.step(action)

        state = next_state
        episode_reward += reward
        episode_length += 1

        if done or episode_length == args['max_episode_length']:
            state = env.reset()
            tb_dict["episode_reward"].append(episode_reward)
            tb_dict["episode_length"].append(episode_length)
            episode_reward = 0
            episode_length = 0
            episode_idx += 1
            comm.Send(b'', dest=global_dict['rank_learner'])
            comm.Recv(buf=recv_param_buf, source=global_dict['rank_learner'])
            param = pickle.loads(recv_param_buf)
            model.load_state_dict(param)

            if (episode_idx *
                    args['num_envs_per_worker']) % args['tb_interval'] == 0:
                writer.add_scalar('evaluator/1_episode_reward_mean',
                                  np.mean(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/2_episode_reward_max',
                                  np.max(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/3_episode_reward_min',
                                  np.min(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/4_episode_reward_std',
                                  np.std(tb_dict['episode_reward']),
                                  episode_idx)
                writer.add_scalar('evaluator/5_episode_length_mean',
                                  np.mean(tb_dict['episode_length']),
                                  episode_idx)
                tb_dict['episode_reward'].clear()
                tb_dict['episode_length'].clear()
Esempio n. 9
0
class Worker(mp.Process):
    def __init__(self, worker_id, env_id, seed, epsilon, size, lock,
                n_steps, gamma, send_interval, task_queue, buffer, max_episode_length):
        mp.Process.__init__(self)
        self.worker_id = worker_id
        self.env = gym.make(env_id)
        self.seed = seed
        self.task_queue = task_queue
        self.buffer = buffer
        self.max_episode_length = max_episode_length
        self.send_interval = send_interval
        self.storage = BatchStorage(n_steps, gamma)
        self.size = size
        self.memory = []
        
        self.model = DuelingDQN(self.env)
        # self.writer = SummaryWriter(comment="-{}-actor{}".format(env_id, worker_id))
        self.lock = lock

        self.set_all_seeds()
        self.epsilon = epsilon
    def set_all_seeds(self):
        self.env.seed(self.seed)
        np.random.seed(self.seed)
        random.seed(self.seed)
        torch.manual_seed(self.seed)
        torch.cuda.manual_seed_all(self.seed)
    def update_weights(self, DQN_state_dict):
        self.model.load_state_dict(DQN_state_dict)
    def record_batch(self):

        episode_reward, episode_length, episode_idx, actor_idx = 0, 0, 0, 0
        state = self.env.reset()
        self.storage.reset()
        self.memory = []
        # while actor_idx < self.size:
        while True:
            action, q_values = self.model.act(torch.FloatTensor(np.array(state)), self.epsilon)
            next_state, reward, done, _ = self.env.step(action)
            
            
            self.storage.add(state, reward, action, done, q_values)

            state = next_state
            episode_reward += reward
            episode_length += 1
            actor_idx += 1
            if done or episode_length >= self.max_episode_length:
                state = self.env.reset()
                # self.writer.add_scalar("actor/episode_reward", episode_reward, episode_idx)
                # self.writer.add_scalar("actor/episode_length", episode_length, episode_idx)
                episode_reward = 0
                episode_length = 0
                episode_idx += 1


            if done or len(self.storage) == self.send_interval:
                batch, prios = self.storage.make_batch()
                self.memory.append((*batch, prios))
                # for i in range(len(prios)):
                #     self.buffer.add(batch[0][i],batch[1][i],batch[2][i],batch[3][i],batch[4][i],prios[i])

                batch, prios = None, None
                self.storage.reset()
            if done:
                break
    def run(self):
        while True:
            ########## run loop
            task = self.task_queue.get(block=True)
            if task["desc"] == "record_batch":
                # print("start record batch")
                self.record_batch()
                self.buffer.put(self.memory)
                self.task_queue.task_done()
                # print("record batch done")
            elif task["desc"] == "set_pi_weights":
                # print("set weight")
                self.update_weights(task["pi_state_dict"])
                self.task_queue.task_done()
                # print("set weight done")
            elif task["desc"] == "cleanup":
                # print("clean up")
                self.env.close()
                self.task_queue.task_done()
Esempio n. 10
0
def exploration(args, actor_id, n_actors, replay_ip, param_queue, sample_enque, sample_deque):
    ctx = zmq.Context()
    batch_socket = ctx.socket(zmq.DEALER)
    batch_socket.setsockopt(zmq.IDENTITY, pickle.dumps('actor-{}'.format(actor_id)))
    batch_socket.connect('tcp://{}:51001'.format(replay_ip))

    writer = SummaryWriter(comment="-{}-actor{}".format(args.env, actor_id))

    env = make_atari(args.env)
    env = wrap_atari_dqn(env, args)

    seed = args.seed + actor_id
    utils.set_global_seeds(seed, use_torch=True)
    env.seed(seed)

    model = DuelingDQN(env)
    epsilon = args.eps_base ** (1 + actor_id / (n_actors - 1) * args.eps_alpha)
    storage = BatchStorage(args.n_steps, args.gamma)

    param = param_queue.get(block=True)
    model.load_state_dict(param)
    param = None
    print("Received First Parameter!")

    episode_reward, episode_length, episode_idx, actor_idx = 0, 0, 0, 0
    state = env.reset()
    while True:
        action, q_values = model.act(torch.FloatTensor(np.array(state)), epsilon)
        next_state, reward, done, _ = env.step(action)
        storage.add(state, reward, action, done, q_values)

        state = next_state
        episode_reward += reward
        episode_length += 1
        actor_idx += 1

        if done or episode_length == args.max_episode_length:
            state = env.reset()
            writer.add_scalar("actor/episode_reward", episode_reward, episode_idx)
            writer.add_scalar("actor/episode_length", episode_length, episode_idx)
            episode_reward = 0
            episode_length = 0
            episode_idx += 1

        if actor_idx % args.update_interval == 0:
            try:
                param = param_queue.get(block=False)
                model.load_state_dict(param)
                print("Updated Parameter..")
            except queue.Empty:
                pass

        # get sample batch after each step
        while sample_enque:
            idxes = sample_enque.get()
            sample_deque.put(storage.get_sample_batch(idxes))

        # only pass the prios and get indexes from ReplayBuffer
        if len(storage) == args.send_interval:
            batch, prios = storage.make_batch()
            data = pickle.dumps(prios)
            batch, prios = None, None
            storage.reset()
            batch_socket.send(data, copy=False)
            _, idxes = batch_socket.recv_multipart(copy=False)
            storage.add_batch(batch, idxes)