Search code examples
pythontensorflowq-learningcross-entropy

Display loss in a Tensorflow DQN without leaving tf.Session()


I have a DQN all set up and working, but I can't figure out how to display the loss without leaving the Tensorflow session.

I first thought it involved creating a new function or class, but I'm not sure where to put it in the code, and what specifically to put into the function or class.

observations = tf.placeholder(tf.float32, shape=[None, num_stops], name='observations')
actions = tf.placeholder(tf.int32,shape=[None], name='actions')
rewards = tf.placeholder(tf.float32,shape=[None], name='rewards')

# Model
Y = tf.layers.dense(observations, 200, activation=tf.nn.relu)
Ylogits = tf.layers.dense(Y, num_stops)

# sample an action from predicted probabilities
sample_op = tf.random.categorical(logits=Ylogits, num_samples=1)


# loss
cross_entropies = tf.losses.softmax_cross_entropy(onehot_labels=tf.one_hot(actions,num_stops), logits=Ylogits)

loss = tf.reduce_sum(rewards * cross_entropies)

# training operation
optimizer = tf.train.RMSPropOptimizer(learning_rate=0.001, decay=.99)
train_op = optimizer.minimize(loss)

I then run the network, which works without error.

with tf.Session() as sess:

'''etc. The network is run'''

sess.run(train_op, feed_dict={observations: observations_list,
                             actions: actions_list,
                             rewards: rewards_list})

I want to have loss from train_op displayed to the user.


Solution

  • try this

    loss, _ = sess.run([loss, train_op], feed_dict={observations: observations_list,
                                 actions: actions_list,
                                 rewards: rewards_list})