본문으로 건너뛰기
Minsing Log
뒤로 가기

왜 nn.Embedding을 하면 transpose를 하는걸까?

Data & ML

동기

lyrics encoder과 melodyU encoder를 Summation한 뒤의 shape는 (1, 192, 1) - (b, h, time(seq_len))이다. 요녀석들은 enhanced condition encoder에서 다시 FFT 연산을 하는데 동일하게 nn.Embedding과 transpose를 해주었더니 시원하게 오류가 나온다.

문제정의 및 해결 요약

  1. 임베딩된 산출물은 nn.Embedding layer를 통과할 필요가 없음. + nn.Embedding의 정의

    • nn.Embedding은 token별 인덱스를 기반으로 lookup table을 만드는 녀석이다. 고로 lyrics representation과 melodyU representation의 summation은 이미 Embedding의 결과물끼리 더한것이므로 float에다가 인덱스도 없으므로 nn.Embedding을 할수 없다.
  2. transpose는 nn.Embedding layer를 통과해서 다시 정상화 시키기 위한 녀석

    • [b,h,t]가 nn.Embedding layer를 통과하면 [b,t,h]가 되어서 다시 정상화 시키기 위해서 transpose를 시킨다.
    • transformer encoder의 ffn이 통과할때는 [b,h,t]로 되어야함.

세부설명

PyTorch의 nn.Conv1d 레이어는 입력 텐서의 shape이 [batch, channels, length] 형태이길 기대합니다.
하지만 자연어 처리 등에서 임베딩을 거친 텐서는 보통 [batch, length, embedding_dim] (즉, [배치, 시퀀스 길이, 임베딩 차원]) 형태로 나옵니다.

예를 들어,

따라서, Conv1d에 입력하기 전에 transpose 또는 permute를 사용해 두 차원의 순서를 바꿔줘야 합니다.
이렇게 해야만 Conv1d가 임베딩 차원을 채널로 인식하고, 시퀀스 길이를 커널이 따라가며 합성곱 연산을 올바르게 수행할 수 있습니다[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]

즉,

이것이 바로 Conv1d 앞에서 transpose를 하는 이유입니다.

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


embedding하면 shape가 달라지는 근거

임베딩 레이어(Embedding Layer)를 거친 텐서의 shape이 [batch, length, embedding_dim]이 되는 이유는 임베딩 레이어의 동작 방식과 자연어 처리 데이터의 구조 때문입니다.

1. 임베딩 레이어의 입력과 출력 구조

“Embedding()은 (number of samples, input_length)인 2D 정수 텐서를 입력받습니다. 이 때 각 sample은 정수 인코딩이 된 결과로, 정수의 시퀀스입니다. Embedding()은 워드 임베딩 작업을 수행하고 (number of samples, input_length, embedding word dimentionality)인 3D 실수 텐서를 리턴합니다.”

  • [위키독스, 케라스 임베딩 층 설명][5][6]

2. 자연어 처리에서 임베딩 레이어 사용 예시

예를 들어,

3. 왜 이런 구조가 되는가?

4. 공식 문서 및 교재 근거


정리:
임베딩 레이어는 입력이 [batch, sequence_length]일 때, 각 인덱스를 임베딩 벡터로 변환해 [batch, sequence_length, embedding_dim] 형태의 텐서를 반환합니다. 이 구조는 자연어 처리에서 각 문장(배치), 각 단어(시퀀스), 각 임베딩 벡터(임베딩 차원)로 구성되기 때문입니다[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 로 구독하거나 이메일 로 알림을 요청해 주세요.


이 글 공유하기:

댓글

GitHub 계정으로 로그인해 댓글을 남길 수 있습니다.


이전 글
nn.Embedding이란
다음 글
Mean pooling - 임베딩 차원 맞춰보자이