<jupyter_start><jupyter_text>1、Load data<jupyter_code>dataMNIST_train = dsets.MNIST(
    root = 'data',
    train = True,
    download = True,
    transform = transforms.Compose([
        transforms.Resize((32,32)),
        transforms.ToTensor(),
       ])
)
dataMNIST_test = dsets.MNIST(
    root = 'data',
    train = False,
    download = True,
    transform = transforms.Compose([
        transforms.Resize((32,32)),
        transforms.ToTensor(),
       ])
)
dataLoaderMNIST_train = torch.utils.data.DataLoader(
    dataset = dataMNIST_train,
    batch_size = 128,
    shuffle = True,
)
dataLoaderMNIST_test = torch.utils.data.DataLoader(
    dataset = dataMNIST_test,
    batch_size = 128,
    shuffle = True,
)
x,y = iter(dataLoaderMNIST_train).next()
plt.imshow(x[0][0],interpolation = 'bilinear')<jupyter_output><empty_output><jupyter_text>2、model<jupyter_code>class AE(nn.Module):
    def __init__(self):
        super(AE, self).__init__()
        self.encoder = nn.Sequential(
            #(32,32)
            nn.Conv2d(1,16,3,stride = 2,padding = 1),#(16,16)
            nn.ReLU(),
            nn.Conv2d(16,32,3,stride = 2,padding = 1),#(8,8)
            nn.ReLU(),
            nn.Conv2d(32,64,3,stride = 2,padding = 1),#(4,4)
            nn.ReLU(),
            nn.Conv2d(64,128,3,stride = 2,padding = 1),#(2,2)
            nn.ReLU(),
            nn.Conv2d(128,256,3,stride = 2,padding = 1),#(1,1)
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(256,12),
            nn.Sigmoid(),
        )
        
        
        self.decoder = nn.Sequential(
            nn.Linear(12,256),
            nn.ReLU(),
            nn.Unflatten(1,(256,1,1)),
            #(1,1)
            nn.ConvTranspose2d(256,128,4,stride = 2,padding = 1),#(2,2)
            nn.ReLU(),
            nn.ConvTranspose2d(128,64,4,stride = 2,padding = 1),#(4,4)
            nn.ReLU(),
            nn.ConvTranspose2d(64,32,4,stride = 2,padding = 1),#(8,8)
            nn.ReLU(),
            nn.ConvTranspose2d(32,16,4,stride = 2,padding = 1),#(16,16)
            nn.ReLU(),
            nn.ConvTranspose2d(16,1,4,stride = 2,padding = 1),#(32,32)
            nn.ReLU(),
        )
        
    def forward(self,x):
#         x.cu
        code = self.encoder(x)
        y = self.decoder(code)
        return y,code
ae = AE()
ae.to(device)
x,y = iter(dataLoaderMNIST_train).next()
y_,code = ae(x.cuda(device))
y_[0][0]
plt.imshow(y_[0][0].cpu().detach().numpy(),interpolation = 'bilinear')
optimizer = optim.Adam(ae.parameters(),lr = 0.001)
loss_func1 = nn.MSELoss()
ae.train()
for i in range(5):
    for step,(x,y) in enumerate(dataLoaderMNIST_train):
        x = x.cuda(device)
#         y = F.one_hot(y).type_as(x).cuda(device)
        y_,code = ae(x)
        loss = loss_func1(y_,x)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print('\r epoch:{epoch:3}--step:{step:5}--loss:{loss:.7f}'.format(epoch=i,step=step,loss=loss),end='')
    print()
plt.imshow(y_[0][0].cpu().detach().numpy(),interpolation = 'bilinear')
plt.imshow(x[0][0].cpu().detach().numpy(),interpolation = 'bilinear')
s = torch.rand(1,12).cuda(device)
y_ = ae.decoder(s)
plt.imshow(y_[0][0].cpu().detach().numpy(),interpolation = 'bilinear')
class AE1(nn.Module):
    def __init__(self):
        super(AE1, self).__init__()
        self.encoder = nn.Sequential(
            #(32,32)
            nn.Conv2d(1,16,3,stride = 2,padding = 1),#(16,16)
            nn.ReLU(),
            nn.Conv2d(16,32,3,stride = 2,padding = 1),#(8,8)
            nn.ReLU(),
            nn.Conv2d(32,64,3,stride = 2,padding = 1),#(4,4)
            nn.ReLU(),
            nn.Conv2d(64,128,3,stride = 2,padding = 1),#(2,2)
            nn.ReLU(),
            nn.Conv2d(128,256,3,stride = 2,padding = 1),#(1,1)
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(256,12),
            nn.ReLU(),
        )
        
        self.fc1 = nn.Sequential(
            nn.Linear(10,10),
            nn.Softmax(),
        )
        
        self.fc2 = nn.Sequential(
            nn.Linear(2,2),
            nn.Sigmoid(),
        )
        
        self.decoder = nn.Sequential(
            nn.Linear(12,256),
            nn.ReLU(),
            nn.Unflatten(1,(256,1,1)),
            #(1,1)
            nn.ConvTranspose2d(256,128,4,stride = 2,padding = 1),#(2,2)
            nn.ReLU(),
            nn.ConvTranspose2d(128,64,4,stride = 2,padding = 1),#(4,4)
            nn.ReLU(),
            nn.ConvTranspose2d(64,32,4,stride = 2,padding = 1),#(8,8)
            nn.ReLU(),
            nn.ConvTranspose2d(32,16,4,stride = 2,padding = 1),#(16,16)
            nn.ReLU(),
            nn.ConvTranspose2d(16,1,4,stride = 2,padding = 1),#(32,32)
            nn.ReLU(),
        )
        
    def forward(self,x,y):
        GT = F.one_hot(y).type_as(x)
        
        code = self.encoder(x)
        clss = self.fc1(code[:,:10])
        feature = self.fc2(code[:,10:])
        
        x1 = torch.hstack((GT,feature))
        y = self.decoder(x1)
        return y,clss,feature
ae1 = AE1()
ae1.to(device)
optimizer1 = optim.Adam(ae1.encoder.parameters(),lr = 0.001)
optimizer2 = optim.Adam(ae1.decoder.parameters(),lr = 0.001)
loss_func1 = nn.MSELoss()
loss_func2 = nn.CrossEntropyLoss()
x,y = iter(dataLoaderMNIST_train).next()
y_,clss,feature = ae1(x.cuda(device),y.cuda(device))
y_[0][0]
optimizer3 = optim.Adam(ae1.parameters(),lr = 0.001)
ae1.train()
for i in range(5):
    for step,(x,y) in enumerate(dataLoaderMNIST_train):
        x = x.cuda(device)
        y = y.cuda(device)

        y_,clss,feature = ae1(x,y)
        loss1 = loss_func1(y_,x)
        loss2 = loss_func2(clss,y)
        
        loss = loss1 + loss2
#         optimizer1.zero_grad()
#         optimizer2.zero_grad()
        optimizer3.zero_grad()
        loss1.backward(retain_graph=True)
        loss2.backward(retain_graph=True)
#         optimizer1.step()
#         optimizer2.step()
        optimizer3.step()
        
        
        
        print('\r epoch:{epoch:3}--step:{step:5}--loss:{loss:.7f}'.format(epoch=i,step=step,loss=loss),end='')
    print()
plt.imshow(y_[5][0].cpu().detach().numpy(),interpolation = 'bilinear')
plt.imshow(x[5][0].cpu().detach().numpy(),interpolation = 'bilinear')
feature[5]
clss[5]
clss = torch.FloatTensor([[0,0,0,0,0,0,0,0,0,1]]).cuda(device)
feature = torch.FloatTensor([[0.5,0.1]]).cuda(device)

code = torch.hstack((clss,feature)).cuda(device)
test = ae1.decoder(code)
plt.imshow(test[0][0].cpu().detach().numpy(),interpolation = 'bilinear')
clss = torch.FloatTensor([[0,0,0,1,0,0,0,0,0,0]]).cuda(device)
feature = torch.FloatTensor([[0.5,0.1]]).cuda(device)

code = torch.hstack((clss,feature)).cuda(device)
test = ae1.decoder(code)
plt.imshow(test[0][0].cpu().detach().numpy(),interpolation = 'bilinear')
# torch.save(ae1,'model/AE/MNIST-AE1.pt')
# mymodel = torch.load('model/AE/MNIST-AE1.pt')
10*91+20*61
logvar = torch.Tensor([[1,2,3]])
logvar
logvar.mul(0.5)
torch.exp(logvar.mul(0.5))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18534850640113798, 'fraction_numerical': 0.06073968705547653, 'mean_word_length': 2.4081434803683957, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '407051', 'license': 'MIT', 'n_tokens_mistral': 2934, 'n_tokens_neox': 2723, 'n_words': 330, 'path': 'AutoEncoder/MNIST-AE.ipynb', 'repo_name': 'Gwanjyun/GwanjyunDeepLearning-2021Summer', 'size': '7030'}