Simple MNIST Convnet - Keras Code Examples
Key Takeaways
This video demonstrates how to train a simple convolutional neural network on the MNIST dataset using Keras, covering data loading, preprocessing, model building, and evaluation.
Full Transcript
welcome to the henry ai labs walkthrough of keras code examples keras has provided 56 code examples implementing popular ideas in deep learning this ranges from the basics such as simple mnist and imdb text classification all the way to cutting-edge research ideas such as knowledge distillation supervised contrastive learning and transformers we'll also explore fun generative examples like variational autoencoders and cyclegan my contribution to these code examples is to explain every single line of code in each of them walking through each of the individual keras examples i'm not the author of these code examples please consider starting the github repositories to show support to the original authors from this video you'll learn how to load the mnist dataset using the keras datasets library how to preprocess this dataset for a quick input into a simple neural network and then how to train the model and evaluate it quickly with this test set that's been loaded into memory with this dataset library so as stated in the title this example is a bit easier simple mnist with a convolutional network so first we import numpy keras and keras layers so now we've prepared the data so here's the first interesting thing we've seen in our series keras has built-in data sets torch vision torch text and keras they all have some built-in data sets that you can load data sets just by calling this api call so you do keras.datasets.mnis.loaddata so this is much different from in the last example we had to get our data by doing a curl and then grabbing this link from the web then we had to pipeline it from the disk into our workspace whereas keras has some examples of data sets that are built in that we can just do datasets.mnis.loaddata so mostly this is just academic datasets that are accessible like this but it's definitely a trend in this direction which is really exciting hugging face they recently did an nlp library where you have this kind of easy syntax to load data into your models so another interesting thing about this code snippet we see we have the 10 eminence digits the input shape here's the other interesting thing so when we use this api to load our images they're originally stored as 28 by 28 dimension so this is describing the height and the width of the mnist digit represented as a grayscale image so in order to make this compatible for our deep neural network we're going to do numpy.expand dimensions x train -1 so as shown in this code that i've added what you do is if you have a numpy array that's 28 by 28 and then you expand the dimensions you add this one channel in the back just to make it describe the 28 height 28 width one channels is in grayscale so the other thing we do is we do keras.utils.2 categorical to convert our y train from say nine or six into a one hot encoded vector for the class label so now y train is gonna describe say it was a two it would now be zero zero one zero zero zero zero this is how we do the categorical cross entropy loss when we're fitting these labels with our machine learning model so now we're building the model compared to the first example we build this complex exception network here we're just using two convolutional layers with 32 filters 3x3 convolutions interleaved with max pooling then we flatten this feature plane apply dropout with a 50 probability and then apply a dense fully connected layer from that flattened feature vector into the 10 output classes so then we also have model.summary so this is model.summary from keras and it is really useful because it shows us the intermediate dimensions of the feature planes in our deep neural networks so we start off with the 28 by 28 mnist digits we pass it through a three by three convolution that down samples the spatial resolution to 26 by 26 note how this is two less than the original height and width of the mnist digits we have 32 such feature planes so the dimension of this intermediate tensor in our deep neural network is 26 by 26 by 32 and we have 320 parameters in these convolutional filters these are the parameters that we use to transform these intermediate feature planes in deep neural networks now we apply max pooling 2d this is going to be a spatial pooling operation that's going to take the max value between every two pixel slots and slide that across to reduce the spatial resolution from 26 to 13. then on this 13 by 13 by 32 feature plane we apply another convolutional 2d that down samples it to 11 by 11 and this convolutional layer has 18 496 parameters then we do another max pooling and then we flatten this out into a vector so we take these five by five we have 64 five by five feature planes and then we flatten that out into a one dimensional vector that has dimensionality one thousand six hundred by one because it's a vector then we apply dropout and we match from one thousand six hundred into the ten output classes so all in all the simple neural network has thirty four thousand eight hundred and twenty six parameters most of which are contained in the second convolutional layer and the output mapping from the dense feature vector into the output classes another really important theme in our series walking through keras code examples is how much faster models run on gpus compared to cpus so this is running the model this simple convolutional network to fit mnist on a cpu note how it takes about 33 seconds to step through the data set now that we've changed the runtime to the gpu provided by google collab it takes one second to step through the data compared to the 33 seconds when using the cpu now that we have a trained model we can use the short syntax provided by keras to evaluate our model with our previously loaded test set so remember that we have this api where we load in the x text and y test we normalize x test by dividing each value by 255 expanding the dimensions of each image so it's compatible and then changing the labels to be categorically one hot encoded vectors compared to just say a numeric number say three four or five that would denote each individual handwritten digit so we can use this quick syntax model to evaluate our data sets that are already loaded into memory and then from this we get the score and score at index zero is the loss and score at index one is the accuracy on the test set so we quickly achieve 99 accuracy on mnist using this simple model because mnist is a is a toy example and it's easy to get a good model with this so to recap some of the big takeaways are noting how we have these data sets that are loaded into keras these are mostly academic data sets like mnist or cfart10 or maybe stl10 these kinds of academic data sets are loaded in computer vision as well as text data sets like imdb and maybe some semantic text similarity data sets so we also note this expanding dimensions converting labels to categorical a quick sense of how we can build convolutional networks and just more experience and familiarity with this syntax training the model noting how much faster it is on the gpu and then a shorthand syntax to evaluate a model on a test set that's already been loaded into memory
Original Description
This example shows you how to train a very simple convolutional neural network on the famous MNIST dataset!
Simple MNIST convnet: https://keras.io/examples/vision/mnist_convnet/
Keras Code Examples: https://keras.io/examples/
Thanks for watching! Please subscribe and check out the Keras Code Examples video playlist!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Connor Shorten · Connor Shorten · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
DenseNets
Connor Shorten
DeepWalk Explained
Connor Shorten
Inception Network Explained
Connor Shorten
StackGAN
Connor Shorten
StyleGAN
Connor Shorten
Progressive Growing of GANs Explained
Connor Shorten
Improved Techniques for Training GANs
Connor Shorten
Word2Vec Explained
Connor Shorten
Must Read Papers on GANs
Connor Shorten
Unsupervised Feature Learning
Connor Shorten
Self-Supervised GANs
Connor Shorten
Embedding Graphs with Deep Learning
Connor Shorten
Transfer Learning in GANs
Connor Shorten
ReLU Activation Function
Connor Shorten
AC-GAN Explained
Connor Shorten
SimGAN Explained
Connor Shorten
DC-GAN Explained!
Connor Shorten
ResNet Explained!
Connor Shorten
Graph Convolutional Networks
Connor Shorten
Neural Architecture Search
Connor Shorten
Henry AI Labs
Connor Shorten
Video Classification with Deep Learning
Connor Shorten
BigGANs in Data Augmentation
Connor Shorten
Introduction to Deep Learning
Connor Shorten
EfficientNet Explained!
Connor Shorten
Self-Attention GAN
Connor Shorten
Curriculum Learning in Deep Neural Networks
Connor Shorten
Deep Learning Podcast #1 | Edward Dixon | Stochastic Weight Averaging
Connor Shorten
Deep Compression
Connor Shorten
Skin Cancer Classification with Deep Learning
Connor Shorten
Deep Learning Podcast #2 | Edward Peake | Deep Learning in Medical Imaging
Connor Shorten
The Lottery Ticket Hypothesis Explained!
Connor Shorten
SqueezeNet
Connor Shorten
GauGAN Explained!
Connor Shorten
AutoML with Hyperband
Connor Shorten
DL Podcast #3 | Yannic Kilcher | Population-Based Search
Connor Shorten
Weakly Supervised Pretraining
Connor Shorten
Image Data Augmentation for Deep Learning
Connor Shorten
Unsupervised Data Augmentation
Connor Shorten
Wide ResNet Explained!
Connor Shorten
RevNet: Backpropagation without Storing Activations
Connor Shorten
GANs with Fewer Labels
Connor Shorten
BigBiGAN Unsupervised Learning!
Connor Shorten
Self-Supervised Learning
Connor Shorten
Multi-Task Self-Supervised Learning
Connor Shorten
Self-Supervised GANs
Connor Shorten
Population Based Training
Connor Shorten
Show, Attend and Tell
Connor Shorten
Siamese Neural Networks
Connor Shorten
WaveGAN Explained!
Connor Shorten
VAE-GAN Explained!
Connor Shorten
Evolution in Neural Architecture Search!
Connor Shorten
AI Research Weekly Update August 18th, 2019
Connor Shorten
Weight Agnostic Neural Networks Explained!
Connor Shorten
AI Research Weekly Update August 25th, 2019
Connor Shorten
Neuroevolution of Augmenting Topologies (NEAT)
Connor Shorten
CoDeepNEAT
Connor Shorten
AI Research Weekly Update September 1st, 2019
Connor Shorten
Randomly Wired Neural Networks
Connor Shorten
Genetic CNN
Connor Shorten
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
Building My First Neural Network From Scratch with PyTorch: A Journey on the Dry Bean Dataset
Medium · Deep Learning
The Reframe
Medium · Deep Learning
Looking for Fast.ai Study Partner (Deep Learning, GMT+5)
Reddit r/deeplearning
Understanding Deep Learning Through Four Interactive Experiments
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI