Deep Learning Tips and Tricks
This post is a distilled collection of conversations, messages, and debates on how to optimize deep models. If you have tricks you’ve found impactful, please share them in the comments below!
By Jonathan Balaban, Data Science Nomad
Below is a distilled collection of conversations, messages, and debates I’ve had with peers and students on how to optimize deep models. If you have tricks you’ve found impactful, please share them!!
First, Why Tweak Models?
Deep learning models like the Convolutional Neural Network (CNN) have a massive number of parameters; we can actually call these hyper-parameters because they are not optimized inherently in the model. You could gridsearch the optimal values for these hyper-parameters, but you’ll need a lot of hardware and time. So, does a true data scientist settle for guessing these essential parameters?
One of the best ways to improve your models is to build on the design and architecture of the experts who have done deep research in your domain, often with powerful hardware at their disposal. Graciously, they often open-source the resulting modeling architectures and rationale.
Deep Learning Techniques
Here are a few ways you can improve your fit time and accuracy with pre-trained models:
- Research the ideal pre-trained architecture: Learn about the benefits of transfer learning, or browse some powerful CNN architectures. Consider domains that may not seem like obvious fits, but share potential latent features.
- Use a smaller learning rate: Since pre-trained weights are usually better than randomly initialized weights, modify more delicately! Your choice here depends on the learning landscape and how well the pre-training went, but check errors across epochs for an idea of how close you are to convergence.
- Play with dropout: As with Ridge and LASSO regularization for regression models, there is no optimized alpha or dropout for all models. It’s a hyper-parameter that depends on your specific problem, and must be tested. Start with bigger changes — a wider gridsearch span across orders of magnitude, like
np.logspace()can provide— then drop down as with the learning rate above.
- Limit weight sizes: We can limit the max norm (absolute value) of the weights for certain layers in order to generalize our model
- Don’t touch the first layers: The first hidden layers of a neural network tend to capture universal and interpretable features, like shapes, curves, or interactions that are very often relevant across domains. We should often leave these alone, and focus on optimizing the meta² latent level further back. This may mean adding hidden layers so we don’t rush the process!
- Modify the output layer: Replace model defaults with a new activation function and output size that is appropriate for your domain. However, don’t limit yourself to the most obvious solution. While MNIST may seem like it wants 10 output classes, some numbers have common variations, and allowing for 12–16 classes may allow better settling of these variants and improved model performance! As with the tip above, deep learning models should be increasingly modified and tailored as we near output.
Techniques in Keras
Here’s how to modify dropout and limit weight sizes in Keras with MNIST:
Dropout Best Practices:
- Use small dropouts of 20–50%, with 20% recommended for inputs. Too low and you have negligible effects; too high and you underfit.
- Use dropout on the input layer as well as hidden layers. This has been proven to improve deep learning performance.
- Use a large learning rate with decay, and large momentum.
- Constrain your weights! A big learning rate can result in exploding gradients. Imposing a constraint on network weight — such as max-norm regularization with a size of 5 — has been shown to improve results.
- Use a larger network. You are likely to get better performance when dropout is used on a larger network, giving the model more of an opportunity to learn independent representations.
Here’s an example of final layer modification in Keras with 14 classes for MNIST:
And an example of how to freeze weights in the first five layers:
Alternatively, we can set the learning rate to zero for that layer, or use per-parameter adaptive learning algorithm like Adadelta or Adam. This is somewhat complicated and better implemented in other platforms, like Caffe.
Galleries of Pre-trained Networks:
View your TensorBoard graph within Jupyter
It’s often essential to get a visual idea of how your model looks. If you’re working in Keras, abstraction is nice but doesn’t allow you to drill down into sections of your model for deeper analysis. Fortunately, the code below lets us visualize our models directly with Python:
This will plot a graph of the model and save it as a png file:
plot takes two optional arguments:
show_shapes(defaults to False) controls whether output shapes are shown in the graph.
show_layer_names(defaults to True) controls whether layer names are shown in the graph.
You can also directly obtain the
pydot.Graph object and render it yourself, for example to show it in an ipython notebook :
Bio: Jonathan Balaban is a data science nomad.
Original. Reposted with permission.
- The Keras 4 Step Workflow
- Improving the Performance of a Neural Network
- Top 8 Free Must-Read Books on Deep Learning