基于策略的学习无法收敛

我在尝试实现近端策略优化时,遇到了一个非常奇怪的问题。

以下是问题的简化演示:

import numpy as npimport tensorflow as tfraw_probs = tf.get_variable("raw_probs",[4])probs = tf.nn.softmax(raw_probs)actions = tf.placeholder(dtype=tf.int32, shape=[None], name='actions')rewards = tf.placeholder(dtype=tf.float32, shape=[None], name='rewards')old_probs = tf.placeholder(dtype=tf.float32, shape=[None], name='old_probs')new_probs = tf.reduce_sum(probs * tf.one_hot(indices=actions, depth=4))ratios = new_probs / old_probsclipped_ratios = tf.clip_by_value(ratios, clip_value_min=0.8, clip_value_max=1.2)loss_clip = -tf.reduce_mean(tf.minimum(tf.multiply(rewards, ratios), tf.multiply(rewards, clipped_ratios)))optimizer = tf.train.AdamOptimizer()train_pol = optimizer.minimize(loss_clip)with tf.Session() as sess:    sess.run(tf.global_variables_initializer())    for i in range(1000):        input_actions = []        input_rewards = []        input_old_probs = []        for j in range(20):            tmp_probs = sess.run(probs)            if j == 0:                print(tmp_probs)            act = np.random.choice(4,p=tmp_probs)            input_actions.append(act)            if act == 0:                input_rewards.append(1)            else:                input_rewards.append(-1)            input_old_probs.append(tmp_probs[act])        sess.run(train_pol,feed_dict={actions: input_actions,rewards: input_rewards,old_probs: input_old_probs})

程序根据概率分布抽取数字。如果抽到0,则获得1的奖励;如果抽到其他数字,则获得-1的奖励。接着,程序会根据结果调整概率。

理论上,选择0的概率应该始终增加,最终收敛到1。但在实际操作中,这个概率却在下降。

我在这里做错了什么?


回答:

我解决了!我对reduce_sum的效果理解得不够透彻。

只需将

new_probs = tf.reduce_sum(probs * tf.one_hot(indices=actions, depth=4))

改为

new_probs = tf.reduce_sum(probs * tf.one_hot(indices=actions, depth=4),1)

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注