Showing posts with label PyTorch. Show all posts
Showing posts with label PyTorch. Show all posts

Friday, September 18, 2026

Hyperparameter tuning with Hyperband Tensorflow and PyTorch, along with KerasTuner and Ray Tune

This post has been a long time coming. Realistically it should have been as part of my Machine Learning with Zeek series. Guess I got sloppy and not included it or was just too lazy. Whatever the reason it is here now.

Another more important reason, is when teaching the SANS SEC595: Machine Learning and AI for Cyber Security Professionals course, the question always arise - before we get to that section - but how do I know how many neurons to use, which activation function, etc.? This post is meant to provide some additional insights with a lot more details than I provide when doing my demos. Make no mistake, my demos make the point. However, I thought I can be a bit more involved here.

Now to be specific, when we talk about hyperparameter optimization in class, we talk about Grid Search, Random Search Bayesian, early stopping as well as genetic optimization. Do note, there are many strategies to address this optimization problem. In this post however, I will focus specifically on Hyperband, which is another strategy for hyperparameter optimization

Why do I need to worry about this?
Simple! In machine learning, it is not only about finding the right algorithm but more importantly finding the right combination of hyperparameters. Or put another way for neural networks, finding the right values for the weights (w) and the biases (b). Traditional machine learning, one my say, finding the right values for the slope (m) and the bias/intercept (b).


What is Hyperband?
Hyperband was introduced in the paper, "Hyperband: A Novel Bandit-Based Approach to Hyperparameter Optimization" by Li, et al.

Hyperband focuses on speeding up Random Search. It sees the problem as an adaptive resource allocation problem and allocates resources among randomly sampled hyperparameter configurations. It also leverages early-stopping strategy, which means if the model is not improving after a certain number of epochs, then training should be stopped for that model. 

Along with early stopping, one can train on a subset of data or even a subset of features.

One of the key strategies used by Hyperband is Successive Halving:

The Successive Halving algorithm is extended by hyperband and called as a subroutine. The core idea behind the original Successive Halving is that at the first iteration, a small amount of resources - the number of samples, etc. - along with all hyperparameters candidates are used.

As we move to the second iteration, we get the best half of the performing hyperparameters - meaning we discard the set that does not work well - and double the resources - once again the number of samples.

The process above gets repeated until the best combination of hyperparameters are found.

This strategy is also called a bandit approach.

Remember, Hyperband's aim is to extend Successive Halving. One of the ways it does this is by randomly distributing resources to each batch rather than uniformly to explore different convergence behaviours. 

To ensure everyone can follow along, we will use a toy dataset to build our intuition. We also look at this from the perspective of both Tensorflow and PyTorch.

See the Tensorflow / KerasTuner Notebook and 
The PyTorch / Ray Tune 

In the end, when we look at the test accuracy, we see they are about the same.


References:
https://arxiv.org/pdf/1603.06560
https://scikit-learn.org/stable/auto_examples/model_selection/plot_successive_halving_iterations.html
https://www.youtube.com/watch?v=eqokKei1aEo&t=221s
https://www.youtube.com/watch?v=U5wbvPzW57M
https://www.youtube.com/watch?v=JUrU92fTxpo
Coding supported by www.duckai.com

Sunday, June 15, 2025

It is finally here! A Little Book On Adversarial AI - Get a FREE PDF Copy

Download a copy here: 
SHA256Sum: f7282afbdf15bbf2ed8fea70e1b0a27630a9c359a3a17f1b4cf274f599cd6ec6 


Over the past few months, I have been working on expanding my knowledge on Adversarial AI. Rather than putting together a bunch of blog posts, I decided to consolidate everything into one book.  

Along with this book, there are 63 labs, focusing on Adversarial AI and other aspects of machine and deep learning. These labs are released as Jupyter Notebooks, which are hosted at my GitHub repo dedicated to this book. 

As much as possible this book is self-contained. Nonetheless, the following background is important for readers to get the most out of this book and its associated Jupyter Notebooks. Note I am using Jupyter as part of VSCode. An intermediate knowledge and understanding of python. If you have already dabbled with building machine learning models and or have written a few scripts to solve your automation tasks or taken the SANS SEC595: Applied Data Science and AI/Machine Learning for Cybersecurity Professionals , then you should be good to go.

Alternatively, if you have not done any prior work with building machine learning models, my series on Beginning Machine and Deep Learning with Zeek logs is a good place to start. There is a lot of content there to get you up to speed relatively fast.

With that said, I’ve made every effort to add the necessary comments to the code, so that we are all clear on what the code is doing. 

Have fun learning!