uber-research / PPLM

Plug and Play Language Model implementation. Allows to steer topic and attributes of GPT-2 models.
Apache License 2.0
1.13k stars 202 forks source link

Equation (4) | Attribute model #5

Closed pgrandinetti closed 4 years ago

pgrandinetti commented 4 years ago

Trying to understand the attribute model in equation (4) in your paper. I have two general questions.

  1. About the whole term p(a | H_t + \Delta H_t).

Given the modified model (which is actually my second question), you want to compute the probability that the model would generate a sequence that contains attributea.

Let's consider the BoW approach. For each word w in the bag, and given the current sentence sequence_so_far, you compute Prob(sequence_so_far + w). Is it correct so far? How do you compute that last term? Is it like model.predict(sequence_so_far+w)?

  1. About the modified history.

I get how it's computed. Not how the model is modified in practice though. Is it something like model.layers[i].set_weights(H[i] + DeltaH[i]), for the specific layers corresponding to the whole H?

Thanks!

dathath commented 4 years ago
  1. What is the last term you are referring to? I am a bit confused.

Prob(sequence_so_far + w) -- Is this the probability of observing the sequence [sequence_so_far, w]?

  1. We don't actually modify the weights themselves -- we modify the activations online during decoding -- See lines 284-310. To see what the "past" vectors are: See code for the GPT-2 model. These are actually the key-value pairs from the past that are cached and forwarded -- this is sort of nice because your optimization updates don't phase out when you sample new tokens, but rather get passed along.
pgrandinetti commented 4 years ago
  1. Here's my interpretation of that section in the paper.
    • It's given the text generated so far: text_so_far
    • For each word in BoW your objective is to compute Prob(text_so_far + w | modified model), in order to compute iteratively \Delta H
    • To compute Prob(text_so_far + w | modified model) you use the modified model in a standard way, e.g, model.predict(text_so_far+w) which returns a probability.

This is also why I thought you would simply (and brutally) modified the weights, so that you could run model.predict after changing the weights. I can see now that I am missing something, can you please give more details?

wuutiing commented 4 years ago
  1. Here's my interpretation of that section in the paper.
  • It's given the text generated so far: text_so_far
  • For each word in BoW your objective is to compute Prob(text_so_far + w | modified model), in order to compute iteratively \Delta H
  • To compute Prob(text_so_far + w | modified model) you use the modified model in a standard way, e.g, model.predict(text_so_far+w) which returns a probability.

This is also why I thought you would simply (and brutally) modified the weights, so that you could run model.predict after changing the weights. I can see now that I am missing something, can you please give more details?

i suggest you read the source code especially the function perturb_past in run_pplm.py(https://github.com/uber-research/PPLM/blob/dc58121277570ae85ee1e114188036f52bc37fe7/run_pplm.py#L117). the code says clearly that before generating a next word, losses are calculated (including bow\ DISCRIM\ kl_loss), and grads are calculated and applied to the hidden state, for num_iterations loops, which is the implementation of the 4th function in the pplm paper(https://arxiv.org/abs/1912.02164).

pgrandinetti commented 4 years ago

great thanks!