See 240104 AutoEncoder.ipynb
R = C = 28
ENCODED_DIMS = 16
def __init__(self):
super(AutoEncoder, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(R * C, 128),
nn.ReLU(inplace=True),
nn.Linear(128, 64),
nn.ReLU(inplace=True),
nn.Linear(64, 32),
nn.ReLU(inplace=True),
nn.Linear(32, ENCODED_DIMS),
)
self.decoder = nn.Sequential(
nn.Linear(ENCODED_DIMS, 32),
nn.ReLU(inplace=True),
nn.Linear(32, 64),
nn.ReLU(inplace=True),
nn.Linear(64, 128),
nn.ReLU(inplace=True),
nn.Linear(128, R*C),
nn.Tanh(),
)
class AutoEncoder_c2(nn.Module):
def __init__(self):
super(AutoEncoder_c2, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(1, 8, 3, stride=2, padding=1), # (28, 28) -> (30, 30) -> (14, 14)
nn.ReLU(inplace=True),
nn.Conv2d(8, 16, 3, stride=2, padding=1), # (14, 14) -> (16, 16) -> (7, 7)
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 3, stride=2), # (7, 7) -> (3, 3)
nn.ReLU(inplace=True),
nn.Conv2d(32, 64, 3), # (3, 3) -> (1, 1)
nn.Flatten(),
nn.ReLU(inplace=True),
nn.Linear(64, 32),
nn.ReLU(inplace=True),
nn.Linear(32, ENCODED_DIMS_c2),
)
self.decoder = nn.Sequential(
nn.Linear(ENCODED_DIMS_c2, 32),
nn.ReLU(inplace=True),
nn.Linear(32, 64),
nn.ReLU(inplace=True),
nn.Unflatten(1, (64, 1, 1)),
nn.ConvTranspose2d(64, 32, 3), # (1, 1) -> (3, 3)
nn.ReLU(inplace=True),
nn.ConvTranspose2d(32, 16, 3, stride=2), # (3, 3) -> (7, 7)
nn.ReLU(inplace=True),
nn.ConvTranspose2d(16, 8, 3, stride=2, padding=1, output_padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(8, 1, 3, stride=2, padding=1, output_padding=1),
nn.Tanh(),
)
效果如图。
https://www.bilibili.com/video/BV1Jb4y1g7q6/