Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

Project - Training from Scratch vs Transfer Learning

4 / 8

Dividing the data sets

Let's split the fashion MNIST training set in two:

X_train_A: all images of all items except for sandals and shirts (classes 5 and 6).

X_train_B: a much smaller training set of just the first 200 images of sandals or shirts. The validation set and the test set are also split this way, but without restricting the number of images.

Why are we doing this?

We will train a model on set A (classification task with 8 classes), and try to reuse it to tackle set B (binary classification). We hope to transfer a little bit of knowledge from task A to task B, since classes in set A (sneakers, ankle boots, coats, t-shirts, etc.) are somewhat similar to classes in set B (sandals and shirts). However, since we are using Dense layers, only patterns that occur at the same location can be reused (in contrast, convolutional layers will transfer much better, since learned patterns can be detected anywhere on the image, as we will see in the CNN chapter).

INSTRUCTIONS
  • Define the split_dataset function, which splits the whole dataset into 2: one which contains sandals and shirts data, the other containing the images of the remaining classes.

    def split_dataset(X, y):
        y_5_or_6 = (y == 5) | (y == 6) # sandals or shirts
        y_A = y[~y_5_or_6]
        y_A[y_A > 6] -= 2 # class indices 7, 8, 9 should be moved to 5, 6, 7
        y_B = (y[y_5_or_6] == 6).astype(np.float32) # binary classification task: is it a shirt (class 6)?
        return ((X[~y_5_or_6], y_A), (X[y_5_or_6], y_B))
    
  • Now call the split_dataset on the X_train and y_train.

    (X_train_A, y_train_A), (X_train_B, y_train_B) = << your code comes here >>(X_train, y_train)
    
  • Similarly, call the split_dataset on the X_valid and y_valid.

    (X_valid_A, y_valid_A), (X_valid_B, y_valid_B) = << your code comes here >>(X_valid, y_valid)
    
  • Similarly, call the split_dataset on the X_test and y_test.

    (X_test_A, y_test_A), (X_test_B, y_test_B) = << your code comes here >>(X_test, y_test)
    
  • Set the random seed for tf and np.

    tf.random.set_seed(42)
    np.random.seed(42)
    
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...