인코더 역할은 데이터(x)가 주어졌을 때 디코더가 원래 데이터로 잘 복원할 수 있는 이상적인 확률 분포 p(z|x)를 찾는 것입니다. 변형 오토인코더에서는 이상적인 확률 분포를 찾는 데 변분추론(variational inference)7을 사용합니다.

    이번에는 디코더 네트워크를 정의합니다.

    코드 13-14 디코더 네트워크

    class Decoder(nn.Module):
        def __init__(self, latent_dim, hidden_dim, output_dim):
            super(Decoder, self).__init__()
            self.hidden1 = nn.Linear(latent_dim, hidden_dim)
            self.hidden2 = nn.Linear(hidden_dim, hidden_dim)
            self.output = nn.Linear(hidden_dim, output_dim)
            self.LeakyReLU = nn.LeakyReLU(0.2)
    
        def forward(self, x):
            h = self.LeakyReLU(self.hidden1(x))
            h = self.LeakyReLU(self.hidden2(h))
            x_hat = torch.sigmoid(self.output(h))
            return x_hat ------ 디코더 결과는 시그모이드를 통과했으므로 0~1 값을 갖습니다.

    디코더는 추출한 샘플을 입력으로 받아 다시 원본으로 재구축(재생성)하는 역할을 수행합니다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.