Recreated Course #1 Cat Classifier

As a way to continue practicing with TensorFlow, I recreated the Cat Classifier that we did in course #1. However, I am not getting the approximate results we obtained in the original exercise. Can anyone take a look and tell me if maybe I made a mistake with the programming or data processing?

I am mostly worried about whether I processed the data correctly.

training accuracy = 0.8421

test accuracy = 0.7000

num_epochs = 180 (If I do 3000 the overfitting is even more clear)

import h5py 
import pandas as pd
import tensorflow as tf
import numpy as np

filename_train = "/kaggle/input/catvnoncat/train_catvnoncat.h5"
filename_test = "/kaggle/input/catvnoncat/test_catvnoncat.h5"

training_set = h5py.File(filename_train,'r')
test_set = h5py.File(filename_test, 'r')

#Training Set Data
train_x_orig = np.array(training_set["train_set_x"])
train_y = np.array(training_set["train_set_y"])

#Testing Set Data
test_x_orig = np.array(test_set["test_set_x"])
test_y = np.array(test_set["test_set_y"])


# Reshape the training and test examples 
train_x_flatten = train_x_orig.reshape(train_x_orig.shape[0], -1)   # The "-1" makes reshape flatten the remaining dimensions
test_x_flatten = test_x_orig.reshape(test_x_orig.shape[0], -1)

#train_x_flatten = train_x_flatten / 255.
#test_x_flatten = test_x_flatten / 255.

#Cast numpy arrays into pandas datasets
#X_training = pd.DataFrame(train_x_flatten, columns = ['Column_A','Column_B'])
#Y_training = pd.DataFrame(train_y_flatten, columns = ['Column_A','Column_B'])

normalizer = tf.keras.layers.Normalization(axis=-1)

def get_basic_model():
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(20, activation='relu'),
        tf.keras.layers.Dense(7, activation='relu'),
        tf.keras.layers.Dense(5, activation='relu'),
        tf.keras.layers.Dense(1, activation = 'sigmoid')

    return model

model = get_basic_model()
history =, train_y, epochs=150, batch_size=256, 
          validation_data = (test_x_flatten, test_y))

Good try !
I think there are multiple parties that use this dataset and show some results, which may help you to understand deeper. For example, Cat classification with 2-layer NN

This is trying to change several hyper-parameters and see the results. I think the results are similar to yours. Of course, there is more common approach to use a convolutional network (Conv2D) to detect some characteristics in 2D space. This may provide some better results. I actually picked one model, and made it run in my local environment. Here is the result.

It may not a big improvement.

The key point here is, this data set only have 209 images for training and 50 images for testing. We only have 72 images for cats. It can be easily overfitted, as you see. And, even we use deep convolutional network as shown above, the result is similar (slightly better, though…)

(By the way, we only have 209 training images. batch_size=256 may not help any…)


Thank you so much for your reply & detailed explanation! It was super helpful.

I will implement better models as I continue learning.