top of page

Hire Top Rated Machine Learning Experts | Practice Exercises

If you are learner and looking for machine learning practice exercises then here you can get many practice exercises. If you need help to complete these exercises then you can get help from our experts with an affordable price.


Here we provide plagiarism free work with proper code standard.


Classification Exercises

For these exercises use the GPU in Google Colab. To enable GPU go to top menu bar in EDIT menu go to NoteBook Settings. Once you click it a window opens, in the hardware accelerator dropdown menu choose GPU.




Introduction

We have already learned about Neural Networks and discussed Multilayered Perceptrons in depth. In this exercise, we will be testing our understanding of the underlying concepts with special emphasis to Hyperparameter tuning.

After doing these exercises, you would be able to better understand:

  • The architecture of a neural network

  • The parameters (training) of a neural network and how they change with changing architecture.

  • Hyperparameter tuning: batch size, number of hidden units and optimizers.

We encourage you to work with other hyperparameters as well like learning rate, number of layers, activation functions etc. And in the end there is an optional exercise, where you can see if what you observe for the MNIST dataset is true for other dataset as well.


Part 1: Building the model

Below we define a function to built a neural network model using TensorFlow Keras.


import tensorflow as tf
import numpy as np
from tensorflow import keras

def built_model(input_shape, n_hidden, nb_classes, optimizer='SGD'):
  '''
  The function builds a fully connected neural network with two hidden layers
  Arguments:
  input_shape: The number of inputs to the neural network
  n_hidden: Number of hidden neurons in the hidden layers
  nb_classes: Number of neurons in the output layer
  optimizer: The optimizer used to train the model. 
  By default we use Stochastic Gradient Descent.
  
  Returns:
  The function returns A model with loss and optimizer defined
  '''  
  model = tf.keras.models.Sequential()
  ## First Hidden layer  
  model.add(keras.layers.Dense(n_hidden,
       input_shape=(input_shape,),
       name='dense_layer', activation='relu'))
    
  ## Second Hidden Layer
  model.add(keras.layers.Dense(n_hidden,
        name='dense_layer_2', activation='relu'))
    
  ## Output Layer  
  model.add(keras.layers.Dense(nb_classes,
        name='dense_layer_3', activation='softmax'))
    
  ## Define loss and optimizer 
  model.compile(optimizer=optimizer, 
              loss='categorical_crossentropy',
              metrics=['accuracy'])
  return model

Exercise 1 What should be the values of the arguments INPUT_SHAPE: the number of input units, N_HIDDEN: the number of hidden units, and NB_CLASSES: the number of output units, if we want to build a model using built_model function with the specifications given in the figure:




# Task to do
INPUT_SHAPE = #?
N_HIDDEN = #?
NB_CLASSES = #?  

## Do not change anything below
assert(INPUT_SHAPE == 5), "Input shape incorrect"
assert(N_HIDDEN == 10), "Number of hidden neurons incorrect"
assert(NB_CLASSES == 2), "Number of output units incorrect"
model = built_model(INPUT_SHAPE, N_HIDDEN,NB_CLASSES)


Exercise 2 Based on the input, hidden and output units what are the total number of trainable parameters in this model?


# Task to do
trainable_parameters = #?
​
## Do not change anything below
assert trainable_parameters==model.count_params(), "Your answer is incorrect"
print("Number of trainable parameters in the model are", trainable_parameters)
model.summary()


Part 2: Reading the dataset

We will continue with the MNIST dataset.

Just run the cells in this part of the notebook. Do not change anything.


mnist = keras.datasets.mnist
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

# Processing the data
assert(len(X_train.shape)==3), "The input data is not of the right shape"
RESHAPED = X_train.shape[1]*X_train.shape[2]

X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

# Data Normalization
X_train, X_test = X_train / 255.0, X_test / 255.0
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

For the MNIST dataset the number of input and number of output units are fixed. However we can choose different values of hidden units.


INPUT_SHAPE = RESHAPED
NB_CLASSES = len(set(Y_train))

# one-hot encode
Y_train = tf.keras.utils.to_categorical(Y_train, NB_CLASSES)
Y_test = tf.keras.utils.to_categorical(Y_test, NB_CLASSES)


Part 3: Hyperparameters Exercise 3: The aim of this exercise is to understand the affect of changing number of hidden units on the model performance. Change the number of hidden units, and train the model. Compare the model performance in terms of accuracy. What do you understand from this?


Answer Please type your answer here...


# Task to do choose different values for number of hidden units (minimum five different values)
N_HIDDEN = #? Choose a value
## Do not change anything below
model = built_model(INPUT_SHAPE,N_HIDDEN, NB_CLASSES)
history = model.fit(X_train, Y_train,
		batch_size=128, epochs=50,
		verbose=1, validation_split=0.2)

# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, Y_test)
print('Test accuracy: {:.2f} %'.format(test_acc*100))

Exercise 4: Let us now repeat the same after changing the batch size (minimum 5 different values). Compare the model performance in terms of accuracy. What do you understand from this?


Answer Please type your answer here...


# Task to do choose different values for batch size (minimum five different values)
BATCH_SIZE =   #? Choose a value
## Do not change anything below
model = built_model(INPUT_SHAPE,128, NB_CLASSES)
history = model.fit(X_train, Y_train,
		batch_size=BATCH_SIZE, epochs=50,
		verbose=1, validation_split=0.2)
# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, Y_test)
print('Test accuracy: {:.2f} %'.format(test_acc*100))


Exercise 5: And now we do the same with different optimizers available in TensorFlow. Change the optimizers and compare the model performance in terms of accuracy. What do you understand from this?


Answer Please type your answer here...

# Task to do choose different optimizers
opt =   #? Choose from available optimizers
## Do not change anything below
N_HIDDEN = 128
model = built_model(INPUT_SHAPE,N_HIDDEN, NB_CLASSES, opt)
history = model.fit(X_train, Y_train,
		batch_size=128, epochs=50,
		verbose=1, validation_split=0.2)

# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, Y_test)
print('Test accuracy: {:.2f} %'.format(test_acc*100))



If you have any query then comment in below comment section or send your requirement details at:



realcode4you@gmail.com

bottom of page