def init_nets(self, global_nets=None): ''' Initialize the neural network used to learn the policy function from the spec Below we automatically select an appropriate net for a discrete or continuous action space if the setting is of the form 'MLPNet'. Otherwise the correct type of network is assumed to be specified in the spec. Networks for continuous action spaces have two heads and return two values, the first is a tensor containing the mean of the action policy, the second is a tensor containing the std deviation of the action policy. The distribution is assumed to be a Gaussian (Normal) distribution. Networks for discrete action spaces have a single head and return the logits for a categorical probability distribution over the discrete actions ''' in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body) NetClass = getattr(net, self.net_spec['type']) self.net = NetClass(self.net_spec, in_dim, out_dim) self.net_names = ['net'] # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler(self.optim, self.net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets() reward_path = '../irl/NeuralDialog-LAED/logs/2019-08-16T12-04-13-mwoz_gan_vae.py' # r4 use_gpu = False # self.reward_agent = reward_agent.RewardAgent(use_gpu) self.reward_agent = reward_agent.RewardAgent_EncoderSide(use_gpu) val_feed = reward_utils.WoZGanDataLoaders('val') reward_agent.load_reward_model(self.reward_agent, reward_path, use_gpu) if use_gpu: self.reward_agent.cuda() self.reward_agent.eval() self.reward_count = 0 self.batch_count = 0 reward_utils.reward_validate(self.reward_agent, val_feed)
def init_nets(self, global_nets=None): ''' Initialize the neural networks used to learn the actor and critic from the spec Below we automatically select an appropriate net based on two different conditions 1. If the action space is discrete or continuous action - Networks for continuous action spaces have two heads and return two values, the first is a tensor containing the mean of the action policy, the second is a tensor containing the std deviation of the action policy. The distribution is assumed to be a Gaussian (Normal) distribution. - Networks for discrete action spaces have a single head and return the logits for a categorical probability distribution over the discrete actions 2. If the actor and critic are separate or share weights - If the networks share weights then the single network returns a list. - Continuous action spaces: The return list contains 3 elements: The first element contains the mean output for the actor (policy), the second element the std dev of the policy, and the third element is the state-value estimated by the network. - Discrete action spaces: The return list contains 2 element. The first element is a tensor containing the logits for a categorical probability distribution over the actions. The second element contains the state-value estimated by the network. 3. If the network type is feedforward, convolutional, or recurrent - Feedforward and convolutional networks take a single state as input and require an OnPolicyReplay or OnPolicyBatchReplay memory - Recurrent networks take n states as input and require env spec "frame_op": "concat", "frame_op_len": seq_len ''' assert 'shared' in self.net_spec, 'Specify "shared" for ActorCritic network in net_spec' self.shared = self.net_spec['shared'] # create actor/critic specific specs actor_net_spec = self.net_spec.copy() critic_net_spec = self.net_spec.copy() for k in self.net_spec: if 'actor_' in k: actor_net_spec[k.replace('actor_', '')] = actor_net_spec.pop(k) critic_net_spec.pop(k) if 'critic_' in k: critic_net_spec[k.replace('critic_', '')] = critic_net_spec.pop(k) actor_net_spec.pop(k) if critic_net_spec['use_same_optim']: critic_net_spec = actor_net_spec in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body, add_critic=self.shared) # main actor network, may contain out_dim self.shared == True NetClass = getattr(net, actor_net_spec['type']) self.net = NetClass(actor_net_spec, in_dim, out_dim) self.net_names = ['net'] if not self.shared: # add separate network for critic critic_out_dim = 1 CriticNetClass = getattr(net, critic_net_spec['type']) self.critic_net = CriticNetClass(critic_net_spec, in_dim, critic_out_dim) self.net_names.append('critic_net') # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler( self.optim, self.net.lr_scheduler_spec) if not self.shared: self.critic_optim = net_util.get_optim(self.critic_net, self.critic_net.optim_spec) self.critic_lr_scheduler = net_util.get_lr_scheduler( self.critic_optim, self.critic_net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets()
def init_nets(self, global_nets=None): '''Initialize the neural network used to learn the Q function from the spec''' if self.algorithm_spec['name'] == 'VanillaDQN': assert all(k not in self.net_spec for k in ['update_type', 'update_frequency', 'polyak_coef']), 'Network update not available for VanillaDQN; use DQN.' in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body) NetClass = getattr(net, self.net_spec['type']) self.net = NetClass(self.net_spec, in_dim, out_dim) self.net_names = ['net'] # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler(self.optim, self.net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets()
def init_nets(self, global_nets=None): '''Initialize the neural network used to learn the Q function from the spec''' if 'Recurrent' in self.net_spec['type']: self.net_spec.update(seq_len=self.net_spec['seq_len']) in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body) NetClass = getattr(net, self.net_spec['type']) self.net = NetClass(self.net_spec, in_dim, out_dim) self.net_names = ['net'] # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler( self.optim, self.net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets()
def init_nets(self, global_nets=None): '''Initialize networks''' if self.algorithm_spec['name'] == 'DQNBase': assert all(k not in self.net_spec for k in ['update_type', 'update_frequency', 'polyak_coef']), 'Network update not available for DQNBase; use DQN.' in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body) NetClass = getattr(net, self.net_spec['type']) self.net = NetClass(self.net_spec, in_dim, out_dim) self.target_net = NetClass(self.net_spec, in_dim, out_dim) self.net_names = ['net', 'target_net'] # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler(self.optim, self.net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets() self.online_net = self.target_net self.eval_net = self.target_net
def init_nets(self, global_nets=None): ''' Initialize the neural network used to learn the policy function from the spec Below we automatically select an appropriate net for a discrete or continuous action space if the setting is of the form 'MLPNet'. Otherwise the correct type of network is assumed to be specified in the spec. Networks for continuous action spaces have two heads and return two values, the first is a tensor containing the mean of the action policy, the second is a tensor containing the std deviation of the action policy. The distribution is assumed to be a Gaussian (Normal) distribution. Networks for discrete action spaces have a single head and return the logits for a categorical probability distribution over the discrete actions ''' in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body) NetClass = getattr(net, self.net_spec['type']) self.net = NetClass(self.net_spec, in_dim, out_dim) self.net_names = ['net'] # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler( self.optim, self.net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets()
def init_nets(self, global_nets=None): ''' Initialize the neural networks used to learn the actor and critic from the spec Below we automatically select an appropriate net based on two different conditions 1. If the action space is discrete or continuous action - Networks for continuous action spaces have two heads and return two values, the first is a tensor containing the mean of the action policy, the second is a tensor containing the std deviation of the action policy. The distribution is assumed to be a Gaussian (Normal) distribution. - Networks for discrete action spaces have a single head and return the logits for a categorical probability distribution over the discrete actions 2. If the actor and critic are separate or share weights - If the networks share weights then the single network returns a list. - Continuous action spaces: The return list contains 3 elements: The first element contains the mean output for the actor (policy), the second element the std dev of the policy, and the third element is the state-value estimated by the network. - Discrete action spaces: The return list contains 2 element. The first element is a tensor containing the logits for a categorical probability distribution over the actions. The second element contains the state-value estimated by the network. 3. If the network type is feedforward, convolutional, or recurrent - Feedforward and convolutional networks take a single state as input and require an OnPolicyReplay or OnPolicyBatchReplay memory - Recurrent networks take n states as input and require env spec "frame_op": "concat", "frame_op_len": seq_len ''' assert 'shared' in self.net_spec, 'Specify "shared" for ActorCritic network in net_spec' self.shared = self.net_spec['shared'] # create actor/critic specific specs actor_net_spec = self.net_spec.copy() critic_net_spec = self.net_spec.copy() for k in self.net_spec: if 'actor_' in k: actor_net_spec[k.replace('actor_', '')] = actor_net_spec.pop(k) critic_net_spec.pop(k) if 'critic_' in k: critic_net_spec[k.replace('critic_', '')] = critic_net_spec.pop(k) actor_net_spec.pop(k) if critic_net_spec['use_same_optim']: critic_net_spec = actor_net_spec in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body, add_critic=self.shared) # main actor network, may contain out_dim self.shared == True NetClass = getattr(net, actor_net_spec['type']) self.net = NetClass(actor_net_spec, in_dim, out_dim) self.net_names = ['net'] if not self.shared: # add separate network for critic critic_out_dim = 1 CriticNetClass = getattr(net, critic_net_spec['type']) self.critic_net = CriticNetClass(critic_net_spec, in_dim, critic_out_dim) self.net_names.append('critic_net') # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler( self.optim, self.net.lr_scheduler_spec) if not self.shared: self.critic_optim = net_util.get_optim(self.critic_net, self.critic_net.optim_spec) self.critic_lr_scheduler = net_util.get_lr_scheduler( self.critic_optim, self.critic_net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets() use_gpu = False vae_type = 'vae' self.experience_buffer = deque(maxlen=20) root_dir = os.path.dirname( os.path.dirname( os.path.dirname( os.path.dirname(os.path.dirname( os.path.abspath(__file__)))))) # reward_path = os.path.join(root_dir, "convlab_repo/saved_models/cl_3_VAE_no_kl_finish") # reward_path = os.path.join(root_dir, "convlab_repo/saved_models/cl_2_VAE") reward_path = os.path.join( root_dir, "convlab_repo/saved_models/cl_3_VAE_pre_training_mode") config_path = os.path.join(reward_path, "params.json") with open(config_path, 'r') as f: dic = json.load(f) config = argparse.Namespace(**dic) self.reward_agent = reward_agent.RewardAgent_EncoderSide( config, use_gpu, "mine") # reward_path = '../irl/NeuralDialog-LAED/logs/2019-09-06T12:04:49.278628-mwoz_gan_vae.py' # new trained vae-based reward # val_feed = reward_utils.WoZGanDataLoaders('val', 16) # train_feed = reward_utils.WoZGanDataLoaders('train', 16) # train_feed.epoch_init(shuffle=True) # self.discriminator = reward_agent.A2C_Discriminator(config, use_gpu, train_feed, 16) # self.optim_disc = self.discriminator.get_optimizer() self.load_pretrain_policy = self.algorithm_spec['load_pretrain_policy'] policy_mdl = './reward_model/policy_pretrain.mdl' if self.load_pretrain_policy: if os.path.exists(policy_mdl): self.net.load_state_dict(torch.load(policy_mdl)) # self.old_net.load_state_dict(torch.load(policy_mdl)) print("successfully loaded the pretrained policy model") else: raise ValueError("No policy model") reward_agent.load_reward_model(self.reward_agent, reward_path, use_gpu) if use_gpu: self.reward_agent.cuda() self.reward_agent.eval() self.reward_count = 0 self.batch_count = 0
def init_nets(self, global_nets=None): ''' Initialize the neural networks used to learn the actor and critic from the spec Below we automatically select an appropriate net based on two different conditions 1. If the action space is discrete or continuous action - Networks for continuous action spaces have two heads and return two values, the first is a tensor containing the mean of the action policy, the second is a tensor containing the std deviation of the action policy. The distribution is assumed to be a Gaussian (Normal) distribution. - Networks for discrete action spaces have a single head and return the logits for a categorical probability distribution over the discrete actions 2. If the actor and critic are separate or share weights - If the networks share weights then the single network returns a list. - Continuous action spaces: The return list contains 3 elements: The first element contains the mean output for the actor (policy), the second element the std dev of the policy, and the third element is the state-value estimated by the network. - Discrete action spaces: The return list contains 2 element. The first element is a tensor containing the logits for a categorical probability distribution over the actions. The second element contains the state-value estimated by the network. 3. If the network type is feedforward, convolutional, or recurrent - Feedforward and convolutional networks take a single state as input and require an OnPolicyReplay or OnPolicyBatchReplay memory - Recurrent networks take n states as input and require env spec "frame_op": "concat", "frame_op_len": seq_len ''' assert 'shared' in self.net_spec, 'Specify "shared" for ActorCritic network in net_spec' self.shared = self.net_spec['shared'] # create actor/critic specific specs actor_net_spec = self.net_spec.copy() critic_net_spec = self.net_spec.copy() for k in self.net_spec: if 'actor_' in k: actor_net_spec[k.replace('actor_', '')] = actor_net_spec.pop(k) critic_net_spec.pop(k) if 'critic_' in k: critic_net_spec[k.replace('critic_', '')] = critic_net_spec.pop(k) actor_net_spec.pop(k) if critic_net_spec['use_same_optim']: critic_net_spec = actor_net_spec in_dim = self.body.state_dim out_dim = net_util.get_out_dim(self.body, add_critic=self.shared) # main actor network, may contain out_dim self.shared == True NetClass = getattr(net, actor_net_spec['type']) self.net = NetClass(actor_net_spec, in_dim, out_dim) self.net_names = ['net'] if not self.shared: # add separate network for critic critic_out_dim = 1 CriticNetClass = getattr(net, critic_net_spec['type']) self.critic_net = CriticNetClass(critic_net_spec, in_dim, critic_out_dim) self.net_names.append('critic_net') # init net optimizer and its lr scheduler self.optim = net_util.get_optim(self.net, self.net.optim_spec) self.lr_scheduler = net_util.get_lr_scheduler( self.optim, self.net.lr_scheduler_spec) if not self.shared: self.critic_optim = net_util.get_optim(self.critic_net, self.critic_net.optim_spec) self.critic_lr_scheduler = net_util.get_lr_scheduler( self.critic_optim, self.critic_net.lr_scheduler_spec) net_util.set_global_nets(self, global_nets) self.post_init_nets() use_gpu = False vae_type = 'vae' self.experience_buffer = deque(maxlen=20) self.reward_agent = reward_agent.RewardAgent_EncoderSide( use_gpu, vae_type) # this is the State Vae and Action Onehot version reward_path = '../irl/NeuralDialog-LAED/logs/2019-09-06T12:04:49.278628-mwoz_gan_vae.py' # new trained vae-based reward val_feed = reward_utils.WoZGanDataLoaders('val', 16) train_feed = reward_utils.WoZGanDataLoaders('train', 16) train_feed.epoch_init(shuffle=True) self.discriminator = reward_agent.A2C_Discriminator( use_gpu, train_feed, 16) self.optim_disc = self.discriminator.get_optimizer() reward_agent.load_reward_model(self.reward_agent, reward_path, use_gpu) if use_gpu: self.reward_agent.cuda() self.reward_agent.eval() self.reward_count = 0 self.batch_count = 0 reward_utils.reward_validate(self.reward_agent, val_feed)