-
Notifications
You must be signed in to change notification settings - Fork 756
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
How to get a probability for the generated text? #311
Comments
You can feed the generated text back into the model with the We have been considering adding functionality to provide the log-likelihood alongside the generated samples but are not currently working on it. A PR would be welcome. |
Thanks for the reply, I will take a look to see if I can get it working. |
Hi @craffel , I've got the scores by using the score function. However, I can only call this function from the model directly. Once the model is exported and loaded back using tf.saved_model.load, there's only the 'serving_default' signature available which just gives me the generated texts. I spent quite a bit time to understand how the model export works and I think the serving_default signature is linked to this concrete function: https://github.com/tensorflow/mesh/blob/efcd89f66d53ffddc5d13d37041d774d1bf7d210/mesh_tensorflow/transformer/utils.py#L500 I was thinking about change the predict logic to add the scores to the predictions dict like below:
However, to calculate scores, it needs the 'targets' feature which is not passed to the function when it's in predict mode. I feel the outputs generated by the predict logic could be used as the 'targets' feature but I've been unable to make it work. Can you please let me know if I'm heading to the right direction and if yes, how can I get the 'targets' to be able to calculate the scores? Thank you. |
I see, I didn't realize you were using an exported model. To address your issue I think the best solution would be to have the model generate log-likelihoods alongside samples in |
Hi @craffel , yes, we need to run this model for real time predictions with a probability score(or some kine of a confidence score for the prediction). At the moment, I'm planning to run an exported model via TF Serving. Is this the right way to use the model? I will try to get the score included in the predict function as this will be a must to have feature for us to use this model. Can you please share some ideas or pseudo code about how this can be done? I'm happy to make a PR if I can get it working. Thank you. |
You should modify the |
Please send a PR when you get this to work or let us know if you need additional help. |
Hi @craffel , @adarob , I've spent some time looking at the t5 code and managed to get the scores added to model output. However, the scores don't look quite right when I compare them with the output from the model.score function. It'll be great if you can take a quick look to see where the problem might be. Below are the changes I made: Changed file 1: /mesh_tensorflow/transformer/transformer.py
to
Changed file 2: /mesh_tensorflow/transformer/utils.py
to
The exported model SignatureDef looks like below:
The problem is the scores look too small(I assume they are log probability) compared to the scores I get from the model.score(input, predicted_text) function. |
Could you post a few examples of what the scores are with your code vs. the scoring code? |
@daphnei , sure, with 10 input text samples, using my code, the scores are:
If I use the model.score function, the output is:
|
@daphnei , I did some debugging and found the root cause of this issue. I think the problem is the raw output of the self.decoder.sample_autoregressive function has tokens after the eos token. In the score function these invalid tokens are removed by the clean_decodes function. After I removed the invalid tokens from the sample_autoregressive output, the scores from my code matches the model.score function. Thanks for your help. |
Would it make sense to return log-probabilities instead of probabilities so as to align with the existing scoring code? |
Thanks for your feedback @daphnei . I've thought about returning the log-prob as the current score functions but for the project I'm working on, they need the texts to come with a probability. If I expose the log-prob in the output, I think there will be a post-processing step required to convert it to prob which is not ideal. There are two options I can think of:
Please let me know your thoughts. |
Sorry for the delayed response. A couple comments: Personally, I think it is preferable to only output logprobs. It is trivial for downstream code using these scores to convert them to probs, and having an extra param seems like it would just over-complicate things. On an unrelated note, if possible, I think it would be a good idea to refractor the logic that is redundant with https://github.com/tensorflow/mesh/blob/master/mesh_tensorflow/transformer/utils.py#L487 to a helper function compute_scores. |
@daphnei would you mind taking a first pass if you have time since you're more familiar with this code? |
Looks good to me. |
Hi there,
Thanks for releasing the code and examples for such a great model.
I've followed the example and been able to train a model using my own dataset and everything is working now.
I have one question though, is it possible to get a probability score for the generated text? When I run the the model in prediction mode, I can only see the inputs and outputs text.
Thanks,
Allen
The text was updated successfully, but these errors were encountered: