Giter Club home page Giter Club logo

Comments (2)

tunz avatar tunz commented on June 21, 2024

I followed what tensor2tensor did.
https://github.com/tensorflow/tensor2tensor/blob/v.1.12.0/tensor2tensor/models/transformer.py#L1209

Actually, it does not change the size like that. line 282 removes the last word in the sequence, but it also adds a padding value to the first position of the sequence in line 283. When I insert print functions, output looks like this:

print(target_embedded.size())  # torch.Size([30, 33, 512])
target_embedded = target_embedded[:, :-1]
print(target_embedded.size())  # torch.Size([30, 32, 512])
target_embedded = F.pad(target_embedded, (0, 0, 1, 0))
print(target_embedded.size())  # torch.Size([30, 33, 512])

The reason why we remove the last word is that we don't need to predict the word after the last word. For example, let's think about this case that we use "abcd" as an input. Then, we have to predict a from nothing, and we have nothing to predict from d. For this, we add a padding value in line 283, and remove the last word in line 282 to eliminate unnecessary computations.

  input                  output
| <pad> | --(predict)--> | a |
|   a   | --(predict)--> | b |
|   b   | --(predict)--> | c |
|   c   | --(predict)--> | d |
|   d   |

from transformer-pytorch.

XinDongol avatar XinDongol commented on June 21, 2024

Got it and appreciate your quick reply!

from transformer-pytorch.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.