Hi Arjun!



 grid_world:

def measure_module_sparsity(module, weight = True, bias = False, use_mask = False):

    num_zeros = 0
    num_elements = 0

    if use_mask == True:
        for buffer_name, buffer in module.named_buffers():
            ...
    else:
        for param_name, param in module.named_parameters():
            ...

    sparsity = num_zeros / num_elements

    return num_zeros, num_elements, sparsity


 22
 —> 23 sparsity = num_zeros / num_elements
 24
 25     return num_zeros, num_elements, sparsity

ZeroDivisionError: division by zero

What’s the problem … ?


If you call measure_module_sparsity() on a Module such as ReLU
that contains neither buffers nor parameters, then num_elements (and
num_zeros) will remain zero (because they never get incremented)
so you get the ZeroDivisionError.
Best.
K. Frank