Skip to content

[Help Wanted] Why take the log function and then apply exp? #1778

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

Closed
Superhzf opened this issue Dec 24, 2021 · 4 comments · Fixed by #2402
Closed

[Help Wanted] Why take the log function and then apply exp? #1778

Superhzf opened this issue Dec 24, 2021 · 4 comments · Fixed by #2402
Labels
docathon-h1-2023 A label for the docathon in H1 2023 easy intro question

Comments

@Superhzf
Copy link
Contributor

Superhzf commented Dec 24, 2021

In line of code, you calculate positional encoding for Transformers by taking the log first and then apply the exponential function.

Would you please elaborate on why you do this instead of directly doing the calculation?

I'm aware that log transformation can make multiplication become addition, but it seems that this is not the case here.

cc @suraj813

@svekars
Copy link
Contributor

svekars commented Mar 6, 2023

@albanD can you respond to this?

@albanD
Copy link
Contributor

albanD commented Mar 7, 2023

Which log/exp are you talking about exactly?
If you mean this line

div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
then I think this is just to make it easier when you multiply with the arange that has to be in the log space.

@svekars svekars added easy docathon-h1-2023 A label for the docathon in H1 2023 labels May 31, 2023
@YoushaaMurhij
Copy link
Contributor

\assigntome

carljparker added a commit that referenced this issue Jun 2, 2023
* Update transformer_tutorial.py

 Add description for positional encoding calculation for Transformers

* Update Positional Encoding description in transformer_tutorial.py

* Update transformer_tutorial.py

---------

Co-authored-by: Carl Parker <[email protected]>
svekars pushed a commit that referenced this issue Jun 2, 2023
svekars pushed a commit that referenced this issue Jun 2, 2023
@HuangZurong
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docathon-h1-2023 A label for the docathon in H1 2023 easy intro question
Projects
None yet
5 participants