<jupyter_start><jupyter_text><jupyter_code>#@title IMPORTS
import numpy as np
import math
import time
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from IPython.display import clear_output
#@title INSPECTING DEVICE
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
#@title DOWNLOAD DATA
transform = transforms.ToTensor()

# load the training and test datasets
train_data = datasets.CIFAR10(root='data', train=True,
                                   download=True, transform=transform)
test_data = datasets.CIFAR10(root='data', train=False,
                                  download=True, transform=transform)
#@title HYPERPARAMETERS AND DATALOADERS
batch_size = 64
epochs = 100
learning_rate = 1e-3

# prepare data loaders
train_loader = torch.utils.data.DataLoader(train_data,
                                           batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, 
                                          batch_size=batch_size, shuffle=True)
#@title PREVIEW THE IMAGES
def imshow(img):    
    plt.imshow(np.transpose(img, (1, 2, 0)))  # convert from Tensor image
    
# specify the image classes
classes = ['airplane', 'automobile', 'bird', 'cat', 'deer',
           'dog', 'frog', 'horse', 'ship', 'truck']

def plot_images(images, labels, no):  
  rows = int(math.sqrt(no))
  plt.ion()
  fig = plt.figure(figsize=(rows*2, rows*2))      
  for idx in np.arange(no):
      ax = fig.add_subplot(rows, no/rows, idx+1, xticks=[], yticks=[])
      imshow(images[idx])
      ax.set_title(classes[labels[idx]]) 
  time.sleep(0.1)
  plt.pause(0.0001)  

 # obtain one batch of training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy() # convert images to numpy for display

plot_images(images,labels,25)   
#@title CONVOLUTIONAL AUTOENCODER
class ConvAutoencoder(nn.Module):
  def __init__(self):
    super(ConvAutoencoder, self).__init__()
    self.encoder = nn.Sequential(
      nn.Conv2d(3, 16, 3, padding=1), 
      nn.ReLU(True),
      nn.MaxPool2d(2, 2),
      nn.Conv2d(16, 4, 3, padding=1), 
      nn.ReLU(True),
      nn.MaxPool2d(2, 2)
    )
    self.decoder = nn.Sequential(
      nn.ConvTranspose2d(4, 16, 2, stride=2),
      nn.ReLU(True),
      nn.ConvTranspose2d(16, 3, 2, stride=2),
      nn.Sigmoid()
    )

  def forward(self, x):        
    x = self.encoder(x)
    x = self.decoder(x)
    return x

model = ConvAutoencoder()
print(model)
#@title LOSS AND OPTIMIZER
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
#@title TRAINING
for epoch in range(epochs):    
  train_loss = 0.0    
  for data in train_loader:        
    images, labels = data        
    optimizer.zero_grad()        
    generated = model(images)        
    loss = loss_fn(generated, images)
    loss.backward()
    optimizer.step()
    train_loss += loss.item()*images.size(0)
          
  train_loss = train_loss/len(train_loader)
  clear_output()
  print(f'Epoch: {epoch+1} Training Loss: {train_loss:.3f}')    
  plot_images(generated.detach(),labels,16)<jupyter_output>Epoch: 100 Training Loss: 0.421
{'fraction_non_alphanumeric': 0.12154360376785171, 'fraction_numerical': 0.02127013065937405, 'mean_word_length': 3.146095717884131, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '602309', 'license': 'Apache-2.0', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1102, 'n_words': 258, 'path': 'GEN_2_conv_autoencoder.ipynb', 'repo_name': 'cxbxmxcx/GenReality', 'size': '3291'}