입력 임베딩 만들기
이제 직접 transformer 모델을 만들어 볼 시간이에요. 첫 단계는 입력 토큰 ID를 임베딩하는 것입니다!
다음 매개변수를 갖는 InputEmbeddings 클래스를 정의하세요:
vocab_size: 모델 어휘의 크기d_model: 입력 임베딩의 차원 수
torch와 math 라이브러리, 그리고 torch.nn은 nn으로 이미 임포트되어 있어요. 이들은 강의 전반의 연습 문제에서 사용됩니다.
이 연습은 강의의 일부입니다
PyTorch로 배우는 Transformer 모델
연습 안내
- 모델 차원 수와 어휘 크기를 각각
d_model과vocab_size인자로 설정하세요. - 임베딩 레이어를 인스턴스화하세요.
- 임베딩에
self.d_model의 제곱근을 곱한 값을 반환하세요. vocab_size를 10,000,d_model을 512로 하여InputEmbeddings를 인스턴스화하고 이를token_ids에 적용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class InputEmbeddings(nn.Module):
def __init__(self, vocab_size: int, d_model: int) -> None:
super().__init__()
# Set the model dimensionality and vocabulary size
self.d_model = ____
self.vocab_size = ____
# Instantiate the embedding layer
self.embedding = ____
def forward(self, x):
# Return the embeddings multiplied by the square root of d_model
return ____
# Instantiate InputEmbeddings and apply it to token_ids
embedding_layer = ____
output = ____
print(output.shape)