Keras is a simple-to-use but powerful deep learning library for Python. In this post, we’ll see how easy it is to build a feedforward neural network and train it to solve a real problem with Keras.
This post is intended for complete beginners to Keras but does assume a basic background knowledge of neural networks. My introduction to Neural Networks covers everything you need to know (and more) for this post – read that first if necessary.
Let’s get started!
Just want the code? The full source code is at the end.
We’re going to tackle a classic machine learning problem: MNIST handwritten digit classification. It’s simple: given an image, classify it as a digit.
Sample images from the MNIST dataset
Each image in the MNIST dataset is 28×28 and contains a centered, grayscale digit. We’ll flatten each 28×28 into a 784 dimensional vector, which we’ll use as input to our neural network. Our output will be one of 10 possible classes: one for each digit.
I’m assuming you already have a basic Python installation ready (you probably do). Let’s first install some packages we’ll need:
$ pip install keras tensorflow numpy mnist
Note: We need to install tensorflow because we’re going to run Keras on a TensorFlow backend (i.e. TensorFlow will power Keras).
You should now be able to import these packages and poke around the MNIST dataset:
import numpy as np import mnist import keras # The first time you run this might be a bit slow, since the # mnist package has to download and cache the data. train_images = mnist.train_images() train_labels = mnist.train_labels() print(train_images.shape) # (60000, 28, 28) print(train_labels.shape) # (60000,)
As mentioned earlier, we need to flatten each image before we can pass it into our neural network. We’ll also normalize the pixel values from [0, 255] to [-0.5, 0.5] to make our network easier to train (using smaller, centered values is often better).
import numpy as np import mnist train_images = mnist.train_images()…
You can read the article in its entirety, on the official website of https://victorzhou.com/blog/keras-neural-network-tutorial/
Thanks a lot … I hope your thoughts, your geniuses can have as much resonance as possible.