Skip to content

Positional encoding potential size issue #63

@gggh900

Description

@gggh900

I am through one of the most complicated and final stage of writing the transformer whole toward the end of ch10. Tons of typos are corrected and now, this line is giving error:

encoded = scaled_x + self.pe[:,:x.size(1),:]
          ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

RuntimeError: The size of tensor a (2) must match the size of tensor b (6) at non-singleton dimension 2

This is from positionalEncoding.forward() class and from full sequence example given starting in around page 433, it is not working.
Now, i see same PE class works OK with minimal example given in around p416 which i called layernorm example:
ch10-p416-layernorm-example.py
when the sizes are such:

full_train=torch.as_tensor(points).float()
PositionalEncoding.forward() entered...

['scaled_x'] : <class 'torch.Tensor'> torch.Size([3, 2, 4])


['self_pe'] : <class 'torch.Tensor'> torch.Size([1, 2, 4])


['self_pe_1'] : <class 'torch.Tensor'> torch.Size([1, 2, 4])


However from example in p433, as you can see, sizes has become as follows during decode stage (did not see in encode stage:

PositionalEncoding.forward() entered...

['scaled_x'] : <class 'torch.Tensor'> torch.Size([16, 2, 2])


['self_pe'] : <class 'torch.Tensor'> torch.Size([1, 100, 6])


['self_pe_1'] : <class 'torch.Tensor'> torch.Size([1, 2, 6])

I see self_pe (which is actually self.pe) has formed into 1,100,6 possibly due to initialization in prob'ly DecoderTransf:
with max_len=100, d_model=6.

I see from smaller example, addition can take place as long as dim 2 and 3 equal ant not necessarily however from bigger example (full_seq), it seems tobe failing due to both dim2 and 3 is different.
I am still looking around for the weaklink, in which If I find, I will close this issue. However, if there is a typo in book, pls let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions