#Import required libraries
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
# Load the MNIST dataset from keras dataset
mnist = tf.keras.datasets.mnist
# Load the data and split
(X_train,y_train),(X_test,y_test) = mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
# Check the shape of training and testing data
X_train.shape, y_train.shape, X_test.shape, y_test.shape
# Visualize one single data to check the digit
plt.imshow(X_train[0])
# Create a simple 10 filters CNN layer
model = Sequential()
model.add(Conv2D(10,(3,3),input_shape=(28,28,1),activation='relu'))
# Grab a single data and then predict to get weights
data = X_train[0]
# reshape the data to 1,28,28,1
data = data.reshape(1,28,28,1)
# Get the weights
yhat = model.predict(data)
# Check the shape of yhat
yhat.shape
# Reshape the yhat to display it or visualize it
yhat = yhat.reshape(26,26,10)
# Visualize all the 10 filters
for i in range(10):
plt.imshow(yhat[:,:,i])
plt.show()