user2249675
user2249675

Reputation: 484

Policy-based learning does not converge

I am trying to implement proximal policy optimization, and I'm facing a very strange problem.

Here is a minimal demonstration of the problem:

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})

The program draws numbers according to a probability distribution. If it draws 0, it is given a reward of 1. If it draws other numbers, it is given a reward of -1. The program then adjusts the probabilities according to the results.

In theory, the probability of choosing 0 should always increase, eventually converging to 1. In practice, however, it is decreasing.

What am I doing wrong here?

Upvotes: 1

Views: 94

Answers (1)

user2249675
user2249675

Reputation: 484

I solved it! I didn't understand the effect of reduce_sum enough.

Just change

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

into

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

Upvotes: 2

Related Questions