utils/data_utils.py
def plot_all_activations_and_grads(activations):
    """
    Plot all activations and grads of the network
    :param activations:
    :return:
    """
    num_cols = 4
    num_rows = len(activations)

    fig_width = 6
    fig_height = num_rows * fig_width / num_cols

    fontsize = 5

    fig, axs = plt.subplots(num_rows, num_cols, gridspec_kw={'hspace': 0.5, 'wspace': 0.4},
                            figsize=(fig_width, fig_height), dpi=300)

    axs[0][0].set_title("Activation Distribution", fontsize=7, fontfamily='serif', pad=5.)
    axs[0][1].set_title("Activation Spectrum", fontsize=7, fontfamily='serif', pad=5.)
    axs[0][2].set_title("Gradient Distribution", fontsize=7, fontfamily='serif', pad=5.)
    axs[0][3].set_title("Gradient Spectrum", fontsize=7, fontfamily='serif', pad=5.)

    x_formatter = matplotlib.ticker.FuncFormatter(lambda x, pos: eformat(x, 0, 1))
    y_formatter = matplotlib.ticker.FuncFormatter(lambda x, pos: eformat(x, 0, 1))

    for idx, (key, value) in enumerate(activations.items()):
        grad_value = value.grad.cpu().detach().squeeze(0)
        flat_grad = grad_value.view(-1)
        axs[idx][2].hist(flat_grad, bins=256, density=True)

        value = value.cpu().detach().squeeze(0)  # (1, num_points, 256)
        flat_value = value.view(-1)

        axs[idx][0].hist(flat_value, bins=256, density=True)

        if idx > 1:
            if not (idx) % 2:
                x = np.linspace(-1, 1., 500)
                axs[idx][0].plot(x, stats.arcsine.pdf(x, -1, 2),
                                 linestyle=':', markersize=0.4, zorder=2)
            else:
                mu = 0
                variance = 1
                sigma = np.sqrt(variance)
                x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 500)
                axs[idx][0].plot(x, stats.norm.pdf(x, mu, sigma),
                                 linestyle=':', markersize=0.4, zorder=2)

        activ_freq, activ_spec = get_spectrum(value)
        axs[idx][1].plot(activ_freq, activ_spec)

        grad_freq, grad_spec = get_spectrum(grad_value)
        axs[idx][-1].plot(grad_freq, grad_spec)

        for ax in axs[idx]:
            ax.tick_params(axis='both', which='major', direction='in',
                           labelsize=fontsize, pad=1., zorder=10)
            ax.tick_params(axis='x', labelrotation=0, pad=1.5, zorder=10)

            ax.xaxis.set_major_formatter(x_formatter)
            ax.yaxis.set_major_formatter(y_formatter)

    Plot all activations and grads of the network
    :param activations:
    :return:
    
Plot all activations and grads of the network
Plot all activations and grads of the network
def plot_all_activations_and_grads(activations):
    num_cols = 4
    num_rows = len(activations)
    fig_width = 6
    fig_height = num_rows * fig_width / num_cols
    fontsize = 5
    fig, axs = plt.subplots(num_rows, num_cols, gridspec_kw={'hspace': 0.5, 'wspace': 0.4},
                            figsize=(fig_width, fig_height), dpi=300)
    axs[0][0].set_title("Activation Distribution", fontsize=7, fontfamily='serif', pad=5.)
    axs[0][1].set_title("Activation Spectrum", fontsize=7, fontfamily='serif', pad=5.)
    axs[0][2].set_title("Gradient Distribution", fontsize=7, fontfamily='serif', pad=5.)
    axs[0][3].set_title("Gradient Spectrum", fontsize=7, fontfamily='serif', pad=5.)
    x_formatter = matplotlib.ticker.FuncFormatter(lambda x, pos: eformat(x, 0, 1))
    y_formatter = matplotlib.ticker.FuncFormatter(lambda x, pos: eformat(x, 0, 1))
    for idx, (key, value) in enumerate(activations.items()):
        grad_value = value.grad.cpu().detach().squeeze(0)
        flat_grad = grad_value.view(-1)
        axs[idx][2].hist(flat_grad, bins=256, density=True)
        value = value.cpu().detach().squeeze(0)  
        flat_value = value.view(-1)
        axs[idx][0].hist(flat_value, bins=256, density=True)
        if idx > 1:
            if not (idx) % 2:
                x = np.linspace(-1, 1., 500)
                axs[idx][0].plot(x, stats.arcsine.pdf(x, -1, 2),
                                 linestyle=':', markersize=0.4, zorder=2)
            else:
                mu = 0
                variance = 1
                sigma = np.sqrt(variance)
                x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 500)
                axs[idx][0].plot(x, stats.norm.pdf(x, mu, sigma),
                                 linestyle=':', markersize=0.4, zorder=2)
        activ_freq, activ_spec = get_spectrum(value)
        axs[idx][1].plot(activ_freq, activ_spec)
        grad_freq, grad_spec = get_spectrum(grad_value)
        axs[idx][-1].plot(grad_freq, grad_spec)
        for ax in axs[idx]:
            ax.tick_params(axis='both', which='major', direction='in',
                           labelsize=fontsize, pad=1., zorder=10)
            ax.tick_params(axis='x', labelrotation=0, pad=1.5, zorder=10)
            ax.xaxis.set_major_formatter(x_formatter)
            ax.yaxis.set_major_formatter(y_formatter)
def plot_all_activations_and_grads ( activations ) : num_cols = 4 num_rows = len ( activations ) fig_width = 6 fig_height = num_rows * fig_width / num_cols fontsize = 5 fig , axs = plt . subplots ( num_rows , num_cols , gridspec_kw = { 'hspace' : 0.5 , 'wspace' : 0.4 } , figsize = ( fig_width , fig_height ) , dpi = 300 ) axs [ 0 ] [ 0 ] . set_title ( "Activation Distribution" , fontsize = 7 , fontfamily = 'serif' , pad = 5. ) axs [ 0 ] [ 1 ] . set_title ( "Activation Spectrum" , fontsize = 7 , fontfamily = 'serif' , pad = 5. ) axs [ 0 ] [ 2 ] . set_title ( "Gradient Distribution" , fontsize = 7 , fontfamily = 'serif' , pad = 5. ) axs [ 0 ] [ 3 ] . set_title ( "Gradient Spectrum" , fontsize = 7 , fontfamily = 'serif' , pad = 5. ) x_formatter = matplotlib . ticker . FuncFormatter ( lambda x , pos : eformat ( x , 0 , 1 ) ) y_formatter = matplotlib . ticker . FuncFormatter ( lambda x , pos : eformat ( x , 0 , 1 ) ) for idx , ( key , value ) in enumerate ( activations . items ( ) ) : grad_value = value . grad . cpu ( ) . detach ( ) . squeeze ( 0 ) flat_grad = grad_value . view ( - 1 ) axs [ idx ] [ 2 ] . hist ( flat_grad , bins = 256 , density = True ) value = value . cpu ( ) . detach ( ) . squeeze ( 0 ) flat_value = value . view ( - 1 ) axs [ idx ] [ 0 ] . hist ( flat_value , bins = 256 , density = True ) if idx > 1 : if not ( idx ) % 2 : x = np . linspace ( - 1 , 1. , 500 ) axs [ idx ] [ 0 ] . plot ( x , stats . arcsine . pdf ( x , - 1 , 2 ) , linestyle = ':' , markersize = 0.4 , zorder = 2 ) else : mu = 0 variance = 1 sigma = np . sqrt ( variance ) x = np . linspace ( mu - 3 * sigma , mu + 3 * sigma , 500 ) axs [ idx ] [ 0 ] . plot ( x , stats . norm . pdf ( x , mu , sigma ) , linestyle = ':' , markersize = 0.4 , zorder = 2 ) activ_freq , activ_spec = get_spectrum ( value ) axs [ idx ] [ 1 ] . plot ( activ_freq , activ_spec ) grad_freq , grad_spec = get_spectrum ( grad_value ) axs [ idx ] [ - 1 ] . plot ( grad_freq , grad_spec ) for ax in axs [ idx ] : ax . tick_params ( axis = 'both' , which = 'major' , direction = 'in' , labelsize = fontsize , pad = 1. , zorder = 10 ) ax . tick_params ( axis = 'x' , labelrotation = 0 , pad = 1.5 , zorder = 10 ) ax . xaxis . set_major_formatter ( x_formatter ) ax . yaxis . set_major_formatter ( y_formatter )
Plot all activations and grads of the network
Plot all activations and grads of the network
"""
    Plot all activations and grads of the network
    :param activations:
    :return:
    """ # (1, num_points, 256)
None
activations
None