# Tutorial 3 (JAX): Activation Functions¶ Note: This notebook is written in JAX+Flax. It is a 1-to-1 translation of the original notebook written in PyTorch+PyTorch Lightning with almost identical results. For an introduction to JAX, check out our Tutorial 2 (JAX): Introduction to JAX+Flax. Further, throughout the notebook, we comment on major differences to the PyTorch version and provide explanations for the major parts of the JAX code. We do not provide speed comparisons for this notebook since they tend to be uninformative for such small networks and potentially bottlenecked by other factors.

In this tutorial, we will take a closer look at (popular) activation functions and investigate their effect on optimization properties in neural networks. Activation functions are a crucial part of deep learning models as they add the non-linearity to neural networks. There is a great variety of activation functions in the literature, and some are more beneficial than others. The goal of this tutorial is to show the importance of choosing a good activation function (and how to do so), and what problems might occur if we don’t.

Before we start, we import our standard libraries and set up basic functions:

:

## Standard libraries
import os
import json
import math
import numpy as np
from typing import Any, Sequence
import pickle
from copy import deepcopy

## Imports for plotting
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg', 'pdf') # For export
import seaborn as sns
sns.set()

## Progress bar
from tqdm.auto import tqdm

## JAX
import jax
import jax.numpy as jnp
from jax import random

## Flax (NN in JAX)
try:
import flax
except ModuleNotFoundError: # Install flax if missing
!pip install --quiet flax
import flax
from flax import linen as nn
from flax.training import train_state, checkpoints

## Optax (Optimizers in JAX)
try:
import optax
except ModuleNotFoundError: # Install optax if missing
!pip install --quiet optax
import optax


In contrast to PyTorch, we do not have to set the seed as a global variable since we do not allow for functions with side effects. Instead, we explicitly define a PRNG key whenever needed for random operations like network initializations.

Additionally, the following cell defines two paths: DATASET_PATH and CHECKPOINT_PATH. The dataset path is the directory where we will download datasets used in the notebooks. It is recommended to store all datasets from PyTorch in one joined directory to prevent duplicate downloads. The checkpoint path is the directory where we will store trained model weights and additional files. The needed files will be automatically downloaded. In case you are on Google Colab, it is recommended to change the directories to start from the current directory (i.e. remove ../../ for both dataset and checkpoint path).

:

# Path to the folder where the datasets are/should be downloaded (e.g. MNIST)
DATASET_PATH = "../../data"
# Path to the folder where the pretrained models are saved
CHECKPOINT_PATH = "../../saved_models/tutorial3_jax"

# Verifying the device that will be used throughout this notebook
print("Device:", jax.devices())

Device: gpu:0


The following cell downloads all pretrained models we will use in this notebook. The files are stored on a separate repository to reduce the size of the notebook repository, especially for building the documentation on ReadTheDocs.

:

import urllib.request
from urllib.error import HTTPError
# Github URL where saved models are stored for this tutorial
base_url = "https://raw.githubusercontent.com/phlippe/saved_models/main/JAX/tutorial3/"
pretrained_files = ["FashionMNIST_elu.config", "FashionMNIST_elu.tar",
"FashionMNIST_leakyrelu.config", "FashionMNIST_leakyrelu.tar",
"FashionMNIST_relu.config", "FashionMNIST_relu.tar",
"FashionMNIST_sigmoid.config", "FashionMNIST_sigmoid.tar",
"FashionMNIST_swish.config", "FashionMNIST_swish.tar",
"FashionMNIST_tanh.config", "FashionMNIST_tanh.tar"]
# Create checkpoint path if it doesn't exist yet
os.makedirs(CHECKPOINT_PATH, exist_ok=True)

for file_name in pretrained_files:
file_path = os.path.join(CHECKPOINT_PATH, file_name)
if not os.path.isfile(file_path):
file_url = base_url + file_name
try:
urllib.request.urlretrieve(file_url, file_path)
except HTTPError as e:


## Common activation functions¶

As a first step, we will implement some common activation functions by ourselves. Of course, most of them can also be found in the flax.linen or jax.nn package (see the documentation for an overview). However, we’ll write our own functions here for a better understanding and insights.

Every activation function will be an nn.Module so that we can integrate them nicely in a network. We start with implementing two of the “oldest” activation functions that are still commonly used for various tasks: sigmoid and tanh. Both the sigmoid and tanh activation can be also found as JAX functions (nn.sigmoid, nn.tanh). Note that since all modules are essentially functions, you can directly use functions in nn.Sequential (e.g. nn.Sequential([nn.Dense(8), nn.sigmoid, nn.Dense(1)]). Still, for a better understanding, we implement them by hand:

:

##############################

class Sigmoid(nn.Module):

def __call__(self, x):
return 1 / (1 + jnp.exp(-x))

##############################

class Tanh(nn.Module):

def __call__(self, x):
x_exp, neg_x_exp = jnp.exp(x), jnp.exp(-x)
return (x_exp - neg_x_exp) / (x_exp + neg_x_exp)

##############################


Another popular activation function that has allowed the training of deeper networks, is the Rectified Linear Unit (ReLU). Despite its simplicity of being a piecewise linear function, ReLU has one major benefit compared to sigmoid and tanh: a strong, stable gradient for a large range of values. Based on this idea, a lot of variations of ReLU have been proposed, of which we will implement the following three: LeakyReLU, ELU, and Swish. LeakyReLU replaces the zero settings in the negative part with a smaller slope to allow gradients to flow also in this part of the input. Similarly, ELU replaces the negative part with an exponential decay. The third, most recently proposed activation function is Swish, which is actually the result of a large experiment with the purpose of finding the “optimal” activation function. Compared to the other activation functions, Swish is both smooth and non-monotonic (i.e. contains a change of sign in the gradient). This has been shown to prevent dead neurons as in standard ReLU activation, especially for deep networks. If interested, a more detailed discussion of the benefits of Swish can be found in this paper .

Let’s implement the four activation functions below:

:

##############################

class ReLU(nn.Module):

def __call__(self, x):
return jnp.maximum(x, 0)

##############################

class LeakyReLU(nn.Module):
alpha : float = 0.1

def __call__(self, x):
return jnp.where(x > 0, x, self.alpha * x)

##############################

class ELU(nn.Module):

def __call__(self, x):
return jnp.where(x > 0, x, jnp.exp(x)-1)

##############################

class Swish(nn.Module):

def __call__(self, x):
return x * nn.sigmoid(x)

##############################


For later usage, we summarize all our activation functions in a dictionary mapping the name to the class object. In case you implement a new activation function by yourself, add it here to include it in future comparisons as well:

:

act_fn_by_name = {
"sigmoid": Sigmoid,
"tanh": Tanh,
"relu": ReLU,
"leakyrelu": LeakyReLU,
"elu": ELU,
"swish": Swish
}


### Visualizing activation functions¶

To get an idea of what each activation function actually does, we will visualize them in the following. Since modules are functions in disguise, we can do this by applying JAX’s gradient transformation jax.grad. However, note that we want to take the gradients for multiple inputs independently and the gradient transformation requires the output of the forward pass to be a single scalar. We can implement this by simply summing the outputs of the activation function, since a sum distributes the gradients equally to all its inputs.

:

def get_grads(act_fn, x):
"""
Computes the gradients of an activation function at specified positions.

Inputs:
act_fn - An module or function of the forward pass of the activation function.
x - 1D input array.
Output:
An array with the same size of x containing the gradients of act_fn at x.
"""


Now we can visualize all our activation functions including their gradients:

:

def vis_act_fn(act_fn, ax, x):
# Run activation function
y = act_fn(x)
# Push x, y and gradients back to cpu for plotting
## Plotting
ax.plot(x, y, linewidth=2, label="ActFn")
ax.set_title(act_fn.name)
ax.legend()
ax.set_ylim(-1.5, x.max())

# Add activation functions if wanted
act_fns = [act_fn() for act_fn in act_fn_by_name.values()]
x = np.linspace(-5, 5, 1000) # Range on which we want to visualize the activation functions
## Plotting
rows = math.ceil(len(act_fns)/2.0)
fig, ax = plt.subplots(rows, 2, figsize=(8, rows*4))
for i, act_fn in enumerate(act_fns):
vis_act_fn(act_fn, ax[divmod(i,2)], x)
plt.show() ## Analysing the effect of activation functions¶

After implementing and visualizing the activation functions, we are aiming to gain insights into their effect. We do this by using a simple neural network trained on FashionMNIST and examine various aspects of the model, including the performance and gradient flow.

### Setup¶

Firstly, let’s set up a neural network. The chosen network views the images as 1D tensors and pushes them through a sequence of linear layers and a specified activation function. Feel free to experiment with other network architectures. We explicitly set the initialization for the weights and biases to stay close to the PyTorch implementation of the Tutorial. As we will see in Tutorial 4, the initialization has a considerable influence on which networks may be trainable with which activation functions.

:

# To keep the results close to the PyTorch tutorial, we use the same init function as PyTorch
# which is uniform(-1/sqrt(in_features), 1/sqrt(in_features)) - similar to He et al./kaiming
# The default for Flax is lecun_normal (i.e., half the variance of He) and zeros for bias.
init_func = lambda x: (lambda rng, shape, dtype: random.uniform(rng,
shape=shape,
minval=-1/np.sqrt(x.shape),
maxval=1/np.sqrt(x.shape),
dtype=dtype))

# Network
class BaseNetwork(nn.Module):
act_fn : nn.Module
num_classes : int = 10
hidden_sizes : Sequence = (512, 256, 256, 128)

@nn.compact
def __call__(self, x, return_activations=False):
x = x.reshape(x.shape, -1) # Reshape images to a flat vector
# We collect all activations throughout the network for later visualizations
# Remember that in jitted functions, unused tensors will anyways be removed.
activations = []
for hd in self.hidden_sizes:
x = nn.Dense(hd,
kernel_init=init_func(x),
bias_init=init_func(x))(x)
activations.append(x)
x = self.act_fn(x)
activations.append(x)
x = nn.Dense(self.num_classes,
kernel_init=init_func(x),
bias_init=init_func(x))(x)
return x if not return_activations else (x, activations)


We also add functions for loading and saving the model. The hyperparameters are stored in a configuration file (simple json file):

:

def _get_config_file(model_path, model_name):
# Name of the file for storing hyperparameter details
return os.path.join(model_path, model_name + ".config")

def _get_model_file(model_path, model_name):
# Name of the file for storing network parameters
return os.path.join(model_path, model_name + ".tar")

"""
Loads a saved model from disk.

Inputs:
model_path - Path of the checkpoint directory
model_name - Name of the model (str)
state - (Optional) If given, the parameters are loaded into this training state. Otherwise,
a new one is created alongside a network architecture.
"""
config_file, model_file = _get_config_file(model_path, model_name), _get_model_file(model_path, model_name)
assert os.path.isfile(config_file), f"Could not find the config file \"{config_file}\". Are you sure this is the correct path and you have your model config stored here?"
assert os.path.isfile(model_file), f"Could not find the model file \"{model_file}\". Are you sure this is the correct path and you have your model stored here?"
with open(config_file, "r") as f:
if state is None:
act_fn_name = config_dict["act_fn"].pop("name").lower()
act_fn = act_fn_by_name[act_fn_name](**config_dict.pop("act_fn"))
net = BaseNetwork(act_fn=act_fn, **config_dict)
state = train_state.TrainState(step=0,
params=None,
apply_fn=net.apply,
tx=None,
opt_state=None)
else:
net = None
# You can also use flax's checkpoint package. To show an alternative,
# you can instead load the parameters simply from a pickle file.
with open(model_file, 'rb') as f:
state = state.replace(params=params)
return state, net

def save_model(model, params, model_path, model_name):
"""
Given a model, we save the parameters and hyperparameters.

Inputs:
model - Network object without parameters
params - Parameters to save of the model
model_path - Path of the checkpoint directory
model_name - Name of the model (str)
"""
config_dict = deepcopy(model.__dict__)
config_dict['act_fn'] = config_dict['act_fn'].__dict__
for k in ['parent', 'name', '_state']:
config_dict.pop(k)
config_dict['act_fn'].pop(k)
config_dict['act_fn']['name'] = model.act_fn.__class__.__name__.lower()
os.makedirs(model_path, exist_ok=True)
config_file, model_file = _get_config_file(model_path, model_name), _get_model_file(model_path, model_name)
with open(config_file, "w") as f:
json.dump(config_dict, f)
# You can also use flax's checkpoint package. To show an alternative,
# you can instead save the parameters simply in a pickle file.
with open(model_file, 'wb') as f:
pickle.dump(params, f)


We also set up the dataset we want to train it on, namely FashionMNIST. FashionMNIST is a more complex version of MNIST and contains black-and-white images of clothes instead of digits. The 10 classes include trousers, coats, shoes, bags and more. To load this dataset, we will make use of yet another PyTorch package, namely torchvision (documentation). The torchvision package consists of popular datasets, model architectures, and common image transformations for computer vision. We will use the package for many of the notebooks in this course to simplify our dataset handling.

Let’s load the dataset below, and visualize a few images to get an impression of the data.

:

import torch
import torch.utils.data as data
import torchvision
from torchvision.datasets import FashionMNIST
from torchvision import transforms

# Transformations applied on each image => bring them into a numpy array and normalize between -1 and 1
def image_to_numpy(img):
img = np.array(img, dtype=np.float32)
img = (img / 255. - 0.5) / 0.5
return img

# We need to stack the batch elements as numpy arrays
def numpy_collate(batch):
if isinstance(batch, np.ndarray):
return np.stack(batch)
elif isinstance(batch, (tuple,list)):
transposed = zip(*batch)
return [numpy_collate(samples) for samples in transposed]
else:
return np.array(batch)

# Loading the training dataset. We need to split it into a training and validation part
train_dataset = FashionMNIST(root=DATASET_PATH,
train=True,
transform=image_to_numpy,
train_set, val_set = torch.utils.data.random_split(train_dataset,
[50000, 10000],
generator=torch.Generator().manual_seed(42))

test_set = FashionMNIST(root=DATASET_PATH,
train=False,
transform=image_to_numpy,

# We define a set of data loaders that we can use for various purposes later.
# Note that for actually training a model, we will use different data loaders
# with a lower batch size.
batch_size=1024,
shuffle=False,
drop_last=False,
collate_fn=numpy_collate)
batch_size=1024,
shuffle=False,
drop_last=False,
collate_fn=numpy_collate)
batch_size=1024,
shuffle=False,
drop_last=False,
collate_fn=numpy_collate)

:

exmp_imgs = [train_set[i] for i in range(16)]
# Organize the images into a grid for nicer visualization
img_grid = torchvision.utils.make_grid(torch.from_numpy(np.stack(exmp_imgs, axis=0))[:,None],
nrow=4,
normalize=True,
img_grid = img_grid.permute(1, 2, 0)

plt.figure(figsize=(8,8))
plt.title("FashionMNIST examples")
plt.imshow(img_grid)
plt.axis('off')
plt.show()
plt.close() ### Visualizing the gradient flow after initialization¶

As mentioned previously, one important aspect of activation functions is how they propagate gradients through the network. Imagine we have a very deep neural network with more than 50 layers. The gradients for the input layer, i.e. the very first layer, have passed >50 times the activation function, but we still want them to be of a reasonable size. If the gradient through the activation function is (in expectation) considerably smaller than 1, our gradients will vanish until they reach the input layer. If the gradient through the activation function is larger than 1, the gradients exponentially increase and might explode.

To get a feeling of how every activation function influences the gradients, we can look at a freshly initialized network and measure the gradients for each parameter for a batch of 256 images:

:

small_loader = data.DataLoader(train_set, batch_size=256, shuffle=False, collate_fn=numpy_collate)

:

def visualize_gradients(net, params, color="C0"):
"""
Inputs:
net - Object of class BaseNetwork
color - Color in which we want to visualize the histogram (for easier separation of activation functions)
"""
# Pass one batch through the network, and calculate the gradients for the weights
def loss_func(p):
imgs, labels = exmp_batch
logits = net.apply(p, imgs)
loss = optax.softmax_cross_entropy_with_integer_labels(logits, labels).mean()
return loss
# We limit our visualization to the weight parameters and exclude the bias to reduce the number of plots

## Plotting
fig, ax = plt.subplots(1, columns, figsize=(columns*3.5, 2.5))
fig_index = 0
key = f'Layer {g_idx * 2} - weights'
key_ax = ax[g_idx%columns]
sns.histplot(data=g, bins=30, ax=key_ax, color=color, kde=True)
key_ax.set_title(str(key))
fig.suptitle(f"Gradient magnitude distribution for activation function {net.act_fn.__class__.__name__}", fontsize=14, y=1.05)
plt.show()
plt.close()

:

# Seaborn prints warnings if histogram has small values. We can ignore them for now
import warnings
warnings.filterwarnings('ignore')
## Create a plot for every activation function
for i, act_fn_name in enumerate(act_fn_by_name):
act_fn = act_fn_by_name[act_fn_name]()
net_actfn = BaseNetwork(act_fn=act_fn)
params = net_actfn.init(random.PRNGKey(0), exmp_batch)      The sigmoid activation function shows a clearly undesirable behavior. While the gradients for the output layer are very large with up to 0.1, the input layer has the lowest gradient norm across all activation functions with only 1e-5. This is due to its small maximum gradient of 1/4, and finding a suitable learning rate across all layers is not possible in this setup. All the other activation functions show to have similar gradient norms across all layers. Interestingly, the ReLU activation has a spike around 0 which is caused by its zero-part on the left, and dead neurons (we will take a closer look at this later on).

Note that additionally to the activation, the initialization of the weight parameters can be crucial. By default, PyTorch uses the Kaiming initialization for linear layers optimized for ReLU activations. In Tutorial 4, we will take a closer look at initialization, but assume for now that the Kaiming initialization works for all activation functions reasonably well.

### Training a model¶

Next, we want to train our model with different activation functions on FashionMNIST and compare the gained performance. All in all, our final goal is to achieve the best possible performance on a dataset of our choice. First, let’s write the training and evaluation step that we can compile just-in-time (jax.jit) for efficiency:

:

def calculate_loss(params, apply_fn, batch):
imgs, labels = batch
logits = apply_fn(params, imgs)
loss = optax.softmax_cross_entropy_with_integer_labels(logits, labels).mean()
acc = (labels == logits.argmax(axis=-1)).mean()
return loss, acc

@jax.jit
def train_step(state, batch):
has_aux=True)
return state, acc

@jax.jit
def eval_step(state, batch):
_, acc = calculate_loss(state.params, state.apply_fn, batch)
return acc


Using these functions, we write a training loop in the next cell including a validation after every epoch and a final test on the best model:

:

def train_model(net, model_name, max_epochs=50, patience=7, batch_size=256, overwrite=False):
"""
Train a model on the training set of FashionMNIST

Inputs:
net - Object of BaseNetwork
model_name - (str) Name of the model, used for creating the checkpoint names
max_epochs - Number of epochs we want to (maximally) train for
patience - If the performance on the validation set has not improved for #patience epochs, we stop training early
batch_size - Size of batches used in training
overwrite - Determines how to handle the case when there already exists a checkpoint. If True, it will be overwritten. Otherwise, we skip training.
"""
file_exists = os.path.isfile(_get_model_file(CHECKPOINT_PATH, model_name))
if file_exists and not overwrite:
print("Model file already exists. Skipping training...")
state = None
else:
if file_exists:
print("Model file exists, but will be overwritten...")

# Initializing parameters and training state
params = net.init(random.PRNGKey(42), exmp_batch)
state = train_state.TrainState.create(apply_fn=net.apply,
params=params,
tx=optax.sgd(learning_rate=1e-2,
momentum=0.9))

batch_size=batch_size,
shuffle=True,
drop_last=True,
collate_fn=numpy_collate,
generator=torch.Generator().manual_seed(42))

val_scores = []
best_val_epoch = -1
for epoch in range(max_epochs):
############
# Training #
############
train_acc = 0.
for batch in tqdm(train_loader_local, desc=f"Epoch {epoch+1}", leave=False):
state, acc = train_step(state, batch)
train_acc += acc

##############
# Validation #
##############
val_scores.append(val_acc)
print(f"[Epoch {epoch+1:2d}] Training accuracy: {train_acc:05.2%}, Validation accuracy: {val_acc:4.2%}")

if len(val_scores) == 1 or val_acc > val_scores[best_val_epoch]:
print("\t   (New best performance, saving model...)")
save_model(net, state.params, CHECKPOINT_PATH, model_name)
best_val_epoch = epoch
elif best_val_epoch <= epoch - patience:
print(f"Early stopping due to no improvement over the last {patience} epochs")
break

# Plot a curve of the validation accuracy
plt.plot([i for i in range(1,len(val_scores)+1)], val_scores)
plt.xlabel("Epochs")
plt.ylabel("Validation accuracy")
plt.title(f"Validation performance of {model_name}")
plt.show()
plt.close()

state, _ = load_model(CHECKPOINT_PATH, model_name, state=state)
print((f" Test accuracy: {test_acc:4.2%} ").center(50, "=")+"\n")
return state, test_acc

"""
Test a model on a specified dataset.

Inputs:
state - Training state including parameters and model apply function.
data_loader - DataLoader object of the dataset to test on (validation or test)
"""
true_preds, count = 0., 0
acc = eval_step(state, batch)
batch_size = batch.shape
true_preds += acc * batch_size
count += batch_size
test_acc = true_preds / count
return test_acc


We train one model for each activation function. We recommend using the pretrained models to save time if you are running this notebook on CPU.

:

for act_fn_name in act_fn_by_name:
print(f"Training BaseNetwork with {act_fn_name} activation...")
act_fn = act_fn_by_name[act_fn_name]()
net_actfn = BaseNetwork(act_fn=act_fn)
train_model(net_actfn, f"FashionMNIST_{act_fn_name}", overwrite=False)

Training BaseNetwork with sigmoid activation...
Model file already exists. Skipping training...
============= Test accuracy: 78.46% ==============

Training BaseNetwork with tanh activation...
Model file already exists. Skipping training...
============= Test accuracy: 88.57% ==============

Training BaseNetwork with relu activation...
Model file already exists. Skipping training...
============= Test accuracy: 88.65% ==============

Training BaseNetwork with leakyrelu activation...
Model file already exists. Skipping training...
============= Test accuracy: 88.33% ==============

Training BaseNetwork with elu activation...
Model file already exists. Skipping training...
============= Test accuracy: 88.32% ==============

Training BaseNetwork with swish activation...
Model file already exists. Skipping training...
============= Test accuracy: 88.33% ==============



Surprisingly, in contrast to the PyTorch implementation, the model using the sigmoid activation function actually trains. However, when looking at the validation curve, one can see that the model stays at random performance for many epochs, before it slowly starts learning. The different learning behavior can therefore be explained by small differences in, e.g., initialization or sampled batches.

All the other activation functions gain similar performance to each other and the PyTorch implementation. To have a more accurate conclusion, we would have to train the models for multiple seeds and look at the averages. However, the “optimal” activation function also depends on many other factors (hidden sizes, number of layers, type of layers, task, dataset, optimizer, learning rate, initialization, etc.) so that a thorough grid search would not be useful in our case. In the literature, activation functions that have shown to work well with deep networks are all types of ReLU functions we experiment with here, with small gains for specific activation functions in specific networks.

### Visualizing the activation distribution¶

After we have trained the models, we can look at the actual activation values that find inside the model. For instance, how many neurons are set to zero in ReLU? Where do we find most values in Tanh? To answer these questions, we can write a simple function which takes a trained model, applies it to a batch of images, and plots the histogram of the activations inside the network:

:

def visualize_activations(net, color="C0"):
activations = {}

imgs, labels = exmp_batch
_, activations = net(imgs, return_activations=True)

## Plotting
columns = 4
rows = math.ceil(len(activations)/columns)
fig, ax = plt.subplots(rows, columns, figsize=(columns*2.7, rows*2.5))
act_fn_name = net.act_fn.__class__.__name__
for idx, activ in enumerate(activations):
key_ax = ax[idx//columns][idx%columns]
sns.histplot(data=activ.reshape(-1), bins=50, ax=key_ax, color=color, kde=True, stat="density")
key_ax.set_title(f"Layer {idx} - {'Dense' if idx%2==0 else act_fn_name}")
fig.suptitle(f"Activation distribution for activation function {act_fn_name}", fontsize=14)
plt.show()
plt.close()

:

for i, act_fn_name in enumerate(act_fn_by_name):
net_actfn = net_actfn.bind(state.params)
visualize_activations(net_actfn, color=f"C{i}")      For the model with sigmoid activation, we see a very high peak for the values 0 and 1 in the last layer. This suggests that the model is close to the saturation bounds, in which gradients do not properly flow back. Further, this is also the effect of the inconsistent gradient magnitudes we have seen before: since the layer closest to the output had the highest gradients, we see it is much stronger converged than, e.g., the second layer.

The tanh shows a more diverse behavior overall. While for the input layer we experience a larger amount of neurons to be close to -1 and 1, where the gradients are close to zero, the activations in the two consecutive layers are closer to zero. This is probably because the input layers look for specific features in the input image, and the consecutive layers combine those together. The activations for the last layer are again more biased to the extreme points because the classification layer can be seen as a weighted average of those values (the gradients push the activations to those extremes).

The ReLU has a strong peak at 0, as we initially expected. The effect of having no gradients for negative values is that the network does not have a Gaussian-like distribution after the linear layers, but a longer tail towards the positive values. The LeakyReLU shows a very similar behavior while ELU follows again a more Gaussian-like distribution. The Swish activation seems to lie in between, although it is worth noting that Swish uses significantly higher values than other activation functions (up to 15).

As all activation functions show slightly different behavior although obtaining similar performance for our simple network, it becomes apparent that the selection of the “optimal” activation function really depends on many factors, and is not the same for all possible networks.

### Finding dead neurons in ReLU networks¶

One known drawback of the ReLU activation is the occurrence of “dead neurons”, i.e. neurons with no gradient for any training input. The issue of dead neurons is that as no gradient is provided for the layer, we cannot train the parameters of this neuron in the previous layer to obtain output values besides zero. For dead neurons to happen, the output value of a specific neuron of the linear layer before the ReLU has to be negative for all input images. Considering the large number of neurons we have in a neural network, it is not unlikely for this to happen.

To get a better understanding of how much of a problem this is, and when we need to be careful, we will measure how many dead neurons different networks have. For this, we implement a function which runs the network on the whole training set and records whether a neuron is exactly 0 for all data points or not:

:

def measure_number_dead_neurons(net):
# For each neuron, we create a boolean variable initially set to 1. If it has an activation unequals 0 at any time,
# we set this variable to 0. After running through the whole training set, only dead neurons will have a 1.
jnp.ones(hd, dtype=jnp.dtype('bool')) for hd in net.hidden_sizes
] # Same shapes as hidden size in BaseNetwork

get_activations = jax.jit(lambda inp: net(inp, return_activations=True))
for imgs, _ in tqdm(train_loader, leave=False): # Run through whole training set
activations = get_activations(imgs)
for layer_index, activ in enumerate(activations[1::2]):
# Are all activations == 0 in the batch, and we did not record the opposite in the last batches?


First, we can measure the number of dead neurons for an untrained network:

:

net_relu = BaseNetwork(act_fn=ReLU())
params = net_relu.init(random.PRNGKey(42), exmp_batch)

Number of dead neurons: [0, 0, 3, 7]
In percentage: 0.00%, 0.00%, 1.17%, 5.47%


We see that only a minor amount of neurons are dead, but that they increase with the depth of the layer. However, this is not a problem for the small number of dead neurons we have as the input to later layers is changed due to updates to the weights of previous layers. Therefore, dead neurons in later layers can potentially become “alive”/active again.

How does this look like for a trained network (with the same initialization)?

:

state, net_relu = load_model(model_path=CHECKPOINT_PATH, model_name="FashionMNIST_relu")

Number of dead neurons: [0, 0, 3, 3]
In percentage: 0.00%, 0.00%, 1.17%, 2.34%


The number of dead neurons indeed decreased in the later layers. However, it should be noted that dead neurons are especially problematic in the input layer. As the input does not change over epochs (the training set is kept as it is), training the network cannot turn those neurons back active. Still, the input data has usually a sufficiently high standard deviation to reduce the risk of dead neurons.

Finally, we check how the number of dead neurons behaves with increasing layer depth. For instance, let’s take the following 10-layer neural network:

:

net_relu = BaseNetwork(act_fn=ReLU(), hidden_sizes=[256, 256, 256, 256, 256, 128, 128, 128, 128, 128])
params = net_relu.init(random.PRNGKey(42), exmp_batch)

Number of dead neurons: [0, 1, 1, 19, 75, 53, 71, 57, 64, 69]