RL如何通过高斯策略进行连续控制?

时间:2019-02-07 09:06:42

标签: python tensorflow gaussian

我正在实现“软演员关键”算法,但我无法理解该静态策略的工作原理。我已经在线搜索过,但没有找到任何有趣的网站可以很好地说明以下实现。我唯一了解的是,在采用随机策略的情况下,我们将其建模为高斯模型,然后对均值和对数std进行参数化(我认为std是标准差),但是例如:为什么我们需要对数std和不只是性病?

class ActorNetwork(object):
    def __init__(self, act_dim, name):
        self.act_dim = act_dim
        self.name = name

    def step(self, obs, log_std_min=-20, log_std_max=2): 
        with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):

            h1 = tf.layers.dense(obs, 256, tf.nn.relu)
            h2 = tf.layers.dense(h1, 256, tf.nn.relu) 
            mu = tf.layers.dense(h2, self.act_dim, None)
            log_std = tf.layers.dense(h2, self.act_dim, tf.tanh)
            '''
            at the start we could have extremely large values for the log_stds, which could result in some actions 
            being either entirely deterministic or too random. To protect against that, 
            we'll constrain the output range of the log_stds, to be within [LOG_STD_MIN, LOG_STD_MAX]
            ''' 
            log_std = log_std_min + 0.5 * (log_std_max - log_std_min) * (log_std + 1)

            std = tf.exp(log_std)
            pi = mu + tf.random_normal(tf.shape(mu)) * std 

            #gaussian likelihood
            pre_sum = -0.5 * (((pi - mu) / (tf.exp(log_std) + EPS)) ** 2 + 2 * log_std + np.log(2 * np.pi)) 
            logp_pi = tf.reduce_sum(pre_sum, axis=1) 

            mu = tf.tanh(mu)
            pi = tf.tanh(pi)

            clip_pi = 1 - tf.square(pi) #pi^2
            clip_up = tf.cast(clip_pi > 1, tf.float32)
            clip_low = tf.cast(clip_pi < 0, tf.float32)
            clip_pi = clip_pi + tf.stop_gradient((1 - clip_pi) * clip_up + (0 - clip_pi) * clip_low)

            logp_pi -= tf.reduce_sum(tf.log(clip_pi + 1e-6), axis=1) 

        return mu, pi, logp_pi

    def evaluate(self, obs): #Choose action
        mu, pi, logp_pi = self.step(obs)
        action_scale = 2.0 # env.action_space.high[0]

        mu *= action_scale
        pi *= action_scale
        return mu, pi, logp_pi

1 个答案:

答案 0 :(得分:1)

你是对的。在高斯策略中,您从观察值(使用策略网络)映射到平均值ls $dirtyfolder/* | ? {(!$_.PSIsContainer) -and !($_.extension -eq ".lnk")} 和操作的标准偏差的对数mu。这是因为您有一个连续的动作空间。训练好模型以在行动空间中分配log_stdmu之后,您就可以计算出采取log_std采样的行动的对数似然率

在高斯政策中,pi.log_std更为可取,只是因为std接受(-inf,+ inf)中的任何值,而log_std限于非-负值。摆脱了这种非负性约束,使培训变得更加容易,并且您不会因这种转换而丢失任何信息。