Project - Training from Scratch vs Transfer Learning

5 / 8

Build and Fit the Model A

Let us define the model for the classification of data set A that we have created previously.

Later the trained weights of this model will be used for the classification task of data B.

  • Create a keras neural network as follows:

    • Add keras.layers.Flatten to flatten the input image to the model.

    • Add 5 dense layers with n_hidden number of neurons and selu activation function.

    • Add a final dense layer with 8 neurons and softmax activation function(for classifying 8 classes of data).

      model_A = keras.models.Sequential()
      model_A.add(keras.layers.Flatten(input_shape=[28, 28]))
      for n_hidden in (300, 100, 50, 50, 50):
          model_A.add(keras.layers.Dense(n_hidden, activation="selu"))
      model_A.add(keras.layers.Dense(8, activation="softmax"))
  • Define the compiling part of the model:

    • Set "sparse_categorical_crossentropy" as loss.

    • Set keras.optimizers.SGD(lr=1e-3) as optimizer.

      model_A.compile(loss= << your code comes here >>,
          optimizer= << your code comes here >>,
  • Now train model_A on X_train_A and y_train_A for 5 epochs , and validation_data=(X_valid_A, y_valid_A) using

    history = << your code comes here >>(X_train_A, y_train_A, epochs=5,
                validation_data=(X_valid_A, y_valid_A))
  • Save the model_A we created."my_model_A.h5")
Get Hint See Answer

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

Loading comments...