| import torch
|
| import torch.nn as nn
|
|
|
| class DenseAutoencoder(nn.Module):
|
| def __init__(self, input_dim=784, hidden_dims=[256, 64], latent_dim=32):
|
| super().__init__()
|
|
|
|
|
| encoder_layers = []
|
| prev_dim = input_dim
|
| for h in hidden_dims:
|
| encoder_layers += [nn.Linear(prev_dim, h), nn.ReLU()]
|
| prev_dim = h
|
| encoder_layers.append(nn.Linear(prev_dim, latent_dim))
|
| self.encoder = nn.Sequential(*encoder_layers)
|
|
|
|
|
| decoder_layers = []
|
| prev_dim = latent_dim
|
| for h in reversed(hidden_dims):
|
| decoder_layers += [nn.Linear(prev_dim, h), nn.ReLU()]
|
| prev_dim = h
|
|
|
|
|
| decoder_layers.append(nn.Linear(prev_dim, input_dim))
|
| self.decoder = nn.Sequential(*decoder_layers)
|
|
|
| def forward(self, x):
|
| z = self.encoder(x)
|
| x_hat = self.decoder(z)
|
| return x_hat, z
|
|
|
|
|
| if __name__ == "__main__":
|
|
|
|
|
| model = DenseAutoencoder(latent_dim=32)
|
|
|
|
|
|
|
|
|
| batch_size = 64
|
| x = torch.randn(batch_size, 784)
|
|
|
| print(f"์๋ณธ ๋ฐ์ดํฐ(x) shape: {x.shape}")
|
|
|
|
|
| x_hat, z = model(x)
|
|
|
| print(f"์์ถ๋ ์ ์ฌ ๋ฒกํฐ(z) shape: {z.shape}")
|
| print(f"๋ณต์๋ ๋ฐ์ดํฐ(x_hat) shape: {x_hat.shape}")
|
|
|
|
|
|
|
| loss_fn = nn.MSELoss()
|
| loss = loss_fn(x_hat, x)
|
|
|
| print(f"\n๊ณ์ฐ๋ ์์ค(MSELoss): {loss.item()}")
|
|
|
|
|
| loss.backward()
|
|
|
| print("์ญ์ ํ ์๋ฃ") |