gpt4 book ai didi

python - 将正态分布转换为 softmax

转载 作者:行者123 更新时间:2023-12-01 08:12:02 25 4
gpt4 key购买 nike

我在 github 上找到了一个很好的强化学习示例,我想使用它。我的问题是输出是正态分布层(下面的代码),因为它用于连续 Action 空间,而我想将它用于离散 Action 空间,其中模型有 4 个输出,我选择其中一个输出作为为环境采取的行动。

作为一项快速测试,我对正态分布层的输出进行 argmax 测试,然后对反向传播所选的操作进行 one-hot。

env_action = np.argmax(action)
action = np.zeros(ppo.a_dim) # turn action into one-hot representation
action[env_action] = 1

它工作得很好,但显然仅仅执行 argmax 会让代理表现得贪婪并且不会探索。

那么(我意识到这很hacky)我可以这样做吗:

nd_actions =  self.sess.run([self.sample_op], {self.state: state})       
rescale_nd = scale(nd_actions, 0, 1)
probs = tf.nn.softmax(rebase_nd)
action = np.random.choice(4, p=probs.numpy()[0])

这样做有什么本质上的错误吗?我知道最好将网络的输出层更改为 softmax,但不幸的是,这样做需要对代码进行大量重写,所以作为概念证明,我想测试一下这是否有效.

l1 = tf.layers.dense(self.state, 400, tf.nn.relu, trainable=trainable,
kernel_regularizer=w_reg, name="pi_l1")
l2 = tf.layers.dense(l1, 400, tf.nn.relu, trainable=trainable, kernel_regularizer=w_reg, name="pi_l2")
mu = tf.layers.dense(l2, self.a_dim, tf.nn.tanh, trainable=trainable,
kernel_regularizer=w_reg, name="pi_mu_out")
log_sigma = tf.get_variable(name="pi_log_sigma_out", shape=self.a_dim, trainable=trainable,
initializer=tf.zeros_initializer(), regularizer=w_reg)
norm_dist = tf.distributions.Normal(loc=mu * self.a_bound, scale=tf.exp(log_sigma))

最佳答案

我找到了一个输出分布层,它提供了我正在寻找的东西,现在我不需要重写大量的代码 - HURRAY!

a_logits = tf.layers.dense(l2, self.a_dim, kernel_regularizer=w_reg, name="pi_logits") 
dist = tf.distributions.Categorical(logits=a_logits)

关于python - 将正态分布转换为 softmax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55183865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com