Exemplo n.º 1
0
def train(rank,
          args,
          shared_model,
          counter,
          lock,
          optimizer=None,
          select_sample=True):
    torch.manual_seed(args.seed + rank)

    print("Process No : {} | Sampling : {}".format(rank, select_sample))

    FloatTensor = torch.cuda.FloatTensor if args.use_cuda else torch.FloatTensor
    DoubleTensor = torch.cuda.DoubleTensor if args.use_cuda else torch.DoubleTensor
    ByteTensor = torch.cuda.ByteTensor if args.use_cuda else torch.ByteTensor

    env = create_mario_env(args.env_name)
    env.seed(args.seed + rank)

    model = ActorCritic(env.observation_space.shape[0], len(ACTIONS))
    if args.use_cuda:
        model.cuda()
    if optimizer is None:
        optimizer = optim.Adam(shared_model.parameters(), lr=args.lr)

    model.train()

    state = env.reset()
    state = torch.from_numpy(state)
    done = True

    episode_length = 0
    for num_iter in count():

        if rank == 0:
            env.render()

            if num_iter % args.save_interval == 0 and num_iter > 0:
                print("Saving model at :" + args.save_path)
                torch.save(shared_model.state_dict(), args.save_path)

        if num_iter % (
                args.save_interval * 2.5
        ) == 0 and num_iter > 0 and rank == 1:  # Second saver in-case first processes crashes
            print("Saving model for process 1 at :" + args.save_path)
            torch.save(shared_model.state_dict(), args.save_path)

        # Sync with the shared model
        model.load_state_dict(shared_model.state_dict())
        if done:
            cx = Variable(torch.zeros(1, 512)).type(FloatTensor)
            hx = Variable(torch.zeros(1, 512)).type(FloatTensor)
        else:
            cx = Variable(cx.data).type(FloatTensor)
            hx = Variable(hx.data).type(FloatTensor)

        values = []
        log_probs = []
        rewards = []
        entropies = []
        reason = ''

        for step in range(args.num_steps):
            episode_length += 1
            state_inp = Variable(state.unsqueeze(0)).type(FloatTensor)
            value, logit, (hx, cx) = model((state_inp, (hx, cx)))
            prob = F.softmax(logit, dim=-1)
            log_prob = F.log_softmax(logit, dim=-1)
            entropy = -(log_prob * prob).sum(-1, keepdim=True)
            entropies.append(entropy)

            if select_sample:
                action = prob.multinomial().data
            else:
                action = prob.max(-1, keepdim=True)[1].data

            log_prob = log_prob.gather(-1, Variable(action))

            action_out = ACTIONS[action][0, 0]

            # print("Process: {} Action: {}".format(rank,  str(action_out)))

            state, reward, done, _ = env.step(action_out)

            done = done or episode_length >= args.max_episode_length
            reward = max(min(reward, 50), -50)

            with lock:
                counter.value += 1

            if done:
                episode_length = 0
                env.change_level(0)
                state = env.reset()
                print("Process {} has completed.".format(rank))

            env.locked_levels = [False] + [True] * 31
            state = torch.from_numpy(state)
            values.append(value)
            log_probs.append(log_prob)
            rewards.append(reward)

            if done:
                break

        R = torch.zeros(1, 1)
        if not done:
            state_inp = Variable(state.unsqueeze(0)).type(FloatTensor)
            value, _, _ = model((state_inp, (hx, cx)))
            R = value.data

        values.append(Variable(R).type(FloatTensor))
        policy_loss = 0
        value_loss = 0
        R = Variable(R).type(FloatTensor)
        gae = torch.zeros(1, 1).type(FloatTensor)
        for i in reversed(range(len(rewards))):
            R = args.gamma * R + rewards[i]
            advantage = R - values[i]
            value_loss = value_loss + 0.5 * advantage.pow(2)

            # Generalized Advantage Estimataion
            delta_t = rewards[i] + args.gamma * \
                values[i + 1].data - values[i].data
            gae = gae * args.gamma * args.tau + delta_t

            policy_loss = policy_loss - \
                log_probs[i] * Variable(gae).type(FloatTensor) - args.entropy_coef * entropies[i]

        total_loss = policy_loss + args.value_loss_coef * value_loss

        print("Process {} loss :".format(rank), total_loss.data)
        # print("Process: {} Episode: {}".format(rank,  str(episode_length)))
        optimizer.zero_grad()

        (total_loss).backward()
        torch.nn.utils.clip_grad_norm(model.parameters(), args.max_grad_norm)

        ensure_shared_grads(model, shared_model)
        optimizer.step()
    print("Process {} closed.".format(rank))
    SAVEPATH = os.getcwd(
    ) + '/save/scme_' + args.reward_type + '/mario_a3c_params.pkl'
    if not os.path.exists(os.getcwd() + '/save/scme_' + args.reward_type):
        os.makedirs(os.getcwd() + '/save/scme_' + args.reward_type)

    env = create_mario_env(args.env_name, args.reward_type)

    shared_model = ActorCritic(env.observation_space.shape[0], len(ACTIONS))
    shared_model.share_memory()

    shared_scme = SCME(env.observation_space.shape[0], len(ACTIONS))
    shared_scme.share_memory()

    if os.path.isfile(SAVEPATH):
        print('Loading A3C parametets & SCME parameters...')
        shared_model.load_state_dict(torch.load(SAVEPATH))
        shared_scme.load_state_dict(torch.load(SAVEPATH[:-4] + '_scme.pkl'))

    torch.manual_seed(args.seed)

    #optimizer = torch.optim.Adam(list(shared_model.parameters()) + list(shared_scme.parameters()), lr=args.lr)
    optimizer = SharedAdam(list(shared_model.parameters()) +
                           list(shared_scme.parameters()),
                           lr=args.lr)
    optimizer.share_memory()

    #     optimizer_icm = SharedAdam(shared_icm.parameters(), lr=args.lr)
    #     optimizer_icm.share_memory()

    print("No of available cores : {}".format(mp.cpu_count()))
Exemplo n.º 3
0
def test(rank, args, shared_model, counter):
    torch.manual_seed(args.seed + rank)

    FloatTensor = torch.cuda.FloatTensor if args.use_cuda else torch.FloatTensor
    DoubleTensor = torch.cuda.DoubleTensor if args.use_cuda else torch.DoubleTensor
    ByteTensor = torch.cuda.ByteTensor if args.use_cuda else torch.ByteTensor

    env = create_mario_env(args.env_name)
    """ 
        need to implement Monitor wrapper with env.change_level
    """
    # expt_dir = 'video'
    # env = wrappers.Monitor(env, expt_dir, force=True, video_callable=lambda count: count % 10 == 0)

    env.seed(args.seed + rank)

    model = ActorCritic(env.observation_space.shape[0], len(ACTIONS))
    if args.use_cuda:
        model.cuda()
    model.eval()

    state = env.reset()
    state = torch.from_numpy(state)
    reward_sum = 0
    done = True
    savefile = os.getcwd() + '/save/mario_curves.csv'

    title = ['Time', 'No. Steps', 'Total Reward', 'Episode Length']
    with open(savefile, 'a', newline='') as sfile:
        writer = csv.writer(sfile)
        writer.writerow(title)

    start_time = time.time()

    # a quick hack to prevent the agent from stucking
    actions = deque(maxlen=4000)
    episode_length = 0
    while True:
        episode_length += 1
        ep_start_time = time.time()
        # Sync with the shared model
        if done:
            model.load_state_dict(shared_model.state_dict())
            cx = Variable(torch.zeros(1, 512), volatile=True).type(FloatTensor)
            hx = Variable(torch.zeros(1, 512), volatile=True).type(FloatTensor)

        else:
            cx = Variable(cx.data, volatile=True).type(FloatTensor)
            hx = Variable(hx.data, volatile=True).type(FloatTensor)

        state_inp = Variable(state.unsqueeze(0),
                             volatile=True).type(FloatTensor)
        value, logit, (hx, cx) = model((state_inp, (hx, cx)))
        prob = F.softmax(logit, dim=-1)
        action = prob.max(-1, keepdim=True)[1].data

        action_out = ACTIONS[action][0, 0]
        # print("Process: Test Action: {}".format(str(action_out)))

        state, reward, done, _ = env.step(action_out)
        env.render()
        done = done or episode_length >= args.max_episode_length
        reward_sum += reward

        # a quick hack to prevent the agent from stucking
        actions.append(action[0, 0])
        if actions.count(actions[0]) == actions.maxlen:
            done = True

        if done:
            print(
                "Time {}, num steps {}, FPS {:.0f}, episode reward {}, episode length {}"
                .format(
                    time.strftime("%Hh %Mm %Ss",
                                  time.gmtime(time.time() - start_time)),
                    counter.value, counter.value / (time.time() - start_time),
                    reward_sum, episode_length))

            data = [
                time.time() - ep_start_time, counter.value, reward_sum,
                episode_length
            ]

            with open(savefile, 'a', newline='') as sfile:
                writer = csv.writer(sfile)
                writer.writerows([data])

            reward_sum = 0
            episode_length = 0
            actions.clear()
            time.sleep(60)
            env.locked_levels = [False] + [True] * 31
            env.change_level(0)
            state = env.reset()

        state = torch.from_numpy(state)
Exemplo n.º 4
0
def train(rank, args, shared_model, shared_scme, counter, lock, optimizer=None, select_sample=True):
    torch.manual_seed(args.seed + rank)

    print("Process No : {} | Sampling : {}".format(rank, select_sample))

    FloatTensor = torch.FloatTensor# torch.cuda.FloatTensor if args.use_cuda else torch.FloatTensor
    DoubleTensor = torch.DoubleTensor# torch.cuda.DoubleTensor if args.use_cuda else torch.DoubleTensor
    ByteTensor = torch.ByteTensor# torch.cuda.ByteTensor if args.use_cuda else torch.ByteTensor

    savefile = os.getcwd() + '/save/scmemi_'+ args.reward_type +'/train_reward.csv'
    saveweights = os.getcwd() + '/save/scmemi_'+ args.reward_type +'/mario_a3c_params.pkl'

    env = create_mario_env(args.env_name, args.reward_type)
    #env.seed(args.seed + rank)

    model = ActorCritic(env.observation_space.shape[0], len(ACTIONS))
    if optimizer is None:
        optimizer = optim.Adam(list(shared_model.parameters()) + list(shared_scme.parameters()), lr=args.lr)
        
    scme_model = SCME(env.observation_space.shape[0], len(ACTIONS))
    
    model.train()
    scme_model.train()

    state = env.reset()
    cum_rew = 0 
    state = torch.from_numpy(state)
    done = True
    
    episode_length = 0
    for num_iter in count():
        #env.render()
        if rank == 0:
            
            if num_iter % args.save_interval == 0 and num_iter > 0:
                print ("Saving model at :" + saveweights)            
                torch.save(shared_model.state_dict(), saveweights)
                torch.save(shared_scme.state_dict(), saveweights[:-4] + '_scme.pkl')

        if num_iter % (args.save_interval * 2.5) == 0 and num_iter > 0 and rank == 1:    # Second saver in-case first processes crashes 
            print ("Saving model for process 1 at :" + saveweights)            
            torch.save(shared_model.state_dict(), saveweights)
            torch.save(shared_scme.state_dict(), saveweights[:-4] + '_scme.pkl')
            
        # Sync with the shared model
        model.load_state_dict(shared_model.state_dict())
        scme_model.load_state_dict(shared_scme.state_dict())
        
        if done:
            cx = Variable(torch.zeros(1, 512)).type(FloatTensor)
            hx = Variable(torch.zeros(1, 512)).type(FloatTensor)
        else:
            cx = Variable(cx.data).type(FloatTensor)
            hx = Variable(hx.data).type(FloatTensor)

        values = []
        log_probs = []
        rewards = []
        entropies = []
        vae_losses = []
        cur_losses = []
        mi_losses = []
        #reason =''
        
        for step in range(args.num_steps):
            episode_length += 1            
            state_inp = Variable(state.unsqueeze(0)).type(FloatTensor)
            value, logit, (hx, cx) = model((state_inp, (hx, cx)))
            prob = F.softmax(logit, dim=-1)
            log_prob = F.log_softmax(logit, dim=-1)
            entropy = -(log_prob * prob).sum(-1, keepdim=True)
            entropies.append(entropy)
            
            
            if select_sample:
                action = prob.multinomial(1).data
            else:
                action = prob.max(-1, keepdim=True)[1].data
                
            log_prob = log_prob.gather(-1, Variable(action))
            
            action_out = int(action[0, 0].data.numpy())
            state, reward, done, info = env.step(action_out)
            cum_rew = cum_rew + reward
            
            action_one_hot = (torch.eye(len(ACTIONS))[action_out]).view(1,-1)
            
            next_state_inp = Variable(torch.from_numpy(state).unsqueeze(0)).type(FloatTensor)

            pred_z, mi, mi1, actual_z, xt1_hat, xt1, xt1_mu, xt1_logvar = scme_model((state_inp, next_state_inp, action_one_hot))
            vae_loss = loss_function(xt1_hat, xt1, xt1_mu, xt1_logvar)
            cur_loss = ((pred_z - actual_z).pow(2)).sum(-1, keepdim=True)/2/50
            mi_loss = mutual(mi, mi1).sum(-1, keepdim=True)/10
            done = done or episode_length >= args.max_episode_length
            
            cur_reward = (args.alpha*cur_loss).data.numpy()[0,0]
            mi_reward = (args.beta*mi_loss).data.numpy()
            reward = cur_reward + reward + mi_reward
            reward = max(min(reward, 50), -5)
            
            
            with lock:
                counter.value += 1

            if done:
                episode_length = 0
#                 env.change_level(0)
                state = env.reset()
                with open(savefile[:-4]+'_{}.csv'.format(rank), 'a', newline='') as sfile:
                    writer = csv.writer(sfile)
                    writer.writerows([[cum_rew, info['x_pos']/x_norm]])
                cum_rew = 0 
 #               print ("Process {} has completed.".format(rank))

#            env.locked_levels = [False] + [True] * 31
            state = torch.from_numpy(state)
            values.append(value)
            log_probs.append(log_prob)
            rewards.append(reward)
            vae_losses.append(vae_loss)
            cur_losses.append(cur_loss)
            mi_losses.append(mi_loss)
            
            
            if done:
                break
        R = torch.zeros(1, 1)
        if not done:
            state_inp = Variable(state.unsqueeze(0)).type(FloatTensor)
            value, _, _ = model((state_inp, (hx, cx)))
            R = value.data

        values.append(Variable(R).type(FloatTensor))
        policy_loss = 0
        value_loss = 0
        scme_loss = 0
        R = Variable(R).type(FloatTensor)
        gae = torch.zeros(1, 1).type(FloatTensor)
        for i in reversed(range(len(rewards))):
            R = args.gamma * R + rewards[i]
            advantage = R - values[i]
            value_loss = value_loss + 0.5 * advantage.pow(2)

            # Generalized Advantage Estimataion
            delta_t = rewards[i] + args.gamma * values[i + 1].data - values[i].data
            gae = gae * args.gamma * args.tau + delta_t

            policy_loss = policy_loss - log_probs[i] * Variable(gae).type(FloatTensor) - args.entropy_coef * entropies[i]
            
            scme_loss = 0.01*vae_losses[i] + cur_losses[i] - mi_losses[i]
        total_loss = args.lambd*(policy_loss + args.value_loss_coef * value_loss)
        
#        print ("Process {} loss :".format(rank), total_loss.data)
        optimizer.zero_grad()
#         cur_optimizer.zero_grad()
        
        (total_loss + scme_loss).backward()
#         (curiosity_loss).backward()
        
        torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
        torch.nn.utils.clip_grad_norm_(scme_model.parameters(), args.max_grad_norm)

        ensure_shared_grads(model, shared_model)
        ensure_shared_grads(scme_model, shared_scme)
        
        optimizer.step()
Exemplo n.º 5
0
def test(rank, args, shared_model, counter):
    torch.manual_seed(args.seed + rank)

    FloatTensor = torch.FloatTensor# torch.cuda.FloatTensor if args.use_cuda else torch.FloatTensor
    DoubleTensor = torch.DoubleTensor# torch.cuda.DoubleTensor if args.use_cuda else torch.DoubleTensor
    ByteTensor = torch.ByteTensor# torch.cuda.ByteTensor if args.use_cuda else torch.ByteTensor

    env = create_mario_env(args.env_name, args.reward_type)
    """ 
        need to implement Monitor wrapper with env.change_level
    """
    # expt_dir = 'video'
    # env = wrappers.Monitor(env, expt_dir, force=True, video_callable=lambda count: count % 10 == 0)
    
    #env.seed(args.seed + rank)

    model = ActorCritic(env.observation_space.shape[0], len(ACTIONS))
    model.eval()

    state = env.reset()
    state = torch.from_numpy(state)
    reward_sum = 0
    done = True
    savefile = os.getcwd() + '/save/scmemi_'+ args.reward_type +'/mario_curves.csv'
    
    title = ['Time','No. Steps', 'Total Reward', 'final_position', 'Episode Length']
    with open(savefile, 'a', newline='') as sfile:
        writer = csv.writer(sfile)
        writer.writerow(title)    

    start_time = time.time()

    # a quick hack to prevent the agent from stucking
    actions = deque(maxlen=400)
    positions = deque(maxlen=400)
    episode_length = 0
    while True:
        episode_length += 1
        ep_start_time = time.time()
        # Sync with the shared model
        if done:
            model.load_state_dict(shared_model.state_dict())
            cx = Variable(torch.zeros(1, 512),  requires_grad=True ).type(FloatTensor)
            with torch.no_grad():
                cx=cx
            hx = Variable(torch.zeros(1, 512),  requires_grad=True).type(FloatTensor)
            with torch.no_grad():
                hx=hx

        else:
            with torch.no_grad():
                cx = Variable(cx.data).type(FloatTensor)
                hx = Variable(hx.data).type(FloatTensor)
        

        with torch.no_grad(): state_inp = Variable(state.unsqueeze(0)).type(FloatTensor)
        value, logit, (hx, cx) = model((state_inp, (hx, cx)))
        prob = F.softmax(logit, dim=-1)
        action = prob.max(-1, keepdim=True)[1].data
        action_out = int(action[0, 0].data.numpy())
        state, reward, done, info = env.step(action_out)
        #env.render()
        done = done or episode_length >= args.max_episode_length
        reward_sum += reward

        # a quick hack to prevent the agent from stucking
        actions.append(action[0, 0])
        if actions.count(actions[0]) == actions.maxlen:
            done = True
            print('action')
        if args.pos_stuck :
            positions.append(info['x_pos'])
            pos_ar = np.array(positions)
            if (len(positions) >= 200) and (pos_ar < pos_ar[-1] + 20).all() and (pos_ar > pos_ar[-1] - 20).all():
                done = True

        if done:
            print("Time {}, num steps {}, FPS {:.0f}, episode reward {:.3f}, distance covered {:.3f}, episode length {}".format(
                time.strftime("%Hh %Mm %Ss",
                              time.gmtime(time.time() - start_time)), 
                counter.value, counter.value / (time.time() - start_time),
                reward_sum, info['x_pos']/x_norm, episode_length))
            
            data = [time.time() - ep_start_time,
                    counter.value, reward_sum, info['x_pos']/x_norm, episode_length]
            
            with open(savefile, 'a', newline='') as sfile:
                writer = csv.writer(sfile)
                writer.writerows([data])
            
            reward_sum = 0
            episode_length = 0
            actions.clear()
            positions.clear()
            time.sleep(60)
#             env.locked_levels = [False] + [True] * 31
#             env.change_level(0)
            state = env.reset()

        state = torch.from_numpy(state)
Exemplo n.º 6
0
if __name__ == '__main__':

	os.environ['OMP_NUM_THREADS'] = '1'

	args = parser.parse_args()
	env = create_mario_env(args.env_name)

	shared_model = ActorCritic( env.observation_space.shape[0], len(COMPLEX_MOVEMENT))
	if args.use_cuda:
		shared_model.cuda()

	shared_model.share_memory()

	if os.path.isfile(args.save_path):
		print('Loading A3C parametets ...')
		shared_model.load_state_dict(torch.load(args.save_path, map_location='cpu'))

	torch.manual_seed(args.seed)

	optimizer = SharedAdam(shared_model.parameters(), lr=args.lr)
	optimizer.share_memory()

	print (color.BLUE + "No of available cores : {}".format(mp.cpu_count()) + color.END)

	processes = []

	counter = mp.Value('i', 0)
	lock = mp.Lock()

	#test(0, args, shared_model, counter)
Exemplo n.º 7
0
if __name__ == '__main__':
    os.environ['OMP_NUM_THREADS'] = '1'

    args = parser.parse_args()
    env = create_mario_env(args.env_name)

    shared_model = ActorCritic(env.observation_space.shape[0], len(ACTIONS))
    if args.use_cuda:
        shared_model.cuda()

    shared_model.share_memory()

    if os.path.isfile(args.save_path):
        print('Loading A3C parametets ...')
        shared_model.load_state_dict(torch.load(args.save_path))

    torch.manual_seed(args.seed)

    optimizer = SharedAdam(shared_model.parameters(), lr=args.lr)
    optimizer.share_memory()

    print(color.BLUE + "No of available cores : {}".format(mp.cpu_count()) +
          color.END)

    processes = []

    counter = mp.Value('i', 0)
    lock = mp.Lock()

    p = mp.Process(target=test,