2023-03-23 14:05:28 +00:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import gym
|
|
|
|
import time
|
|
|
|
import json
|
|
|
|
import dmc2gym
|
|
|
|
|
2023-04-12 15:15:17 +00:00
|
|
|
import copy
|
2023-03-27 17:23:42 +00:00
|
|
|
import tqdm
|
2023-03-23 14:05:28 +00:00
|
|
|
import wandb
|
|
|
|
import utils
|
2023-04-12 15:15:17 +00:00
|
|
|
from utils import ReplayBuffer, FreezeParameters, make_env, preprocess_obs, soft_update_params, save_image
|
2023-04-10 18:18:17 +00:00
|
|
|
from models import ObservationEncoder, ObservationDecoder, TransitionModel, Actor, ValueModel, RewardModel, ProjectionHead, ContrastiveHead, CLUBSample
|
2023-03-23 14:05:28 +00:00
|
|
|
from logger import Logger
|
|
|
|
from video import VideoRecorder
|
2023-03-25 16:07:07 +00:00
|
|
|
from dmc2gym.wrappers import set_global_var
|
2023-03-23 14:05:28 +00:00
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
import torch.nn as nn
|
|
|
|
import torch.nn.functional as F
|
2023-04-09 16:22:41 +00:00
|
|
|
import torchvision.transforms as T
|
2023-04-12 15:15:17 +00:00
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
2023-04-09 16:22:41 +00:00
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
#from agent.baseline_agent import BaselineAgent
|
|
|
|
#from agent.bisim_agent import BisimAgent
|
|
|
|
#from agent.deepmdp_agent import DeepMDPAgent
|
|
|
|
#from agents.navigation.carla_env import CarlaEnv
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
# environment
|
|
|
|
parser.add_argument('--domain_name', default='cheetah')
|
2023-03-28 18:21:26 +00:00
|
|
|
parser.add_argument('--version', default=1, type=int)
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--task_name', default='run')
|
|
|
|
parser.add_argument('--image_size', default=84, type=int)
|
|
|
|
parser.add_argument('--channels', default=3, type=int)
|
|
|
|
parser.add_argument('--action_repeat', default=1, type=int)
|
2023-04-09 16:22:41 +00:00
|
|
|
parser.add_argument('--frame_stack', default=3, type=int)
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--resource_files', type=str)
|
|
|
|
parser.add_argument('--eval_resource_files', type=str)
|
|
|
|
parser.add_argument('--img_source', default=None, type=str, choices=['color', 'noise', 'images', 'video', 'none'])
|
2023-03-27 17:23:42 +00:00
|
|
|
parser.add_argument('--total_frames', default=1000, type=int) # 10000
|
2023-03-25 16:07:07 +00:00
|
|
|
parser.add_argument('--high_noise', action='store_true')
|
2023-03-23 14:05:28 +00:00
|
|
|
# replay buffer
|
2023-03-27 17:23:42 +00:00
|
|
|
parser.add_argument('--replay_buffer_capacity', default=50000, type=int) #50000
|
2023-04-09 16:22:41 +00:00
|
|
|
parser.add_argument('--episode_length', default=51, type=int)
|
2023-03-23 14:05:28 +00:00
|
|
|
# train
|
|
|
|
parser.add_argument('--agent', default='dpi', type=str, choices=['baseline', 'bisim', 'deepmdp', 'db', 'dpi', 'rpc'])
|
2023-04-09 16:22:41 +00:00
|
|
|
parser.add_argument('--init_steps', default=10000, type=int)
|
|
|
|
parser.add_argument('--num_train_steps', default=10000, type=int)
|
|
|
|
parser.add_argument('--batch_size', default=20, type=int) #512
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--state_size', default=256, type=int)
|
|
|
|
parser.add_argument('--hidden_size', default=128, type=int)
|
|
|
|
parser.add_argument('--history_size', default=128, type=int)
|
2023-03-31 16:00:07 +00:00
|
|
|
parser.add_argument('--num-units', type=int, default=200, help='num hidden units for reward/value/discount models')
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--load_encoder', default=None, type=str)
|
2023-04-09 16:22:41 +00:00
|
|
|
parser.add_argument('--imagine_horizon', default=15, type=str)
|
2023-04-10 18:18:17 +00:00
|
|
|
parser.add_argument('--grad_clip_norm', type=float, default=100.0, help='Gradient clipping norm')
|
2023-03-23 14:05:28 +00:00
|
|
|
# eval
|
|
|
|
parser.add_argument('--eval_freq', default=10, type=int) # TODO: master had 10000
|
|
|
|
parser.add_argument('--num_eval_episodes', default=20, type=int)
|
2023-04-10 18:18:17 +00:00
|
|
|
# value
|
|
|
|
parser.add_argument('--value_lr', default=1e-4, type=float)
|
|
|
|
parser.add_argument('--value_beta', default=0.9, type=float)
|
|
|
|
parser.add_argument('--value_tau', default=0.005, type=float)
|
2023-04-12 15:15:17 +00:00
|
|
|
parser.add_argument('--value_target_update_freq', default=100, type=int)
|
2023-04-12 07:33:42 +00:00
|
|
|
parser.add_argument('--td_lambda', default=0.95, type=int)
|
2023-04-10 18:18:17 +00:00
|
|
|
# reward
|
|
|
|
parser.add_argument('--reward_lr', default=1e-4, type=float)
|
2023-03-23 14:05:28 +00:00
|
|
|
# actor
|
2023-04-10 18:18:17 +00:00
|
|
|
parser.add_argument('--actor_lr', default=1e-4, type=float)
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--actor_beta', default=0.9, type=float)
|
|
|
|
parser.add_argument('--actor_log_std_min', default=-10, type=float)
|
|
|
|
parser.add_argument('--actor_log_std_max', default=2, type=float)
|
|
|
|
parser.add_argument('--actor_update_freq', default=2, type=int)
|
2023-04-10 18:18:17 +00:00
|
|
|
# world/encoder/decoder
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--encoder_type', default='pixel', type=str, choices=['pixel', 'pixelCarla096', 'pixelCarla098', 'identity'])
|
|
|
|
parser.add_argument('--encoder_feature_dim', default=50, type=int)
|
2023-04-10 18:18:17 +00:00
|
|
|
parser.add_argument('--world_model_lr', default=1e-3, type=float)
|
2023-04-12 07:33:42 +00:00
|
|
|
parser.add_argument('--past_transition_lr', default=1e-3, type=float)
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--encoder_lr', default=1e-3, type=float)
|
2023-04-12 15:15:17 +00:00
|
|
|
parser.add_argument('--encoder_tau', default=0.001, type=float)
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--encoder_stride', default=1, type=int)
|
|
|
|
parser.add_argument('--decoder_type', default='pixel', type=str, choices=['pixel', 'identity', 'contrastive', 'reward', 'inverse', 'reconstruction'])
|
|
|
|
parser.add_argument('--decoder_lr', default=1e-3, type=float)
|
|
|
|
parser.add_argument('--decoder_update_freq', default=1, type=int)
|
|
|
|
parser.add_argument('--decoder_weight_lambda', default=0.0, type=float)
|
|
|
|
parser.add_argument('--num_layers', default=4, type=int)
|
|
|
|
parser.add_argument('--num_filters', default=32, type=int)
|
2023-04-10 18:18:17 +00:00
|
|
|
parser.add_argument('--aug', action='store_true')
|
2023-03-23 14:05:28 +00:00
|
|
|
# sac
|
|
|
|
parser.add_argument('--discount', default=0.99, type=float)
|
|
|
|
parser.add_argument('--init_temperature', default=0.01, type=float)
|
|
|
|
parser.add_argument('--alpha_lr', default=1e-3, type=float)
|
|
|
|
parser.add_argument('--alpha_beta', default=0.9, type=float)
|
|
|
|
# misc
|
|
|
|
parser.add_argument('--seed', default=1, type=int)
|
2023-04-12 07:33:42 +00:00
|
|
|
parser.add_argument('--logging_freq', default=100, type=int)
|
2023-03-23 14:05:28 +00:00
|
|
|
parser.add_argument('--work_dir', default='.', type=str)
|
|
|
|
parser.add_argument('--save_tb', default=False, action='store_true')
|
|
|
|
parser.add_argument('--save_model', default=False, action='store_true')
|
|
|
|
parser.add_argument('--save_buffer', default=False, action='store_true')
|
|
|
|
parser.add_argument('--save_video', default=False, action='store_true')
|
|
|
|
parser.add_argument('--transition_model_type', default='', type=str, choices=['', 'deterministic', 'probabilistic', 'ensemble'])
|
|
|
|
parser.add_argument('--render', default=False, action='store_true')
|
|
|
|
parser.add_argument('--port', default=2000, type=int)
|
2023-04-12 07:33:42 +00:00
|
|
|
parser.add_argument('--num_likelihood_updates', default=5, type=int)
|
2023-03-23 14:05:28 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DPI:
|
2023-04-12 15:15:17 +00:00
|
|
|
def __init__(self, args, writer):
|
2023-03-23 14:05:28 +00:00
|
|
|
# wandb config
|
|
|
|
#run = wandb.init(project="dpi")
|
|
|
|
|
|
|
|
self.args = args
|
|
|
|
|
2023-03-25 16:07:07 +00:00
|
|
|
# set environment noise
|
|
|
|
set_global_var(self.args.high_noise)
|
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
# environment setup
|
|
|
|
self.env = make_env(self.args)
|
2023-04-09 16:22:41 +00:00
|
|
|
#self.args.seed = np.random.randint(0, 1000)
|
2023-03-23 14:05:28 +00:00
|
|
|
self.env.seed(self.args.seed)
|
|
|
|
|
2023-03-28 18:21:26 +00:00
|
|
|
# noiseless environment setup
|
|
|
|
self.args.version = 2 # env_id changes to v2
|
|
|
|
self.args.img_source = None # no image noise
|
|
|
|
self.args.resource_files = None
|
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
# stack several consecutive frames together
|
|
|
|
if self.args.encoder_type.startswith('pixel'):
|
|
|
|
self.env = utils.FrameStack(self.env, k=self.args.frame_stack)
|
|
|
|
|
|
|
|
# create replay buffer
|
|
|
|
self.data_buffer = ReplayBuffer(size=self.args.replay_buffer_capacity,
|
|
|
|
obs_shape=(self.args.frame_stack*self.args.channels,self.args.image_size,self.args.image_size),
|
|
|
|
action_size=self.env.action_space.shape[0],
|
|
|
|
seq_len=self.args.episode_length,
|
2023-03-24 19:39:14 +00:00
|
|
|
batch_size=args.batch_size,
|
|
|
|
args=self.args)
|
2023-04-12 07:33:42 +00:00
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
# create work directory
|
|
|
|
utils.make_dir(self.args.work_dir)
|
2023-03-24 19:39:14 +00:00
|
|
|
self.video_dir = utils.make_dir(os.path.join(self.args.work_dir, 'video'))
|
|
|
|
self.model_dir = utils.make_dir(os.path.join(self.args.work_dir, 'model'))
|
|
|
|
self.buffer_dir = utils.make_dir(os.path.join(self.args.work_dir, 'buffer'))
|
2023-03-23 14:05:28 +00:00
|
|
|
|
|
|
|
# create models
|
2023-03-24 19:39:14 +00:00
|
|
|
self.build_models(use_saved=False, saved_model_dir=self.model_dir)
|
2023-03-23 14:05:28 +00:00
|
|
|
|
|
|
|
def build_models(self, use_saved, saved_model_dir=None):
|
2023-04-10 18:18:17 +00:00
|
|
|
# World Models
|
2023-03-23 14:05:28 +00:00
|
|
|
self.obs_encoder = ObservationEncoder(
|
2023-04-12 15:15:17 +00:00
|
|
|
obs_shape=(self.args.frame_stack*self.args.channels,self.args.image_size,self.args.image_size), # (9,84,84)
|
2023-03-23 14:05:28 +00:00
|
|
|
state_size=self.args.state_size # 128
|
|
|
|
)
|
2023-04-02 16:52:46 +00:00
|
|
|
|
|
|
|
self.obs_encoder_momentum = ObservationEncoder(
|
2023-04-12 15:15:17 +00:00
|
|
|
obs_shape=(self.args.frame_stack*self.args.channels,self.args.image_size,self.args.image_size), # (9,84,84)
|
2023-04-02 16:52:46 +00:00
|
|
|
state_size=self.args.state_size # 128
|
|
|
|
)
|
2023-03-23 14:05:28 +00:00
|
|
|
|
|
|
|
self.obs_decoder = ObservationDecoder(
|
|
|
|
state_size=self.args.state_size, # 128
|
2023-04-12 15:15:17 +00:00
|
|
|
output_shape=(self.args.frame_stack*self.args.channels,self.args.image_size,self.args.image_size) # (9,84,84)
|
2023-03-23 14:05:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.transition_model = TransitionModel(
|
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
action_size=self.env.action_space.shape[0], # 6
|
|
|
|
history_size=self.args.history_size, # 128
|
|
|
|
)
|
2023-04-10 11:18:08 +00:00
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
# Actor Model
|
|
|
|
self.actor_model = Actor(
|
2023-04-10 11:18:08 +00:00
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
hidden_size=self.args.hidden_size, # 256,
|
|
|
|
action_size=self.env.action_space.shape[0], # 6
|
|
|
|
)
|
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
# Value Models
|
2023-04-10 11:18:08 +00:00
|
|
|
self.value_model = ValueModel(
|
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
)
|
|
|
|
|
|
|
|
self.target_value_model = ValueModel(
|
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
)
|
|
|
|
|
|
|
|
self.reward_model = RewardModel(
|
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
)
|
2023-04-10 18:18:17 +00:00
|
|
|
|
|
|
|
# Contrastive Models
|
|
|
|
self.prjoection_head = ProjectionHead(
|
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
action_size=self.env.action_space.shape[0], # 6
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
)
|
|
|
|
|
|
|
|
self.prjoection_head_momentum = ProjectionHead(
|
|
|
|
state_size=self.args.state_size, # 128
|
|
|
|
action_size=self.env.action_space.shape[0], # 6
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
)
|
|
|
|
|
|
|
|
self.contrastive_head = ContrastiveHead(
|
|
|
|
hidden_size=self.args.hidden_size, # 256
|
|
|
|
)
|
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
|
|
|
|
# model parameters
|
2023-04-10 18:18:17 +00:00
|
|
|
self.world_model_parameters = list(self.obs_encoder.parameters()) + list(self.obs_decoder.parameters()) + \
|
|
|
|
list(self.value_model.parameters()) + list(self.transition_model.parameters()) + \
|
|
|
|
list(self.prjoection_head.parameters())
|
2023-04-12 07:33:42 +00:00
|
|
|
self.past_transition_parameters = self.transition_model.parameters()
|
2023-03-23 14:05:28 +00:00
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
# optimizers
|
|
|
|
self.world_model_opt = torch.optim.Adam(self.world_model_parameters, self.args.world_model_lr)
|
|
|
|
self.value_opt = torch.optim.Adam(self.value_model.parameters(), self.args.value_lr)
|
|
|
|
self.actor_opt = torch.optim.Adam(self.actor_model.parameters(), self.args.actor_lr)
|
2023-04-12 07:33:42 +00:00
|
|
|
self.past_transition_opt = torch.optim.Adam(self.past_transition_parameters, self.args.past_transition_lr)
|
2023-04-10 18:18:17 +00:00
|
|
|
|
|
|
|
# Create Modules
|
|
|
|
self.world_model_modules = [self.obs_encoder, self.obs_decoder, self.value_model, self.transition_model, self.prjoection_head]
|
|
|
|
self.value_modules = [self.value_model]
|
|
|
|
self.actor_modules = [self.actor_model]
|
2023-03-23 14:05:28 +00:00
|
|
|
|
|
|
|
if use_saved:
|
|
|
|
self._use_saved_models(saved_model_dir)
|
|
|
|
|
|
|
|
def _use_saved_models(self, saved_model_dir):
|
|
|
|
self.obs_encoder.load_state_dict(torch.load(os.path.join(saved_model_dir, 'obs_encoder.pt')))
|
|
|
|
self.obs_decoder.load_state_dict(torch.load(os.path.join(saved_model_dir, 'obs_decoder.pt')))
|
|
|
|
self.transition_model.load_state_dict(torch.load(os.path.join(saved_model_dir, 'transition_model.pt')))
|
|
|
|
|
2023-03-28 18:21:26 +00:00
|
|
|
def collect_sequences(self, episodes):
|
2023-03-23 14:05:28 +00:00
|
|
|
obs = self.env.reset()
|
|
|
|
done = False
|
2023-03-24 19:39:14 +00:00
|
|
|
|
2023-03-25 16:07:07 +00:00
|
|
|
#video = VideoRecorder(self.video_dir if args.save_video else None, resource_files=args.resource_files)
|
2023-03-27 17:23:42 +00:00
|
|
|
for episode_count in tqdm.tqdm(range(episodes), desc='Collecting episodes'):
|
2023-03-28 18:21:26 +00:00
|
|
|
if args.save_video:
|
|
|
|
self.env.video.init(enabled=True)
|
2023-03-31 16:00:07 +00:00
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
for i in range(self.args.episode_length):
|
2023-04-09 16:22:41 +00:00
|
|
|
|
2023-03-23 14:05:28 +00:00
|
|
|
action = self.env.action_space.sample()
|
2023-03-28 18:21:26 +00:00
|
|
|
|
2023-04-09 16:22:41 +00:00
|
|
|
next_obs, rew, done, _ = self.env.step(action)
|
2023-03-23 14:05:28 +00:00
|
|
|
|
2023-04-12 07:33:42 +00:00
|
|
|
self.data_buffer.add(obs, action, next_obs, rew, episode_count+1, done)
|
2023-03-25 13:18:07 +00:00
|
|
|
|
2023-03-28 18:21:26 +00:00
|
|
|
if args.save_video:
|
2023-04-09 16:22:41 +00:00
|
|
|
self.env.video.record(self.env)
|
2023-03-25 13:18:07 +00:00
|
|
|
|
2023-04-09 16:22:41 +00:00
|
|
|
if done or i == self.args.episode_length-1:
|
2023-03-23 14:05:28 +00:00
|
|
|
obs = self.env.reset()
|
2023-04-09 16:22:41 +00:00
|
|
|
done=False
|
2023-03-23 14:05:28 +00:00
|
|
|
else:
|
|
|
|
obs = next_obs
|
2023-03-28 18:21:26 +00:00
|
|
|
if args.save_video:
|
|
|
|
self.env.video.save('noisy/%d.mp4' % episode_count)
|
2023-03-23 14:05:28 +00:00
|
|
|
print("Collected {} random episodes".format(episode_count+1))
|
|
|
|
|
2023-03-24 19:39:14 +00:00
|
|
|
def train(self):
|
|
|
|
# collect experience
|
2023-03-28 18:21:26 +00:00
|
|
|
self.collect_sequences(self.args.batch_size)
|
2023-03-24 19:39:14 +00:00
|
|
|
|
2023-04-09 16:22:41 +00:00
|
|
|
# Group observations and next_observations by steps from past to present
|
2023-04-12 07:33:42 +00:00
|
|
|
last_observations = torch.tensor(self.data_buffer.group_steps(self.data_buffer,"observations")).float()[:self.args.episode_length-1]
|
2023-04-09 16:22:41 +00:00
|
|
|
current_observations = torch.Tensor(self.data_buffer.group_steps(self.data_buffer,"next_observations")).float()[:self.args.episode_length-1]
|
|
|
|
next_observations = torch.Tensor(self.data_buffer.group_steps(self.data_buffer,"next_observations")).float()[1:]
|
|
|
|
actions = torch.Tensor(self.data_buffer.group_steps(self.data_buffer,"actions",obs=False)).float()[:self.args.episode_length-1]
|
|
|
|
next_actions = torch.Tensor(self.data_buffer.group_steps(self.data_buffer,"actions",obs=False)).float()[1:]
|
2023-04-12 07:33:42 +00:00
|
|
|
rewards = torch.Tensor(self.data_buffer.group_steps(self.data_buffer,"rewards",obs=False)).float()[1:]
|
2023-04-12 15:15:17 +00:00
|
|
|
|
|
|
|
# Preprocessing
|
|
|
|
last_observations = preprocess_obs(last_observations)
|
|
|
|
current_observations = preprocess_obs(current_observations)
|
|
|
|
next_observations = preprocess_obs(next_observations)
|
|
|
|
|
2023-03-27 17:23:42 +00:00
|
|
|
# Initialize transition model states
|
|
|
|
self.transition_model.init_states(self.args.batch_size, device="cpu") # (N,128)
|
|
|
|
self.history = self.transition_model.prev_history # (N,128)
|
|
|
|
|
2023-03-24 19:39:14 +00:00
|
|
|
# Train encoder
|
2023-04-10 18:18:17 +00:00
|
|
|
step = 0
|
|
|
|
total_steps = 10000
|
2023-04-12 07:33:42 +00:00
|
|
|
metrics = {}
|
2023-04-10 18:18:17 +00:00
|
|
|
while step < total_steps:
|
|
|
|
for i in range(self.args.episode_length-1):
|
|
|
|
if i > 0:
|
|
|
|
# Encode observations and next_observations
|
|
|
|
self.last_states_dict = self.get_features(last_observations[i])
|
|
|
|
self.current_states_dict = self.get_features(current_observations[i])
|
|
|
|
self.next_states_dict = self.get_features(next_observations[i], momentum=True)
|
|
|
|
self.action = actions[i] # (N,6)
|
2023-04-12 07:33:42 +00:00
|
|
|
self.next_action = next_actions[i] # (N,6)
|
2023-04-10 18:18:17 +00:00
|
|
|
history = self.transition_model.prev_history
|
|
|
|
|
|
|
|
# Encode negative observations
|
|
|
|
idx = torch.randperm(current_observations[i].shape[0]) # random permutation on batch
|
|
|
|
random_time_index = torch.randint(0, self.args.episode_length-2, (1,)).item() # random time index
|
|
|
|
negative_current_observations = current_observations[random_time_index][idx]
|
|
|
|
self.negative_current_states_dict = self.obs_encoder(negative_current_observations)
|
|
|
|
|
|
|
|
# Predict current state from past state with transition model
|
|
|
|
last_states_sample = self.last_states_dict["sample"]
|
|
|
|
predicted_current_state_dict = self.transition_model.imagine_step(last_states_sample, self.action, self.history)
|
|
|
|
self.history = predicted_current_state_dict["history"]
|
|
|
|
|
|
|
|
# Calculate upper bound loss
|
2023-04-12 07:33:42 +00:00
|
|
|
likeli_loss, ub_loss = self._upper_bound_minimization(self.last_states_dict,
|
|
|
|
self.current_states_dict,
|
|
|
|
self.negative_current_states_dict,
|
|
|
|
predicted_current_state_dict
|
|
|
|
)
|
|
|
|
#likeli_loss = torch.tensor(likeli_loss.numpy(),dtype=torch.float32, requires_grad=True)
|
|
|
|
#ikeli_loss = likeli_loss.mean()
|
2023-04-10 18:18:17 +00:00
|
|
|
|
|
|
|
# Calculate encoder loss
|
|
|
|
encoder_loss = self._past_encoder_loss(self.current_states_dict,
|
|
|
|
predicted_current_state_dict)
|
|
|
|
|
|
|
|
#total_ub_loss += ub_loss
|
|
|
|
#total_encoder_loss += encoder_loss
|
|
|
|
|
|
|
|
# contrastive projection
|
|
|
|
vec_anchor = predicted_current_state_dict["sample"]
|
|
|
|
vec_positive = self.next_states_dict["sample"].detach()
|
|
|
|
z_anchor = self.prjoection_head(vec_anchor, self.action)
|
|
|
|
z_positive = self.prjoection_head_momentum(vec_positive, next_actions[i]).detach()
|
|
|
|
|
|
|
|
# contrastive loss
|
|
|
|
logits = self.contrastive_head(z_anchor, z_positive)
|
|
|
|
labels = labels = torch.arange(logits.shape[0]).long()
|
|
|
|
lb_loss = F.cross_entropy(logits, labels)
|
|
|
|
|
|
|
|
# behaviour learning
|
|
|
|
with FreezeParameters(self.world_model_modules):
|
2023-04-12 07:33:42 +00:00
|
|
|
imagine_horizon = self.args.imagine_horizon #np.minimum(self.args.imagine_horizon, self.args.episode_length-1-i)
|
2023-04-10 18:18:17 +00:00
|
|
|
imagined_rollout = self.transition_model.imagine_rollout(self.current_states_dict["sample"].detach(),
|
2023-04-12 07:33:42 +00:00
|
|
|
self.next_action, self.history.detach(),
|
2023-04-10 18:18:17 +00:00
|
|
|
imagine_horizon)
|
2023-04-12 07:33:42 +00:00
|
|
|
|
2023-04-12 15:15:17 +00:00
|
|
|
# decoder loss
|
|
|
|
horizon = np.minimum(50-i, imagine_horizon)
|
|
|
|
obs_dist = self.obs_decoder(imagined_rollout["sample"][:horizon])
|
|
|
|
decoder_loss = -torch.mean(obs_dist.log_prob(next_observations[i:i+horizon]))
|
|
|
|
|
|
|
|
# reward loss
|
|
|
|
reward_dist = self.reward_model(self.current_states_dict["sample"])
|
|
|
|
reward_loss = -torch.mean(reward_dist.log_prob(rewards[:-1]))
|
|
|
|
|
|
|
|
# update models
|
|
|
|
world_model_loss = encoder_loss + ub_loss + lb_loss + decoder_loss * 1e-2
|
|
|
|
self.world_model_opt.zero_grad()
|
|
|
|
world_model_loss.backward()
|
|
|
|
nn.utils.clip_grad_norm_(self.world_model_parameters, self.args.grad_clip_norm)
|
|
|
|
self.world_model_opt.step()
|
|
|
|
|
2023-04-12 07:33:42 +00:00
|
|
|
# actor loss
|
|
|
|
with FreezeParameters(self.world_model_modules + self.value_modules):
|
|
|
|
imag_rew_dist = self.reward_model(imagined_rollout["sample"])
|
|
|
|
target_imag_val_dist = self.target_value_model(imagined_rollout["sample"])
|
|
|
|
|
|
|
|
imag_rews = imag_rew_dist.mean
|
|
|
|
target_imag_vals = target_imag_val_dist.mean
|
|
|
|
|
|
|
|
discounts = self.args.discount * torch.ones_like(imag_rews).detach()
|
2023-04-10 18:18:17 +00:00
|
|
|
|
2023-04-12 07:33:42 +00:00
|
|
|
self.target_returns = self._compute_lambda_return(imag_rews[:-1],
|
|
|
|
target_imag_vals[:-1],
|
|
|
|
discounts[:-1] ,
|
|
|
|
self.args.td_lambda,
|
|
|
|
target_imag_vals[-1])
|
|
|
|
|
|
|
|
discounts = torch.cat([torch.ones_like(discounts[:1]), discounts[1:-1]], 0)
|
|
|
|
self.discounts = torch.cumprod(discounts, 0).detach()
|
|
|
|
actor_loss = -torch.mean(self.discounts * self.target_returns)
|
|
|
|
|
2023-04-12 15:15:17 +00:00
|
|
|
# update actor
|
2023-04-12 07:33:42 +00:00
|
|
|
self.actor_opt.zero_grad()
|
|
|
|
actor_loss.backward()
|
|
|
|
nn.utils.clip_grad_norm_(self.actor_model.parameters(), self.args.grad_clip_norm)
|
|
|
|
self.actor_opt.step()
|
|
|
|
|
|
|
|
# value loss
|
|
|
|
with torch.no_grad():
|
|
|
|
value_feat = imagined_rollout["sample"][:-1].detach()
|
|
|
|
value_targ = self.target_returns.detach()
|
|
|
|
|
|
|
|
value_dist = self.value_model(value_feat)
|
|
|
|
value_loss = -torch.mean(self.discounts * value_dist.log_prob(value_targ).unsqueeze(-1))
|
2023-04-12 15:15:17 +00:00
|
|
|
|
|
|
|
# update value
|
2023-04-12 07:33:42 +00:00
|
|
|
self.value_opt.zero_grad()
|
|
|
|
value_loss.backward()
|
|
|
|
nn.utils.clip_grad_norm_(self.value_model.parameters(), self.args.grad_clip_norm)
|
|
|
|
self.value_opt.step()
|
|
|
|
|
2023-04-12 15:15:17 +00:00
|
|
|
# update target value
|
|
|
|
if step % self.args.value_target_update_freq == 0:
|
|
|
|
self.target_value_model = copy.deepcopy(self.value_model)
|
|
|
|
|
|
|
|
# update momentum encoder
|
|
|
|
soft_update_params(self.obs_encoder, self.obs_encoder_momentum, self.args.encoder_tau)
|
|
|
|
|
|
|
|
# update momentum projection head
|
|
|
|
soft_update_params(self.prjoection_head, self.prjoection_head_momentum, self.args.encoder_tau)
|
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
step += 1
|
2023-04-12 15:15:17 +00:00
|
|
|
|
|
|
|
if step % self.args.logging_freq:
|
|
|
|
writer.add_scalar('Main Loss/World Loss', world_model_loss, step)
|
|
|
|
writer.add_scalar('Main Models Loss/Encoder Loss', encoder_loss, step)
|
|
|
|
writer.add_scalar('Main Models Loss/Decoder Loss', decoder_loss, step)
|
|
|
|
writer.add_scalar('Actor Critic Loss/Actor Loss', actor_loss, step)
|
|
|
|
writer.add_scalar('Actor Critic Loss/Value Loss', value_loss, step)
|
|
|
|
writer.add_scalar('Actor Critic Loss/Reward Loss', reward_loss, step)
|
|
|
|
writer.add_scalar('Bound Loss/Upper Bound Loss', ub_loss, step)
|
|
|
|
writer.add_scalar('Bound Loss/Lower Bound Loss', lb_loss, step)
|
|
|
|
|
|
|
|
"""
|
|
|
|
if step % self.args.logging_freq:
|
|
|
|
metrics['Upper Bound Loss'] = ub_loss.item()
|
|
|
|
metrics['Encoder Loss'] = encoder_loss.item()
|
|
|
|
metrics['Decoder Loss'] = decoder_loss.item()
|
|
|
|
metrics["Lower Bound Loss"] = lb_loss.item()
|
|
|
|
metrics["World Model Loss"] = world_model_loss.item()
|
|
|
|
wandb.log(metrics)
|
|
|
|
"""
|
2023-04-10 18:18:17 +00:00
|
|
|
|
|
|
|
if step>total_steps:
|
|
|
|
print("Training finished")
|
|
|
|
break
|
2023-04-09 16:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _upper_bound_minimization(self, last_states, current_states, negative_current_states, predicted_current_states):
|
|
|
|
club_sample = CLUBSample(last_states,
|
|
|
|
current_states,
|
|
|
|
negative_current_states,
|
|
|
|
predicted_current_states)
|
2023-04-12 07:33:42 +00:00
|
|
|
likelihood_loss = club_sample.learning_loss()
|
|
|
|
club_loss = club_sample()
|
|
|
|
return likelihood_loss, club_loss
|
2023-04-09 16:22:41 +00:00
|
|
|
|
|
|
|
def _past_encoder_loss(self, curr_states_dict, predicted_curr_states_dict):
|
|
|
|
# current state distribution
|
|
|
|
curr_states_dist = curr_states_dict["distribution"]
|
2023-03-27 17:23:42 +00:00
|
|
|
|
2023-04-09 16:22:41 +00:00
|
|
|
# predicted current state distribution
|
|
|
|
predicted_curr_states_dist = predicted_curr_states_dict["distribution"]
|
|
|
|
|
2023-04-10 18:18:17 +00:00
|
|
|
|
2023-03-27 17:23:42 +00:00
|
|
|
|
|
|
|
# KL divergence loss
|
2023-04-10 18:18:17 +00:00
|
|
|
loss = torch.distributions.kl.kl_divergence(curr_states_dist, predicted_curr_states_dist).mean()
|
2023-03-23 14:05:28 +00:00
|
|
|
|
2023-03-27 17:23:42 +00:00
|
|
|
return loss
|
2023-04-09 16:22:41 +00:00
|
|
|
|
2023-04-12 15:15:17 +00:00
|
|
|
def get_features(self, x, momentum=False):
|
2023-04-10 18:18:17 +00:00
|
|
|
if self.args.aug:
|
2023-04-09 16:22:41 +00:00
|
|
|
x = T.RandomCrop((80, 80))(x) # (None,80,80,4)
|
|
|
|
x = T.functional.pad(x, (4, 4, 4, 4), "symmetric") # (None,88,88,4)
|
|
|
|
x = T.RandomCrop((84, 84))(x) # (None,84,84,4)
|
|
|
|
|
|
|
|
with torch.no_grad():
|
|
|
|
if momentum:
|
|
|
|
x = self.obs_encoder_momentum(x)
|
2023-04-10 18:18:17 +00:00
|
|
|
else:
|
|
|
|
x = self.obs_encoder(x)
|
2023-04-09 16:22:41 +00:00
|
|
|
return x
|
2023-04-12 07:33:42 +00:00
|
|
|
|
|
|
|
def _compute_lambda_return(self, rewards, values, discounts, td_lam, last_value):
|
|
|
|
next_values = torch.cat([values[1:], last_value.unsqueeze(0)],0)
|
|
|
|
targets = rewards + discounts * next_values * (1-td_lam)
|
|
|
|
rets =[]
|
|
|
|
last_rew = last_value
|
|
|
|
|
|
|
|
for t in range(rewards.shape[0]-1, -1, -1):
|
|
|
|
last_rew = targets[t] + discounts[t] * td_lam *(last_rew)
|
|
|
|
rets.append(last_rew)
|
|
|
|
|
|
|
|
returns = torch.flip(torch.stack(rets), [0])
|
|
|
|
return returns
|
2023-03-23 14:05:28 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parse_args()
|
2023-04-12 15:15:17 +00:00
|
|
|
|
|
|
|
writer = SummaryWriter()
|
2023-03-23 14:05:28 +00:00
|
|
|
|
2023-04-12 15:15:17 +00:00
|
|
|
dpi = DPI(args, writer)
|
2023-03-24 19:39:14 +00:00
|
|
|
dpi.train()
|