Taking softmax of scores in TensorFlow to output probabilities in addition to predictions

944 views Asked by At

I would like to be able to access the probability of my binary classifier trained in TensorFlow to allow me to tune the probability threshold without retraining the full model. The following two lines have been working fine for me for months:

self.scores = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores") self.predictions = tf.argmax(self.scores, 1, name="predictions")

I added the following line to evaluate the softmax of the scores:

self.probabilities = tf.nn.softmax(self.scores, 1, name="probabilities")

Which threw the following error:

Traceback (most recent call last):
      File "/home/produser/code/python/deeplearning/text_cnn_main.py", line 111, in <module>
        vocabulary=vocab_processor.vocabulary_)
      File "/var/store/code/python/deeplearning/text_cnn.py", line 84, in __init__
        self.probabilities = tf.nn.softmax(self.scores, 1, name="probs")
    TypeError: softmax() got multiple values for keyword argument 'name'

I tried removing the optional name argument, and ran the call as follows:

self.probabilities = tf.nn.softmax(self.scores, 1)

In this case, the following error was thrown:

Traceback (most recent call last):
  File "/home/produser/code/python/deeplearning/text_cnn_main.py", line 111, in <module>
    vocabulary=vocab_processor.vocabulary_)
  File "/var/store/code/python/deeplearning/text_cnn.py", line 84, in __init__
    self.probabilities = tf.nn.softmax(self.scores, 1)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 1396, in softmax
    result = _op_def_lib.apply_op("Softmax", logits=logits, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 357, in apply_op
    with g.as_default(), ops.name_scope(name) as scope:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2799, in name_scope
    if not _VALID_SCOPE_NAME_REGEX.match(name):
TypeError: expected string or buffer

It appears that the softmax function is inheriting the name from the input tensor. What is the best way to address this confusion in the namespace? Many thanks for your time and thoughts.

0

There are 0 answers