Get startedGet started for free

Tuning the model parameters

It's time to try out different parameters on your model and see how well it performs!

The create_model() function you built in the previous exercise is ready for you to use.

Since fitting the RandomizedSearchCV object would take too long, the results you'd get are printed in the show_results() function. You could try random_search.fit(X,y) in the console yourself to check it does work after you have built everything else, but you will probably timeout the exercise (so copy your code first if you try this or you can lose your progress!).

You don't need to use the optional epochs and batch_size parameters when building your KerasClassifier object since you are passing them as params to the random search and this works already.

This exercise is part of the course

Introduction to Deep Learning with Keras

View Course

Exercise instructions

  • Import KerasClassifier from tensorflow.keras scikit_learn wrappers.
  • Use your create_model function when instantiating your KerasClassifier.
  • Set 'relu' and 'tanh' as activation, 32, 128, and 256 as batch_size, 50, 100, and 200 epochs, and learning_rate of 0.1, 0.01, and 0.001.
  • Pass your converted model and the chosen params as you build your RandomizedSearchCV object.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import KerasClassifier from tensorflow.keras scikit learn wrappers
from tensorflow.keras.wrappers.____ import ____

# Create a KerasClassifier
model = KerasClassifier(build_fn = ____)

# Define the parameters to try out
params = {'activation': [____, ____], 'batch_size': [____, ____, ____], 
          'epochs': [____, ____, ____], 'learning_rate': [____, ____, ____]}

# Create a randomize search cv object passing in the parameters to try
random_search = RandomizedSearchCV(____, param_distributions = ____, cv = KFold(3))

# Running random_search.fit(X,y) would start the search,but it takes too long! 
show_results()
Edit and Run Code