Project - How to Build a Neural Network for Image Classification with TensorFlow

7 / 8

Fitting the Model

Let us train the model on the train data.

Note:

  • plt.gca() is used to get a reference to the current axes, if you need to change the limits on the y-axis, for example.
INSTRUCTIONS
  • Use model.fit to train the model. We shall pass the X_train, y_train, epochs=30, validation_data=(X_valid, y_valid) as input parameters to the method.

    history = model.fit(X_train, y_train, epochs=30,
                validation_data=(X_valid, y_valid))
    

    This might take some time, so go and have a sip of water if you like.

  • Let us now print the parameter history of the history.

    history.params
    
  • Let us print the name of the first hidden layer:

    hidden1 = model.layers[1]
    print(hidden1.name)
    
  • Let us use get_weights() function to see the trained weights and biases of the hidden1.

    weights, biases = hidden1.get_weights() # getting the weights and biases
    print(weights.shape, weights)
    print(biases)
    
  • Let us visualize the model training history. Here, history.history is a dictionary containing information about the accuracy and loss measures through the epochs.

    Let us store this dictionary as a pandas dataframe and plot the values.

    import pandas as pd
    
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1) # setting limits for y-axis
    plt.show()
    
Get Hint See Answer


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

Loading comments...