Search code examples
pythonpytorchautoencoderbatchsize

How to save output after two layers of neural network in Pytorch


I wrote a convolutional autoencoder that was supposed to work on the ORL dataset (400 images in dataset, size 32*32) in csv. format. What I want is to observe how the data changes through the autoencoder. That's why I wrote a test1 function in the class that goes through only the first two layers.

class ConvAutoencoder(nn.Module):
    def __init__(self):
        super(ConvAutoencoder, self).__init__()
        ## encoder layers ##
        self.conv1 = nn.Conv2d(1, 3, 3)  
        self.conv2 = nn.Conv2d(3 ,1, 3)  
        self.conv3 = nn.Conv2d(1, 3, 3)
        self.conv4 = nn.Conv2d(3, 1, 3)
        
        ## decoder layers ##
        self.t_conv1 = nn.ConvTranspose2d(1, 3, 3)
        self.t_conv2 = nn.ConvTranspose2d(3, 1, 3)
        self.t_conv3 = nn.ConvTranspose2d(1, 3, 3)
        self.t_conv4 = nn.ConvTranspose2d(3, 1, 3)
    
    def forward(self, x):
        
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = F.relu(self.conv4(x))
        ## decode ##
       
        x = F.relu(self.t_conv1(x))
        x = F.relu(self.t_conv2(x))
        x = F.relu(self.t_conv3(x))
        x = (self.t_conv4(x))
        return x

    def test1(self, x):
      x = F.relu(self.conv1(x))
      x = F.relu(self.conv2(x))
      return x

But the problem arises when I really want to check what is in those two layers.

mn_dataset_loader = torch.utils.data.DataLoader(dataset=custom_mnist_from_csv,
                                                    batch_size=200,
                                                    shuffle=False)
for epoch in range(200):
  running_loss = 0
  br = 0
  for data in  mn_dataset_loader:
            inputs = data[0].to(device, non_blocking=True)
            optimizer.zero_grad()
            outputs = model(inputs).to(device)
            i1 = model.test1(inputs).to(device)
            i1 = torch.squeeze(i1, 0)
            i1 = i1.flatten().to(device)
            i1=i1.unsqueeze(0)
            print(i1.shape)
            loss = lossFn(data.to(device), outputs)
            loss.backward()
            optimizer.step()
 
            running_loss += loss.item()
  print('[Epoch %d] loss: %.3f' %
                      (epoch + 1, running_loss/len(mn_dataset_loader)))
 
  print('Done Training')

My question is actually why is i1 what is the output after only 2 layers of the form torch.Size([1, 784])? Why not torch.Size([400, 784]) because there are actually so many images? So how to actually see what is actually the output of the first two layers?


Solution

  • You specify a batch size of 200 but then take only the first element (inputs = data[0]) If you want to run it on all images change the batch size to 400 and don't take only the first element