Skip to content
Minsing Log
Go back

English Edition

Why do we transpose when using nn.Embedding?

Data & ML

Motivation

The shape after summing the lyrics encoder and melodyU encoder is (1, 192, 1) - (b, h, time(seq_len)). These are subjected to FFT operations again in the enhanced condition encoder, but when nn.Embedding and transpose were applied in the same way, it produced a clear error.

Problem Definition and Solution Summary

  1. The embedded output does not need to pass through an nn.Embedding layer. + Definition of nn.Embedding

    • nn.Embedding creates a lookup table based on per-token indices. Therefore, the summation of the lyrics representation and melodyU representation is already the sum of the outputs of the Embedding layer; since they are floats and have no indices, nn.Embedding cannot be applied.
  2. transpose is used to normalize the data again after passing through the nn.Embedding layer

    • When [b,h,t] passes through the nn.Embedding layer, it becomes [b,t,h], so transpose is used to normalize it again.
    • It must be [b,h,t] when passing through the ffn of the transformer encoder.

Detailed Explanation

PyTorch’s nn.Conv1d layer expects the input tensor to have the shape [batch, channels, length].
However, tensors that have passed through an embedding in natural language processing and similar tasks usually have the shape [batch, length, embedding_dim] (that is, [batch, sequence length, embedding dimension]).

For example,

Therefore, before inputting data into Conv1d, you must use transpose or permute to switch the order of the two dimensions.
This allows Conv1d to recognize the embedding dimension as the channels and correctly perform the convolution operation while the kernel follows the sequence length[6][8].

“In pytorch your input shape of[6] should actually be[6] where the feature length is represented by the channel dimension and sequence length is the length dimension. Then you can define your conv1d with in/out channels of 768 and 100 respectively to get an output of[6].
… you can convert it to the correct shape with Tensor.transpose.”[6]

In other words,

This is precisely why transpose is performed before Conv1d.

Citations: [1] https://cumulu-s.tistory.com/29 [2] https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose1d.html [3] https://velog.io/@gilson/View-and-Reshape-VS-permute-and-transpose [4] https://jisuhan.tistory.com/128 [5] https://westlife0615.tistory.com/257 [6] https://stackoverflow.com/questions/62372938/understanding-input-shape-to-pytorch-conv1d [7] https://dacon.io/forum/406022 [8] https://discuss.pytorch.org/t/understanding-input-shape-to-pytorch-conv1d/85429 [9] https://www.thespacemoon.com/2021/03/03/pytorch-view-reshape-transpose/ [10] https://sksdudtjs.tistory.com/50 [11] https://iamseungjun.tistory.com/15 [12] https://jimmy-ai.tistory.com/378 [13] https://stackoverflow.com/questions/69915792/pytorch-conv1d-gives-different-size-to-convtranspose1d [14] https://wikidocs.net/64779 [15] https://imlim0813.tistory.com/58 [16] https://velog.io/@workhard/lstm-9.-pytorch-%EC%82%AC%EC%9A%A9%EB%B2%95 [17] https://wikidocs.net/217064 [18] https://leeejihyun.tistory.com/37


Answer from Perplexity: pplx.ai/share


Basis for the shape changing when embedding

The reason that the shape of a tensor passing through an embedding layer becomes [batch, length, embedding_dim] lies in the operating method of the embedding layer and the structure of natural language processing data.

1. Input and Output Structure of the Embedding Layer

“Embedding() receives a 2D integer tensor whose shape is (number of samples, input_length). Here, each sample is the result of integer encoding and is a sequence of integers. Embedding() performs word embedding and returns a 3D floating-point tensor whose shape is (number of samples, input_length, embedding word dimentionality).”

  • [Wikidocs, Keras Embedding Layer Description][5][6]

2. Example of Using an Embedding Layer in Natural Language Processing

For example,

3. Why Does This Structure Result?

4. Evidence from Official Documentation and Textbooks


Summary:
When the input is [batch, sequence_length], the embedding layer converts each index into an embedding vector and returns a tensor in the form [batch, sequence_length, embedding_dim]. This structure consists of each sentence (batch), each word (sequence), and each embedding vector (embedding dimension) in natural language processing[5][6][3].

Citations: [1] https://tech.kakao.com/posts/585 [2] https://wikidocs.net/31379 [3] https://simpling.tistory.com/1 [4] https://ebbnflow.tistory.com/154 [5] https://ingu627.github.io/keras/keras_ch6_org_1/ [6] https://wikidocs.net/33793 [7] https://wikidocs.net/37001 [8] https://velog.io/@oilyhand_01/%EB%94%A5%EB%9F%AC%EB%8B%9D-%EA%B8%B0%EC%B4%88-14 [9] https://velog.io/@yenaryu/%EC%9E%90%EC%97%B0%EC%96%B4%EC%B2%98%EB%A6%AC-%EC%9E%91%EC%82%AC%EA%B0%80-%EC%9D%B8%EA%B3%B5%EC%A7%80%EB%8A%A5-%EB%A7%8C%EB%93%A4%EA%B8%B0 [10] https://heeya-stupidbutstudying.tistory.com/entry/DL-%EC%9E%90%EC%97%B0%EC%96%B4-%EC%B2%98%EB%A6%AC%EC%97%90%EC%84%9C%EC%9D%98-%EB%8B%A8%EC%96%B4-%ED%91%9C%ED%98%84-Word-Embedding [11] https://blog.naver.com/iwin1203/222010971529 [12] https://aigaeddo.tistory.com/37 [13] https://www.youtube.com/watch?v=BbBCLPl0x_U [14] https://www.ibm.com/kr-ko/think/topics/vector-embedding [15] https://sjh9708.tistory.com/231 [16] https://dusanbaek.tistory.com/99 [17] https://wikidocs.net/22660 [18] https://cumulu-s.tistory.com/4 [19] https://soyoung-new-challenge.tistory.com/32


Answer from Perplexity: pplx.ai/share

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
What is nn.Embedding
Next Post
Mean pooling - Let's match the embedding dimensions