피드포워드 하위 계층
피드포워드 하위 계층은 Attention 출력값을 비선형의 추상 표현으로 매핑해 더 복잡한 관계를 잘 포착하도록 도와줘요.
이 연습에서는 인코더 전용 Transformer를 위한 FeedForwardSubLayer를 만들어 보세요. 이 계층은 두 개의 선형 계층 사이에 ReLU 활성화 함수를 두는 구조로 이뤄져요. 또한 입력 임베딩의 차원을 나타내는 d_model과, 두 선형 계층 사이의 차원을 나타내는 d_ff라는 두 매개변수를 받아요.
d_model과 d_ff는 이미 준비되어 있어 바로 사용할 수 있어요.
이 연습은 강의의 일부입니다
PyTorch로 배우는 Transformer 모델
연습 안내
d_model과 계층 사이의 차원d_ff를 사용해, 피드포워드 하위 계층 클래스의 첫 번째·두 번째 선형 계층과 ReLU 활성화를 정의하세요.forward()메서드에서 입력을 계층과 활성화 함수에 순서대로 통과시키세요.- 제공된
d_model과d_ff(각각512,2048으로 설정)를 사용해FeedForwardSubLayer를 인스턴스화하고, 입력 임베딩x에 적용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class FeedForwardSubLayer(nn.Module):
def __init__(self, d_model, d_ff):
super().__init__()
# Define the layers and activation
self.fc1 = ____
self.fc2 = ____
self.relu = ____
def forward(self, x):
# Pass the input through the layers and activation
return self.____(self.____(self.____(x)))
# Instantiate the FeedForwardSubLayer and apply it to x
feed_forward = ____
output = ____
print(f"Input shape: {x.shape}")
print(f"Output shape: {output.shape}")