Hi devs,
If you're new to deep learning, you've likely come across the name Keras. But what is it exactly, and how does it work? In this post, I'll explain everything from the ground up and show you a step-by-step example using Keras to build a simple deep learning model. I'll explain key concepts like the MNIST dataset as well, so that you can follow along easily!
Keras is an open-source high-level neural networks API written in Python. It allows developers to quickly and easily build deep learning models using a user-friendly interface. Keras sits on top of more complex deep learning frameworks like TensorFlow, allowing you to focus on building your model without getting bogged down by the underlying complexity.
The MNIST dataset is one of the most famous datasets in machine learning. It contains 70,000 images of handwritten digits (0-9). Each image is a grayscale picture, 28x28 pixels in size. The goal is to classify these images into one of the ten digit categories.
Here’s an example of some digits from the MNIST dataset:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
When working with Keras, you'll often see the MNIST dataset used in tutorials because it's simple, well understood, and great for testing out new models.
Let's now build a simple neural network using Keras to classify these handwritten digits. We'll go through it step by step.
First, you need to have TensorFlow installed, as Keras is part of TensorFlow in the latest versions. You can install it via pip:
pip install tensorflow
We'll import TensorFlow and Keras-specific libraries that we'll need to build and train the model.
import tensorflow as tf from tensorflow.keras import layers, models
Here, tensorflow.keras is the Keras API within TensorFlow.
Keras provides easy access to datasets like MNIST. We’ll load the dataset and split it into training and test sets.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
In this step, train_images and train_labels hold the training data, while test_images and test_labels hold the test data.
Each image in train_images is a 28x28 pixel grayscale image, and train_labels contains the digit labels (0-9) corresponding to each image.
Next, we need to normalize the pixel values of the images to make the model training more efficient. Each pixel value in an image is between 0 and 255. We'll scale these values to be between 0 and 1 by dividing the images by 255.
pip install tensorflow
Now let's build our neural network using Keras. We’ll create a Sequential model, which allows us to stack layers one on top of another.
import tensorflow as tf from tensorflow.keras import layers, models
Next, we need to compile the model. This is where we specify the optimizer, loss function, and evaluation metrics.
# Load the MNIST dataset mnist = tf.keras.datasets.mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
Now, we’re ready to train the model! We’ll train it for 5 epochs (i.e., the model will go through the entire training dataset 5 times).
# Normalize pixel values to be between 0 and 1 train_images = train_images / 255.0 test_images = test_images / 255.0
Once the model is trained, we can evaluate its performance on the test data.
# Build the model model = models.Sequential([ layers.Flatten(input_shape=(28, 28)), # Flatten the 28x28 images into a 1D vector of 784 pixels layers.Dense(128, activation='relu'), # Add a fully-connected (Dense) layer with 128 neurons layers.Dense(10, activation='softmax') # Output layer with 10 neurons (one for each digit 0-9) ])
This will give us the model’s accuracy on the test dataset.
To put it simply:
Keras simplifies the process of building and training neural networks, making it an ideal starting point for beginners. Once you're comfortable with basic models, you can experiment with more complex architectures like convolutional neural networks (CNNs) and recurrent neural networks (RNNs).
Feel free to dive deeper into the world of deep learning with Keras, experiment with different models, and push the boundaries of what's possible!
What do you think of Keras so far?
The above is the detailed content of Keras: Understanding the Basics with a Detailed Example. For more information, please follow other related articles on the PHP Chinese website!