我正在实现软Actor-Critic算法,但我不能理解stocastic策略是如何工作的。我已经在网上搜索过了,但我没有找到任何有趣的网站来很好地解释下面的实现。我唯一理解的是,在stocastic策略的情况下,我们将其建模为高斯模型,并将平均值和log std参数化(我认为std是标准差),但例如:为什么我们需要log 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发布于 2019-02-10 07:11:21
你是正确的。在高斯策略中,您可以从观察值(使用策略网络)映射到平均值mu和标准偏差的对数log_std of action。这是因为你有一个连续的动作空间。一旦训练模型在动作空间中分配mu和log_std,就可以计算由pi.采样的采取行动的对数似然
在高斯策略中,log_std优于std,这只是因为log_std接受(-inf,+inf)中的任何值,而std被限制为非负值。摆脱这种非负性约束会使训练变得更容易,而且这种转换也不会丢失任何信息。
https://stackoverflow.com/questions/54569726
复制相似问题