Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Automatic Hyperparameter Tuning | Conclusion
Introduction to Neural Networks

bookChallenge: Automatic Hyperparameter Tuning

Rather than manually selecting specific values for our model's hyperparameters, randomized search (RandomizedSearchCV) offers a more efficient way to find an optimal configuration. Unlike grid search (GridSearchCV), which systematically evaluates all possible combinations of hyperparameters, randomized search selects a random subset of these combinations. This approach significantly reduces computational cost while still yielding strong results.

For neural networks, where the number of possible hyperparameter combinations can be immense, exhaustively testing every option is often impractical. Randomized search circumvents this issue by randomly sampling a defined number of hyperparameter sets, balancing exploration and efficiency.

RandomizedSearchCV(
    estimator=model, 
    param_distributions=randomized_parameters, 
    n_iter=number_of_models_to_test,  # Number of random combinations to evaluate
    scoring='accuracy',  # Evaluation metric
    random_state=42,  # Ensures reproducibility
)
  • estimator: the model to optimize (e.g., MLPClassifier);
  • param_distributions: a dictionary where keys are hyperparameter names and values are lists which to sample;
  • n_iter: specifies how many random combinations should be tested. A higher value increases the chances of finding an optimal combination but requires more computation;
  • scoring: defines the evaluation metric (e.g., 'accuracy' for classification).
Task

Swipe to start coding

Your goal is to tune the hyperparameters of a multilayer perceptron (MLP) using the RandomizedSearchCV method from scikit-learn.

Follow these steps carefully:

  1. Define the parameter grid param_distributions:
    • 'hidden_layer_sizes': include three configurations β€” (20, 20), (25, 25), and (30, 30);
    • 'learning_rate_init': include values 0.02, 0.01, and 0.005;
    • 'max_iter': include values 10, 30, and 50.
  2. Initialize the model using MLPClassifier().
  3. Apply RandomizedSearchCV:
    • Use the defined mlp model as the estimator;
    • Use the defined param_distributions grid;
    • Set n_iter=4 to limit the number of parameter combinations;
    • Use 'accuracy' as the evaluation metric;
    • Set random_state=1 for reproducibility.
  4. Fit the randomized search on the training data and print the best parameters found.
  5. Train the best model on the full training data and evaluate its accuracy on both the training and test sets.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the difference between RandomizedSearchCV and GridSearchCV in more detail?

How do I choose the right number for n_iter in RandomizedSearchCV?

What types of problems is RandomizedSearchCV best suited for?

close

Awesome!

Completion rate improved to 4

bookChallenge: Automatic Hyperparameter Tuning

Swipe to show menu

Rather than manually selecting specific values for our model's hyperparameters, randomized search (RandomizedSearchCV) offers a more efficient way to find an optimal configuration. Unlike grid search (GridSearchCV), which systematically evaluates all possible combinations of hyperparameters, randomized search selects a random subset of these combinations. This approach significantly reduces computational cost while still yielding strong results.

For neural networks, where the number of possible hyperparameter combinations can be immense, exhaustively testing every option is often impractical. Randomized search circumvents this issue by randomly sampling a defined number of hyperparameter sets, balancing exploration and efficiency.

RandomizedSearchCV(
    estimator=model, 
    param_distributions=randomized_parameters, 
    n_iter=number_of_models_to_test,  # Number of random combinations to evaluate
    scoring='accuracy',  # Evaluation metric
    random_state=42,  # Ensures reproducibility
)
  • estimator: the model to optimize (e.g., MLPClassifier);
  • param_distributions: a dictionary where keys are hyperparameter names and values are lists which to sample;
  • n_iter: specifies how many random combinations should be tested. A higher value increases the chances of finding an optimal combination but requires more computation;
  • scoring: defines the evaluation metric (e.g., 'accuracy' for classification).
Task

Swipe to start coding

Your goal is to tune the hyperparameters of a multilayer perceptron (MLP) using the RandomizedSearchCV method from scikit-learn.

Follow these steps carefully:

  1. Define the parameter grid param_distributions:
    • 'hidden_layer_sizes': include three configurations β€” (20, 20), (25, 25), and (30, 30);
    • 'learning_rate_init': include values 0.02, 0.01, and 0.005;
    • 'max_iter': include values 10, 30, and 50.
  2. Initialize the model using MLPClassifier().
  3. Apply RandomizedSearchCV:
    • Use the defined mlp model as the estimator;
    • Use the defined param_distributions grid;
    • Set n_iter=4 to limit the number of parameter combinations;
    • Use 'accuracy' as the evaluation metric;
    • Set random_state=1 for reproducibility.
  4. Fit the randomized search on the training data and print the best parameters found.
  5. Train the best model on the full training data and evaluate its accuracy on both the training and test sets.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
single

single

some-alt