Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
The Deep Learning with Keras Workshop

You're reading from  The Deep Learning with Keras Workshop

Product type Book
Published in Jul 2020
Publisher Packt
ISBN-13 9781800562967
Pages 496 pages
Edition 1st Edition
Languages
Authors (3):
Matthew Moocarme Matthew Moocarme
Profile icon Matthew Moocarme
Mahla Abdolahnejad Mahla Abdolahnejad
Profile icon Mahla Abdolahnejad
Ritesh Bhagwat Ritesh Bhagwat
Profile icon Ritesh Bhagwat
View More author details

Table of Contents (11) Chapters

Preface
1. Introduction to Machine Learning with Keras 2. Machine Learning versus Deep Learning 3. Deep Learning with Keras 4. Evaluating Your Model with Cross-Validation Using Keras Wrappers 5. Improving Model Accuracy 6. Model Evaluation 7. Computer Vision with Convolutional Neural Networks 8. Transfer Learning and Pre-Trained Models 9. Sequential Modeling with Recurrent Neural Networks Appendix

1. Introduction to Machine Learning with Keras

Activity 1.01: Adding Regularization to the Model

In this activity, we will utilize the same logistic regression model from the scikit-learn package. This time, however, we will add regularization to the model and search for the optimum regularization parameter - a process often called hyperparameter tuning. After training the models, we will test the predictions and compare the model evaluation metrics to the ones that were produced by the baseline model and the model without regularization.

  1. Load the feature data from Exercise 1.03, Appropriate Representation of the Data, and the target data from Exercise 1.02, Cleaning the Data:
    import pandas as pd
    feats = pd.read_csv('../data/OSI_feats_e3.csv')
    target = pd.read_csv('../data/OSI_target_e2.csv')
  2. Create a test and train dataset. Train the data using the training dataset. This time, however, use part of the training dataset for validation in order to choose...

2. Machine Learning versus Deep Learning

Activity 2.01: Creating a Logistic Regression Model Using Keras

In this activity, we are going to create a basic model using the Keras library. The model that we will build will classify users of a website into those that will purchase a product from a website and those that will not. To do this, we will utilize the same online shopping purchasing intention dataset that we did previously and attempt to predict the same variables that we did in Chapter 1, Introduction to Machine Learning with Keras.

Perform the following steps to complete this activity:

  1. Open a Jupyter notebook from the start menu to implement this activity. Load in the online shopping purchasing intention datasets, which you can download from the GitHub repository. We will use the pandas library for data loading, so import the pandas library. Ensure you have saved the csv files to an appropriate data folder for this chapter first. Alternatively, you can change...

3. Deep Learning with Keras

Activity 3.01: Building a Single-Layer Neural Network for Performing Binary Classification

In this activity, we will compare the results of a logistic regression model and single-layer neural networks of different node sizes and different activation functions. The dataset we will use represents the normalized test results of aircraft propeller inspections, while the class represents whether they passed or failed a manual visual inspection. We will create models to predict the results of the manual inspection when given the automated test results. Follow these steps to complete this activity:

  1. Load all the required packages:
    # import required packages from Keras
    from keras.models import Sequential 
    from keras.layers import Dense, Activation 
    import numpy as np
    import pandas as pd
    from tensorflow import random
    from sklearn.model_selection import train_test_split
    # import required packages for plotting
    import matplotlib.pyplot as plt 
    import matplotlib...

4. Evaluating Your Model with Cross-Validation Using Keras Wrappers

Activity 4.01: Model Evaluation Using Cross-Validation for an Advanced Fibrosis Diagnosis Classifier

In this activity, we are going to use what we learned in this topic to train and evaluate a deep learning model using k-fold cross-validation. We will use the model that resulted in the best test error rate from the previous activity and the goal will be to compare the cross-validation error rate with the training set/test set approach error rate. The dataset we will use is the hepatitis C dataset, in which we will build a classification model to predict which patients get advanced fibrosis. Follow these steps to complete this activity:

  1. Load the dataset and print the number of records and features in the dataset, as well as the number of possible classes in the target dataset:
    # Load the dataset
    import pandas as pd
    X = pd.read_csv('../data/HCV_feats.csv')
    y = pd.read_csv('../data/HCV_target...

5. Improving Model Accuracy

Activity 5.01: Weight Regularization on an Avila Pattern Classifier

In this activity, you will build a Keras model to perform classification on the Avila pattern dataset according to given network architecture and hyperparameter values. The goal is to apply different types of weight regularization on the model, that is, L1 and L2, and observe how each type changes the result. Follow these steps to complete this activity:

  1. Load the dataset and split the dataset into a training set and a test set:
    # Load the dataset
    import pandas as pd
    X = pd.read_csv('../data/avila-tr_feats.csv')
    y = pd.read_csv('../data/avila-tr_target.csv')
    """
    Split the dataset into training set and test set with a 0.8-0.2 ratio
    """
    from sklearn.model_selection import train_test_split
    seed = 1
    X_train, X_test, y_train, y_test = \
    train_test_split(X, y, test_size=0.2, random_state=seed)
  2. Define a Keras sequential model with three...

6. Model Evaluation

Activity 6.01: Computing the Accuracy and Null Accuracy of a Neural Network When We Change the Train/Test Split

In this activity, we will see that our null accuracy and accuracy will be affected by changing the train/test split. To implement this, the part of the code where the train/test split was defined has to be changed. We will use the same dataset that we used in Exercise 6.02, Computing Accuracy and Null Accuracy with APS Failure for Scania Trucks Data. Follow these steps to complete this activity:

  1. Import the required libraries. Load the dataset using the pandas read_csv function and look at the first five rows of the dataset:
    # Import the libraries
    import numpy as np
    import pandas as pd
    # Load the Data
    X = pd.read_csv("../data/aps_failure_training_feats.csv")
    y = pd.read_csv("../data/aps_failure_training_target.csv")
    # Use the head function to get a glimpse data
    X.head()

    The following table shows the output of the preceding code...

7. Computer Vision with Convolutional Neural Networks

Activity 7.01: Amending Our Model with Multiple Layers and the Use of softmax

Let's try and improve the performance of our image classification algorithm. There are many ways to improve its performance, and one of the most straightforward ways is by adding multiple ANN layers to the model, which we will learn about in this activity. We will also change the activation from sigmoid to softmax. Then, we can compare the result with that of the previous exercise. Follow these steps to complete this activity:

  1. Import the numpy library and the necessary Keras libraries and classes:
    # Import the Libraries 
    from keras.models import Sequential
    from keras.layers import Conv2D, MaxPool2D, Flatten, Dense
    import numpy as np
    from tensorflow import random
  2. Now, initiate the model with the Sequential class:
    # Initiate the classifier
    seed = 1
    np.random.seed(seed)
    random.set_seed(seed)
    classifier=Sequential()
  3. Add the first layer...

8. Transfer Learning and Pre-Trained Models

Activity 8.01: Using the VGG16 Network to Train a Deep Learning Network to Identify Images

Use the VGG16 network to predict the image given (test_image_1). Before you start, ensure that you have downloaded the image (test_image_1) to your working directory. Follow these steps to complete this activity:

  1. Import the numpy library and the necessary Keras libraries:
    import numpy as np
    from keras.applications.vgg16 import VGG16, preprocess_input
    from keras.preprocessing import image 
  2. Initiate the model (note that, at this point, you can also view the architecture of the network, as shown in the following code):
    classifier = VGG16()
    classifier.summary()

    classifier.summary() shows us the architecture of the network. The following points should be noted: it has a four-dimensional input shape (None, 224, 224, 3) and it has three convolutional layers.

    The last four layers of the output are as follows:

    Figure 8.16: The architecture of the...

9. Sequential Modeling with Recurrent Neural Networks

Activity 9.01: Predicting the Trend of Amazon's Stock Price Using an LSTM with 50 Units (Neurons)

In this activity, we will examine the stock price of Amazon for the last 5 years—from January 1, 2014, to December 31, 2018. In doing so, we will try to predict and forecast the company's future trend for January 2019 using an RNN and LSTM. We have the actual values for January 2019, so we can compare our predictions to the actual values later. Follow these steps to complete this activity:

  1. Import the required libraries:
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    from tensorflow import random
  2. Import the dataset using the pandas read_csv function and look at the first five rows of the dataset using the head method:
    dataset_training = pd.read_csv('../AMZN_train.csv')
    dataset_training.head()

    The following figure shows the output of the preceding code:

    Figure 9.24: The first...

lock icon The rest of the chapter is locked
arrow left Previous Chapter
You have been reading a chapter from
The Deep Learning with Keras Workshop
Published in: Jul 2020 Publisher: Packt ISBN-13: 9781800562967
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}