基于策略的学习不收敛

2024-06-23 03:08:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现最近的策略优化,我面临一个非常奇怪的问题

下面是对问题的一个简单演示:

import numpy as np
import tensorflow as tf

raw_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_probs
clipped_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。但实际上,它是在减少

我做错什么了


Tags: actionsinputrawcliptfasactold
1条回答
网友
1楼 · 发布于 2024-06-23 03:08:15

我解决了!我对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)

相关问题 更多 >

    热门问题