<jupyter_start><jupyter_text>How to use ScorersThis demo includes how to use scorers to get same size scores for each score.<jupyter_code>import torch
from torch.autograd import Variable
from torch.nn import Parameter
import sys
sys.path.insert(0,"../")
from pytorchpruner.scorers import magnitudeScorer,gradientScorer,hessianScorer

TOLERANCE = 1e-5
# We have 4 random parameters
w = Parameter(torch.rand(2,2))
print(w)

def L2d(w):
    '''
    A custom loss function
        w00 w01
        w10 w11
    '''    
    return (w[1,0]**2)*w[1,1]+4*(w[0,0]**3)*w[1,0]

def G2d(w):
    #Jacobian of the L(w)
    wd = w.data
    return torch.Tensor([
                         [12*(wd[0,0]**2)*wd[1,0],
                          0],
                         [2*wd[1,0]*wd[1,1]+4*(wd[0,0]**3),
                          wd[1,0]**2]
                        ])

#torch.autograd
loss_val = L2d(w)
grad_score = gradientScorer(loss_val, w)
print(grad_score)
#emprical gradient
correct_score = G2d(w)
# print(correct_score)
print((grad_score-correct_score).abs().sum() < TOLERANCE)

hessian_score = hessianScorer(loss_val,w)

print(hessian_score)
def H2dScore(w):
    #Hessian of the L2(w)
    wd=w.data
    gw12 = 2*wd[1,0]
    gw13 = 12*(wd[0,0]**2)
    gw23 = 0
    gw11 = 2*wd[1,1]
    gw22 = 0
    gw33 = 24*wd[0,0]*wd[1,0]
    ## x3 0
    ## x1 x2
    return torch.Tensor([[gw33+0+gw13+gw23, #0,0,: #x3 with others
                       0+0+0+0], #0,1,:
                      [gw13+0+gw11+gw12, #1,0,: #x1 with others
                       gw23+0+gw12+gw22]]) #1,1,: #x2 with others

correct_score = H2dScore( w)
print((correct_score-hessian_score).abs().sum() < TOLERANCE)<jupyter_output>3.0034  0.0000
 2.9434  0.3537
[torch.FloatTensor of size 2x2]

True
{'fraction_non_alphanumeric': 0.1519131924614506, 'fraction_numerical': 0.08280982295830953, 'mean_word_length': 2.5901639344262297, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '113454', 'license': 'MIT', 'n_tokens_mistral': 788, 'n_tokens_neox': 728, 'n_words': 141, 'path': 'notebooks/demo_scorers.ipynb', 'repo_name': 'evcu/pytorchpruner', 'size': '1751'}