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

6 / 8

Building the Model

Let us build a neural network with Dense layers for this classification problem.

Note:

  • keras.layers.Flatten : Flattens the input.

  • keras.layers.Dense(300, activation="relu") : Just your regular densely-connected NN layer with 300 neurons and relu activation function.

  • Why do we need clear_session?

    It is useful when you're creating multiple models in successions, such as during hyperparameter search or cross-validation. Each model you train adds nodes (potentially numbering in the thousands) to the graph. Eventually, models will become slower and slower to train, and you may also run out of memory. Clearing the session removes all the nodes left over from previous models, freeing memory and preventing slowdown.

INSTRUCTIONS

Note: Makesure to write the following code snippets in separate code cells.

  • Let us first clear the session:

    keras.backend.clear_session()
    
  • Now, let us build the neural network as follows:

    model = keras.models.Sequential([
        keras.layers.Flatten(input_shape=[28, 28]),
        keras.layers.Dense(300, activation="relu"),
        keras.layers.Dense(100, activation="relu"),
        keras.layers.Dense(10, activation="softmax")
    ])
    
  • Let us view the layers of model.

    model.layers
    
  • Let us view the summary of model.

    model.summary()
    
  • Let us plot the arcitecture of model and save it into my_fashion_mnist_model.png file.

    keras.utils.plot_model(model, "my_fashion_mnist_model.png", show_shapes=True)
    
  • Define the sgd optimizer by using keras.optimizers.SGD and setting the learning_rate=0.01.

    sgd = {{your code comes here}}(learning_rate=0.01)
    
  • Now let us define the compile part of the model as below:

    model.compile(loss="sparse_categorical_crossentropy",
          optimizer=sgd,
          metrics=["accuracy"])
    

    We have set accuracy as a performance evaluation metric. Also, we set sparse_categorical_crossentropy as the loss function, as we have the labels which are not one-hot vectors but the integers. More here.

Get Hint See Answer


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

Loading comments...