<jupyter_start><jupyter_text><jupyter_code># Let's start by loading the modules
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import cifar10
import tensorflow as tf<jupyter_output>Using TensorFlow backend.<jupyter_text>Now we load the MNIST data (LeCun et al 1998). These are grey scale images of handwritten digits. For many years, this dataset has been used in testing computer vision and machine learning algorithms.---<jupyter_code>(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print("shape of the training sample=", x_train.shape)
filters = {'horizontal_edge': np.array([ [-1, 0, -1], [1, 0, -1], [1, 0, -1]]), 
           'vertical_edge': np.array([[1,1,1], [0,0,0],[-1,-1,-1]]), 
           'sobel': np.array([[1,0,-1], [2,0,-2],[1,0,-1]])}
def conv(input, filter_name):
  input = input/np.max(input)
  filter = filters[filter_name]
  Nx = input.shape[0]
  Ny = input.shape[1]
  output = np.zeros((Nx-1, Ny-1))
  for i in range(1, Nx-1):
    for j in range(1,Ny-1):
      block = 0.0
      block = block + (input[i - 1, j-1] * filter[0][0])
      block = block + (input[i, j-1] * filter[0][1])
      block = block + (input[i + 1, j-1] * filter[0][2])
      block = block + (input[i-1, j] * filter[1][0])
      block = block + (input[i, j] * filter[1][1])
      block = block + (input[i+1, j] * filter[1][2])
      block = block + (input[i-1, j+1] * filter[2][0])
      block = block + (input[i, j+1] * filter[2][1])
      block = block + (input[i+1, j+1] * filter[2][2])
      block = block 
      if block < 0 :
        block == 0 
      output[i, j] = block       
  
  return output
fig, ax = plt.subplots(nrows = 3, ncols = 4, figsize = (10,10))

for i in range(3):
  input = x_train[i]
  ax[i, 0].imshow(input)
  ax[i,0].set_xticks([])
  k = 1
  for (key, values) in filters.items():
    output = conv(input, key)
    ax[i, k].imshow(output)
    ax[i,k].set_title(key)
    k +=1<jupyter_output><empty_output><jupyter_text>Let's look at another dataset<jupyter_code>(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')<jupyter_output>x_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples<jupyter_text>Note the shape of the array:$(N_x, N_y, N_f) = (28, 28, 3)$; where $N_x$ is the number of pixels along the horizontal axis, $N_y$ is the number of pixels along the vertical axis, and $N_f$ is the number of filters: $GRB$.<jupyter_code>fig, ax = plt.subplots(nrows = 3, ncols = 4, figsize = (10,10))

for i in range(3):
  input = x_train[i]
  ax[i, 0].imshow(input)
  k = 1
  for (key, values) in filters.items():
    output = conv(input[:,:,0], key)+conv(input[:,:,1], key)+conv(input[:,:,2], key)   
    ax[i, k].imshow(output)
    ax[i,k].set_title(key)
    k +=1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1783132530120482, 'fraction_numerical': 0.044061962134251294, 'mean_word_length': 3.740619902120718, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '560361', 'license': 'MIT', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1155, 'n_words': 301, 'path': 'day3/Convolutions.ipynb', 'repo_name': 'mjvakili/MLcourse', 'size': '2905'}