<jupyter_start><jupyter_text><jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as f

import torchvision
import torchvision.transforms as transforms

from torch.utils.data import DataLoader

import numpy as np
import matplotlib.pyplot as plt
data_trasnform = transforms.ToTensor()
dataset = torchvision.datasets.MNIST('./root' , transform = data_trasnform , download = True , train = True)
testset = torchvision.datasets.MNIST('./root' , transform = data_trasnform , download = True , train = False)
batchsize   = 25
num_workers = 0 
trainloader = DataLoader(dataset , shuffle = True , batch_size = batchsize , num_workers = num_workers)
testloader  = DataLoader(testset , shuffle = True , batch_size = batchsize , num_workers = num_workers)
images , labels = iter(trainloader).next()
images = images.numpy()
fig = plt.figure(figsize = (25,4))

for i in range(batchsize):
  ax = fig.add_subplot(5 , batchsize / 2 , i + 1 , xticks = [] , yticks = [])
  ax.imshow(np.squeeze(images[i]) , cmap = 'gray')
  ax.set_title(labels[i].item())

plt.tight_layout()

class linear_autoencoder(nn.Module):
  def __init__(self,input_dims,n_dims = 32):
    super(linear_autoencoder , self).__init__()
    self.input_dims = input_dims
    self.n_dims = n_dims

    self.in_linear  = nn.Linear(input_dims , n_dims) # Encoding
    self.out_linear = nn.Linear(n_dims , input_dims) # Decoding

  def forward(self , x):
    x = self.in_linear(x)
    x = f.sigmoid(self.out_linear(x))
    return x 

model = linear_autoencoder(784 , 256)
print(model)
# specify loss function
criterion = nn.MSELoss()

# specify loss function
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
def train(n_epochs , model):
  training_loss = []

  for epoch in range(n_epochs):
    trainloss = 0.0
    for batch_i , (images , labels) in enumerate(trainloader):
      optimizer.zero_grad()
      images = images.view(images.size(0), -1)
      output = model(images)
      loss = criterion(output , images) # Here we comapre with the image itself insted of the label
      loss.backward()
      optimizer.step()
      trainloss += loss.item()  
    print("Epoch: {} , Batch: {} , Training Loss: {}".format(epoch + 1 , batch_i + 1 , trainloss / len(trainloader)))
    training_loss.append(trainloss)
        
  print("Training Completed !!!")
def plot_images(model):

  images , labels = iter(testloader).next()
  preds = model(images.view(images.size(0) , -1))
  images = images.numpy()

  fig1 = plt.figure(figsize = (25,4))
  fig2 = plt.figure(figsize = (25,4))

  for i in range(batchsize):
    ax1 = fig2.add_subplot(2 , batchsize / 1 , i + 1 , xticks = [] , yticks = [])
    ax1.imshow(np.squeeze(images[i]) , cmap = 'gray')
    ax1.set_title(labels[i].item())

  for i in range(batchsize):
    ax = fig1.add_subplot(2 , batchsize / 1 , i + 1 , xticks = [] , yticks = [])
    ax.imshow(preds[i].detach().numpy().reshape(28 , 28) , cmap = 'gray')
    ax.set_title("d_"+str(labels[i].item()))

  plt.tight_layout()
train(5 , model)
plot_images(model)<jupyter_output>/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1709: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
  warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
{'fraction_non_alphanumeric': 0.1385083713850837, 'fraction_numerical': 0.01796042617960426, 'mean_word_length': 3.9939209726443767, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '424148', 'license': 'Apache-2.0', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1157, 'n_words': 253, 'path': 'Linear_Autoencoders.ipynb', 'repo_name': 'srimanthtenneti/Autoencoders', 'size': '3285'}