nn.Linear(prev_hdim, hdim, bias=True) : Linear layernn.ReLU(True) : 활성화 함수nn.Dropout2d(p=0.5) : dropout (p = 노드를 얼마나 활용 안할지)model.forward(x_torch) = model(x_torch)model.eval() : 평가모드로 변경 (drop out off, 테스트 데이터를 학습 안하기 위해)model.train() : 학습 모드로 변경model.init_param() : parameter 초기화optm.zero_grad() : gradient 초기화loss_out.backward() : backpropagateoptm.step() : optimizer 업데이트optm_sgd = optim.SGD(model.parameters(), lr=LEARNING_RATE) : SGD 사용optm_momentum = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9) : 모멘텀 사용optm_adam = optim.Adam(model.parameters(), lr=LEARNING_RATE) : Adam 사용nn.Conv2d(in_channels, out_channels, kernel_size, padding) : convloution layernn.BatchNorm2d(dims) : batch-normnn.MaxPool2d(kerner_size=(2,2), stride=(2,2)) : max-poolingnn.LSTM(input_size, hidden_size, num_layers, batch_first) : LSTM layerX [N x L x Q]: 차원이 Q, 길이가 L인 sequence N개Y [N x L x D]: 차원이 D(feature), 길이가 L인 sequence N개hn [K x N x D]: 차원이 D(feature)인 hidden state가 N개 있는 layer가 K개cn [K x N x D] : 차원이 D(cell)인 hidden state가 N개 있는 layer가 K개
d_K = K.size()[-1] : Key 차원 scores = Q.matmul(K.transpose(-2,-1)) / np.sqrt(d_K) : softmax 내부식 attention = F.softmax(scores, dim=-1) out = attention.matmul(V)
Q_split = Q_feat.view(n_batch, -1, n_head, d_head).permute(0, 2, 1, 3) : [n_batch, n_head, n_Q, d_head]d_K = K.size()[-1] : Key 차원scores = torch.matmul(Q_split, K_split.permute(0, 1, 3, 2)) / np.sqrt(d_K)attention = torch.softmax(scores, dim=-1)x_raw = torch.matmul(self.dropout(attention), V_split) : dropout은 논문에는 언급 Xif mask is not None:
scores = scores.masked_fill(mask==0, -1e9)기존 NLP에서는 나열된 문장이 입력 / 이미지를 입력으로 사용하기 위해서, 여러개의 Patch로 나눕니다.
마지막 Classification을 위해, 나눠진 Patch의 맨 앞에 Special Token 추가



코드

c = repeat(cls_token, ‘() n d → b n d’, b=batch)x = torch.cat((c, x), dim=1)x = x + positions# query, key , value
Q = self.query(x)
K = self.key(x)
V = self.value(x)
Q = rearrange(Q, 'b q (h d) -> b h q d', h=self.num_heads)
K = rearrange(K, 'b k (h d) -> b h d k', h=self.num_heads)
V = rearrange(V, 'b v (h d) -> b h v d', h=self.num_heads)
# scaled dot-product
weight = torch.matmul(Q, K)
weight = weight * self.scaling
attention = torch.softmax(weight, dim=-1)
attention = self.att_drop(attention)
context = torch.matmul(attention, V)
context = rearrange(context, 'b h q d -> b q (h d)')
x = self.linear(context)x = linear_1(x)x = nn.functional.gelu(x)x = dropout(x)x = linear_2(x)Train






# model
self.model = nn.Sequential(
nn.Linear(1024, 512),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(512, 512),
nn.Dropout(p=0.2),
nn.ReLU()
)
self.mu = nn.Linear(512, latent_dim)
self.logvar = nn.Linear(512, latent_dim)
# forward
img_flat = img.view(img.shape[0], -1)
x = self.model(img_flat)
mu = self.mu(x)
logvar = self.logvar(x)
z = reparameterization(mu, logvar)Decoder에 들어가기 전, Encoder 아웃풋인 μ(mu)와 σ(sigma)가 나오게 된다. p(z)에서 샘플링을 할때, 데이터의 확률 분포와 같은 분포에서 샘플을 뽑아야하는데, backpropagation을 하기 위해선, reparametrization의 과정을 거친다.
즉, 정규분포에서 z를 샘플링 하는 것

# model
self.model = nn.Sequential(
nn.Linear(latent_dim, 512),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(512, 512),
nn.BatchNorm1d(512),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(512, 1024),
nn.Tanh(),
)
# forward
q = self.model(z)
q = q.view(q.shape[0], *img_shape)self.model = nn.Sequential(
nn.Linear(latent_dim, 512),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(512, 256),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(256, 1),
nn.Sigmoid(),
)
# forward
x = self.model(z)
노이즈로부터 차근히 학습되어 생성된 결과를 확인할 수 있다.