Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement temperature #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

vanechu
Copy link

@vanechu vanechu commented May 11, 2016

Implement temperature #22 with default value as 1. Following description is cited from karpathy/char-rnn
.

Temperature. An important parameter you may want to play with is -temperature, which takes a number in range (0, 1] (0 not included), default = 1. The temperature is dividing the predicted log probabilities before the Softmax, so lower temperature will cause the model to make more likely, but also more boring and conservative predictions. Higher temperatures cause the model to take more chances and increase diversity of results, but at a cost of more mistakes.

@hunkim
Copy link
Contributor

hunkim commented Jul 22, 2016

Looks good!

@fujimotomh
Copy link

fujimotomh commented Oct 25, 2016

I think this line is undesirable:

self.probs = tf.nn.softmax(tf.div(self.logits, temperature))

This overwrites self.probs which looked to be used later (when using the network as a language model) meaning that it impossible to do generation and get probs at the same time without trickery. It may make sense to make a new placeholder variable in the model init for temperature which can be fed, and a new probs with temp which can be retrieved (1). Or it may make sense to the the temperature sampling outside of the tensorflow graph (2).

(1)

# add new placeholder

def __init__(...):
    ...
    self.temperature = tf.placeholder_with_default(tf.constant(1, dtype=tf.float32), None)
    self.temp_probs = tf.nn.softmax(tf.div(self.logits, self.temperature))
    ...

def sample(...):
    # same as this PR but use self.temp_probs when appropriate

(2)

# do temp sampling outside tf graph

def __init__(...):
    # same as before

def sample(...):
    ...
    # when appropriate, run to get self.logits
    logits, state = sess.run([self.logits, self.final_state], feed)
    logits = logits[0]

    if temperature == 0.0:
        sample = np.argmax(logits)
    else:
        scale = logits / temperature
        exp = np.exp(scale - np.max(scale))
        soft = exp / np.sum(exp)

        sample = np.random.choice(len(soft), p=soft)

@hugovk
Copy link
Contributor

hugovk commented Feb 16, 2017

@vanechu This PR has merge conflicts.

@jamesqo jamesqo mentioned this pull request Jul 4, 2018
@jamesqo
Copy link

jamesqo commented Jul 4, 2018

I made a new PR to implement this here with merge conflicts fixed.

@JaeDukSeo
Copy link

@fujimotomh good implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants