SerialDev
SerialDev

Reputation: 2847

Extracting probabilities from a softmax layer in [tensorflow 1.00]

Using tensorflow, I have an LSTM classification model with a softmax as the final node.
This is my softmax layer:

with tf.name_scope("Softmax") as scope:
            with tf.variable_scope("Softmax_params"):
                softmax_w = tf.get_variable("softmax_w", [hidden_size, num_classes])
                softmax_b = tf.get_variable("softmax_b", [num_classes])
            logits = tf.nn.xw_plus_b(output, softmax_w, softmax_b) # Predicted label, eg y = tf.matmul(X, W) + b)

            loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.labels,
                                                                  logits=logits,
                                                                  name='softmax')

My question is when evaluating this model with novel data, how can I extract the probabilities for each class?

num_class = 129

for i in range(runs):
    X_batch, y_batch = sample_batch(X_test[:200], y_test[:200], batch_size)
    predictions = sess.run([model.logits], feed_dict = {model.input: X_batch, model.keep_prob: 1.0})

Upvotes: 2

Views: 4861

Answers (1)

Aaron
Aaron

Reputation: 2364

Just add the line

probs = tf.nn.softmax(logits)

Then do

predictions = sess.run(model.probs, feed_dict=feed_dict)

Upvotes: 4

Related Questions