Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
We shall define 3 utility functions which we will be using further:
rounded_accuracy: we define this function to get the rounded accuracy by calculating the accuracies using the rounded values of the predicted value and the actual value.
plot_image : used to plot the given image
show_reconstructions : we shall use this function to show how our trained model is able to reconstruct the validation data.
Note:
keras.metrics.binary_accuracy : calculates how often predictions matches binary labels. It computes the mean accuracy rate across all predictions for binary classification problems.
plt.imshow : Display data as an image
plt.axis("off") : It does not display the axis.
plt.subplot : Add a subplot to the current figure.
Define the rounded_accuracy:
def rounded_accuracy(y_true, y_pred):
    return keras.metrics.binary_accuracy(tf.round(y_true), tf.round(y_pred))
Define the plot_image function:
def plot_image(image):
    plt.imshow(image, cmap="binary")
    plt.axis("off")
Define the show_reconstructions:
def show_reconstructions(model, images=X_valid, n_images=5):
    reconstructions = model.predict(images[:n_images])
    fig = plt.figure(figsize=(n_images * 1.5, 3))
    for image_index in range(n_images):
        plt.subplot(2, n_images, 1 + image_index)
        plot_image(images[image_index])
        plt.subplot(2, n_images, 1 + n_images + image_index)
        plot_image(reconstructions[image_index])
Here, we are doing the following:

            Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...