Category: Deep Learning

  • Frankenstein

    1. Example: Roberta GoEmotion
      1. Loading model
      2. Norm Space
      3. Network comparison
    2. What does this all mean?

    While evaluating pre-trained models for classification tasks, I realized that the output projection layers reveal much more insight into the model’s behavior, especially when viewed in Euclidean space.

    Example: Roberta-base GoEmotion

    Understanding the inner workings of pre-trained models for classification tasks can be evaluated by analyzing the outputs from a multi-label classification model trained from the roberta-base on the go_emotions. Below is a quick overview of importing the model and fetching predictions.

    1. Loading the model

    Note that it considers the index with the maximum probability. Below is a list of the predicted emotions and their softmax scores

    2. Fetching Predictions (the usual way)
    # tokenize the text
    text = 'I’m glad I had a few margaritas before I read this'
    test = tokenizer(text.lower(), padding='max_length', truncation=True, return_tensors='pt')
    # classifies the tokens
    with torch.no_grad():
    output=model(**test)
    model.config.id2label[torch.argmax(output.logits).item()]
    [Output] 'Joy'

    Below is a list of the predicted emotions and their softmax scores for each emotion class. Note that it considers the index with the maximum softmax scores.

    alternate way of viewing the output projection space of the model

    Another perspective for visualizing the output projections of the model involves conceptualizing Euclidean space by imagining a unit circle centered at the origin of a Cartesian plane. The following code demonstrates how the norms of the weights are computed and its corresponding output.

    I realized that the closer the norms were for each classification label, the less effective the model performs and tested this hypothesis by comparing the model with other models in the remainder of this post.

    Using the same text for predictions, I considered the norm instead. The voted class is then represented as the minimal score instead. Update: An alternative method in outputting the same results is by considering log_softmax(-1)**2.

    torch.tensor([torch.norm(i, p=2).item() for i in output.logits.reshape(-1,1)])

    The results prompts another way of viewing the behaviour of the model. For instance, some possible concluding thoughts that I have hypothesized includes:

    • The model’s voted emotion class is by considering the projected data points to some known ‘centre’ point expected for a class. The voted class is concluded by considering the distance of the point that is closest to the centre of each class. This explains why the output distances are all in with respect to the voted class ‘joy’. This prompts the idea that, perhaps, we are able to derive the marginal distribution conditioned to each class label.
    • The model’s output projections could be viewed as a multivariate normal distribution with mean = 0 and the covariance is the matrix product of the output weights with its transpose. If we consider the log_space(-1)**2 for each class, this will give the standard deviation.
    Network comparison

    Table 1 presents the norms of the last projection layers for sentimental multi-label classifiers. The following models, along with their respective column names mentioned in the subsequent tables, are listed. PS: I randomly selected the third model to assess whether the architectures of the models would influence the network’s consistency

    Moreover, I observed that the poorer the models perform, the smaller the differences in radii become. This trend is evident in Table 1.1, where based on my analysis, the first_model appears to have outperformed the others.

    Table 1.1: L2-norm of output projections

    emote first_model second_model evil_model
    0 admiration 0.724544 NaN NaN
    1 amusement 0.762517 NaN NaN
    2 anger 0.694420 0.589196 NaN
    3 annoyance 0.631149 NaN NaN
    4 approval 0.634043 NaN NaN
    5 caring 0.735186 NaN NaN
    6 confusion 0.702024 NaN NaN
    7 curiosity 0.691748 NaN NaN
    8 desire 0.742869 NaN NaN
    9 disappointment 0.670009 NaN NaN
    10 disapproval 0.666855 NaN NaN
    11 disgust 0.723540 0.580254 NaN
    12 embarrassment 0.765843 NaN NaN
    13 excitement 0.707815 NaN NaN
    14 fear 0.779006 0.563287 NaN
    15 gratitude 0.830122 NaN NaN
    16 grief 0.733750 NaN NaN
    17 joy 0.702564 0.568222 NaN
    18 love 0.777553 NaN NaN
    19 nervousness 0.760698 NaN NaN
    20 optimism 0.692720 NaN NaN
    21 pride 0.766037 NaN NaN
    22 realization 0.628699 NaN NaN
    23 relief 0.747458 NaN NaN
    24 remorse 0.768121 NaN NaN
    25 sadness 0.708254 0.546891 NaN
    26 surprise 0.718787 0.548415 NaN
    27 neutral 0.611252 0.562038 NaN
    28 toxic NaN NaN 0.616543
    29 severe_toxic NaN NaN 0.634660
    30 obscene NaN NaN 0.646761
    31 threat NaN NaN 0.672640
    32 insult NaN NaN 0.611311
    33 identity_hate NaN NaN 0.647124

    Running the text ‘My dog is so sad, what to do’ through the models. Below is a list of the final prediction output for each model.

    • first_model : ‘sadness’
    • second_model : ‘sadness’
    • evil_model : ‘toxic’

    Table 1.2: Predicted Emotion models comparison

    emote first_model second_model evil_model
    0 admiration 50.162245 NaN NaN
    1 amusement 62.996836 NaN NaN
    2 anger 54.838597 46.635899 NaN
    3 annoyance 49.396201 NaN NaN
    4 approval 51.556458 NaN NaN
    5 caring 45.806955 NaN NaN
    6 confusion 69.319389 NaN NaN
    7 curiosity 60.085917 NaN NaN
    8 desire 57.541514 NaN NaN
    9 disappointment 26.382446 NaN NaN
    10 disapproval 56.603353 NaN NaN
    11 disgust 60.335418 34.897641 NaN
    12 embarrassment 76.500645 NaN NaN
    13 excitement 74.679552 NaN NaN
    14 fear 64.041907 43.940767 NaN
    15 gratitude 64.262277 NaN NaN
    16 grief 30.496395 NaN NaN
    17 joy 60.346812 36.453455 NaN
    18 love 42.962534 NaN NaN
    19 nervousness 61.658221 NaN NaN
    20 optimism 69.589441 NaN NaN
    21 pride 100.404767 NaN NaN
    22 realization 47.456986 NaN NaN
    23 relief 76.255958 NaN NaN
    24 remorse 39.107556 NaN NaN
    25 sadness 0.000713 0.001020 NaN
    26 surprise 67.425321 25.456962 NaN
    27 neutral 34.383273 16.373191 NaN
    28 toxic NaN NaN 0.017664
    29 severe_toxic NaN NaN 22.385596
    30 obscene NaN NaN 12.478684
    31 threat NaN NaN 15.735884
    32 insult NaN NaN 9.626380
    33 identity_hate NaN NaN 14.381756

    Just for fun, Table 1.3 displays the normalised tensors of the final projection layers. This was normalised by dividing the vector by its norm. I was wondering if we are able to retrieve the unit vectors or directional vectors where its direction could lead to insights into the projected data’s neighbourhood. However, this may need further clarifications.

    Table 1.3: Raw Tensors for each model

    .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
    emote first_model second_model evil_model
    admiration 0.994845 NaN NaN
    amusement -0.128493 NaN NaN
    anger -1.619268 -1.777849 NaN
    annoyance -0.081112 NaN NaN
    approval -0.318533 NaN NaN
    caring 0.763438 NaN NaN
    confusion 1.004717 NaN NaN
    curiosity -0.164215 NaN NaN
    desire -1.033424 NaN NaN
    disappointment -0.599124 NaN NaN
    disapproval -1.426283 NaN NaN
    disgust -0.153697 -0.953630 NaN
    embarrassment 0.299044 NaN NaN
    excitement -0.363511 NaN NaN
    fear 1.034656 0.276441 NaN
    gratitude -0.148751 NaN NaN
    grief 0.700838 NaN NaN
    joy 0.751123 -1.037767 NaN
    love -0.630390 NaN NaN
    nervousness 0.008017 NaN NaN
    optimism 0.250943 NaN NaN
    pride 2.107973 NaN NaN
    realization -0.223193 NaN NaN
    relief 1.374453 NaN NaN
    remorse 0.548389 NaN NaN
    sadness 1.011848 -0.976564 NaN
    surprise -0.686557 -0.208700 NaN
    neutral 1.217619 0.721465 NaN
    toxic NaN NaN -0.912919
    severe_toxic NaN NaN 0.380740
    obscene NaN NaN -1.738774
    threat NaN NaN 0.167458
    insult NaN NaN -0.460666
    identity_hate NaN NaN -0.560966

    What does this all mean?

    • This is similar to Ensemble Learning but this allows the the possibility of fine-tuning models without retraining networks.
    • Feasibility of merging networks without the need of additional architectural layers.
    • The feasibility of integrating multiple networks together lies in defining a function capable of approximating the multivariate distribution conditioned on the models and other features seamlessly. This can be achieved through the following methods:
      • Gaussian Processes with Kernel Methods
      • Bayes Regression
      • Hyperplanes Modelling e.g. K-means, SVM,..etc
      • Any algorithm where marginal distribution is a worthy feature
      • Any algorithm under Bayes Theorem
    • Fun ways to try with this:
      • Given that it is such a portable, retrievable, accessible and adorable layer, you can use this to negate uncertainty for other networks – additional analysis for pre-trained models.
      • Improve a currently well-behaved networks with simple algorithms like sampling methods
      • Everything

    Tom’s progression · Post 3 of 14

    This post sits in Cognition: Perception.

    ← Reviving Hopfield Neural Networks Missing pieces to Naomi →