<jupyter_start><jupyter_text>CNN DetailsThe kernel/filter/weight matrix is a 5x5 matrix. Out channels corresponds to the number of created feature maps, and therefore impacts the number of kernels.MaxPool2D is a 2x2 sliding window taking the maximum value in that window.Since this is the MNIST dataset, each image is 28x28. If we take a batch size of 1, this means our structure is:Input (28x28) **->**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Conv2D (16x28x28) **->** ReLU (16x28x28) **->** MaxPool2D (16x14x14) **->**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Conv2D (32x14x14) **->** RelU (32x14x14) **->** MaxPool2D (32x7x7) **->**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flatten (1568x1) **->** FCN **->**Returned Output (10x1)<jupyter_code>#Derived from https://medium.com/@nutanbhogendrasharma/pytorch-convolutional-neural-network-with-mnist-dataset-4e8a4265e118

class ExampleCNN(nn.Module):
    def __init__(self):
        super(ExampleCNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(5,5), stride=(1,1), padding=2)
        self.activation = nn.ReLU()
        self.pool2d = nn.MaxPool2d(kernel_size=2)
        self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=(5,5), stride=(1,1), padding=2)
        self.fcn = nn.Linear(32 * 7 * 7, 10)
        
    def forward(self, x):
        x = self.pool2d(self.activation(self.conv1(x)))
        x = self.pool2d(self.activation(self.conv2(x)))
        
        x = x.view(x.size(0), -1)
        
        out = self.fcn(x)
        return out
from torch.utils.data import DataLoader

"""
Running in COLAB assumes you've not imported the entire repository and simply saves the data with the notebook

Running in Jupyter Notebook assumes you have imported the entire repository and stores it in the data folder
"""
if RunningInCOLAB:
    train_data = torchvision.datasets.MNIST('data', train=True, transform=ToTensor(), download=True)
    test_data = torchvision.datasets.MNIST('data', train=False, transform=ToTensor(), download=True)
else:
    train_data = torchvision.datasets.MNIST('../../data', train=True, transform=ToTensor(), download=True)
    test_data = torchvision.datasets.MNIST('../../data', train=False, transform=ToTensor(), download=True)
batch_size = 124
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False)
loaders = {'train' : train_loader, 'test' : test_loader}
from torch.autograd import Variable
from torch import optim

cnn = ExampleCNN()


num_epochs = 10

loss_func = nn.CrossEntropyLoss()   
optimizer = optim.Adam(cnn.parameters(), lr = 0.01)   

def train(epochs, network, data_loaders):
    
    network.train()
        
    # Train the model
    total_step = len(data_loaders['train'])
        
    for epoch in range(epochs):
        for i, (x, y) in enumerate(data_loaders['train']):
            
            # gives batch data, normalize x when iterate train_loader
            b_x = Variable(x)   # batch x
            b_y = Variable(y)   # batch y
            output = network(b_x)
            loss = loss_func(output, b_y)
            
            # clear gradients for this training step   
            optimizer.zero_grad()           
            
            # backpropagation, compute gradients 
            loss.backward()                # apply gradients             
            optimizer.step()                
            
            if (i+1) % 100 == 0:
                print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                       .format(epoch + 1, epochs, i + 1, total_step, loss.item()))
                pass
        pass
    pass

train(num_epochs, cnn, loaders)
def test():
    # Test the model
    cnn.eval()    
    
    with torch.no_grad():
        for x, y in loaders['test']:
            out = cnn(x)
            y_prediction = torch.max(out, 1)[1].data.squeeze()
            accuracy = (y_prediction == y).sum().item() / float(y.size(0))
            pass
        print('Test Accuracy of the model on the 10000 test images: %.2f' % accuracy)
    
    pass
test()
sample = next(iter(loaders['test']))
images, labels = sample
actual_number = labels[:10].numpy()
test_output = cnn(images[:10])
y_predictions = torch.max(test_output, 1)[1].data.numpy().squeeze()
print(f'Prediction number: {y_predictions}')
print(f'Actual number: {actual_number}')
print(cnn.conv1.weight)
import matplotlib.pyplot as plt
import numpy as np

layer1_filter = 0

plt.imshow(np.squeeze(cnn.conv1.weight.detach().numpy()[layer1_filter,:,:,:]), cmap='gray')
plt.show()
layer2_filter = 0

plt.imshow(cnn.conv2.weight.detach().numpy()[layer2_filter, layer1_filter, :, :], cmap='gray')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13823529411764707, 'fraction_numerical': 0.030672268907563024, 'mean_word_length': 3.0902061855670104, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '254405', 'license': 'MIT', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1561, 'n_words': 369, 'path': 'tutorials/python/CNNMNIST.ipynb', 'repo_name': 'MJC598/SystemID', 'size': '4760'}