Search code examples
tensorflowdeep-learningreinforcement-learningtflearnopenai-gym

tf.multinomial outputs number other numbers than range


I am working with the OpenAI gym environment (using policy gradient). My network is outputting an action which is higher than the possible action range.

n_outputs = 9
learning_rate = 0.01

initializer = tf.variance_scaling_initializer()

X = tf.placeholder(tf.float32, shape=[None, 50, 70, 1])
network = tflearn.conv_2d(X, 32, 5, strides=2, activation='relu')
network = tflearn.max_pool_2d(network, 2)
network = tflearn.conv_2d(network, 32, 5, strides=2, activation='relu')
network = tflearn.max_pool_2d(network, 2)
network = tflearn.fully_connected(network, 256, activation='relu')
hidden = tf.layers.dense(network, 64, activation=tf.nn.relu, kernel_initializer=initializer)
logits = tf.layers.dense(hidden, n_outputs)
outputs = tf.nn.softmax(logits)
action = tf.multinomial(outputs, num_samples=1)

It outputs 9, which creates an error in the gym environment.

The full code.


Solution

  • tf.multinomial will sample outside of the range if it encounters numerical error, so in other words - you have NaNs in your graph.