question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

top_p returns wrong values and re-orders the data

See original GitHub issue
from routing_transformer.autoregressive_wrapper import top_p, top_k
import torch
import torch.nn.functional as F

test_tensor = torch.tensor([[0.1, 0.2, 0.15, 0.4, 0.3, 0.001, 0.01]])
threshold=0.3

print(test_tensor)
print(F.softmax(test_tensor, dim=-1))

print("Top K")
print(top_k(test_tensor, thres=threshold))
print(F.softmax(top_k(test_tensor, thres=threshold), dim=-1))

print("Top P")
print(top_p(test_tensor, thres=threshold))
print(F.softmax(top_p(test_tensor, thres=threshold), dim=-1))

Output:

tensor([[0.1000, 0.2000, 0.1500, 0.4000, 0.3000, 0.0010, 0.0100]])
tensor([[0.1325, 0.1464, 0.1393, 0.1789, 0.1618, 0.1200, 0.1211]])
Top K
tensor([[  -inf, 0.2000, 0.1500, 0.4000, 0.3000,   -inf,   -inf]])
tensor([[0.0000, 0.2338, 0.2224, 0.2855, 0.2584, 0.0000, 0.0000]])
Top P
tensor([[  -inf,   -inf, 0.3000,   -inf, 0.4000,   -inf,   -inf]])
tensor([[0.0000, 0.0000, 0.4750, 0.0000, 0.5250, 0.0000, 0.0000]])

Thanks for writing this library! I think there is a bug in top_p, with two symptoms:

  1. The wrong results are filtered. It defines the threshold in the opposite way as top_k. So setting thres=0.9 results in everything being returned until a cumulative probability of 0.1 is reached.
  2. The results themselves are gathered twice (instead of gathered and scattered) and as a result the returned tensor’s values are distributed in a nearly random order.

I’ll send over a PR in a few, hope it’s helpful!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lucidrainscommented, Jun 15, 2020

@tomweingarten Hi Tom, just wanted to let you know that I’ve made an important update in the latest version per the author’s suggestion

1reaction
tomweingartencommented, Jun 5, 2020

Very well so far! Only just beginning to run tests to compare it to XLNet but it seems very promising so far.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reorder bars in geom_bar ggplot2 by value - Stack Overflow
I am trying to make a bar-plot where the plot is ordered from the miRNA with the highest value to the miRNA with...
Read more >
The Reorder Point Formula: All You Need to Know [+ Video]
The reorder point formula is your average daily use, multiplied by your average lead time in days – plus safety stock.
Read more >
Change the order in which stacked objects, placeholders, or ...
To reorder objects on a slide, right-click the object (or set of grouped objects), and then click Bring to Front or Send to...
Read more >
Draw a Heat Map - R
A heat map is a false color image (basically image(t(x)) ) with a dendrogram added to the left side and to the top....
Read more >
Reorder an array according to given indexes - GeeksforGeeks
1) Do following for every element arr[i] a) While index[i] is not equal to i (i) Store array and index values of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found