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
# Load model directly
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("SamLowe/roberta-base-go_emotions", return_tensors='pt')
model = AutoModelForSequenceClassification.from_pretrained("SamLowe/roberta-base-go_emotions")
# tokenize 'I’m glad I had a few margaritas before I read this'
test = tokenizer(df.text.values[0].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()]
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 texttext = '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 tokenswith 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.
{'admiration': 0.004,
'amusement': 0.0026,
'anger': 0.0004,
'annoyance': 0.0013,
'approval': 0.0119,
'caring': 0.0024,
'confusion': 0.0005,
'curiosity': 0.0002,
'desire': 0.0005,
'disappointment': 0.0005,
'disapproval': 0.0012,
'disgust': 0.0001,
'embarrassment': 0.0002,
'excitement': 0.0029,
'fear': 0.0001,
'gratitude': 0.0076,
'grief': 0.0002,
'joy': 0.9349,
'love': 0.0008,
'nervousness': 0.0003,
'optimism': 0.0016,
'pride': 0.0016,
'relization': 0.0031,
'relief': 0.0129,
'remorse': 0.0002,
'sadness': 0.0007,
'surprise': 0.0003,
'neutral': 0.0069}
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.
embed = torch.tensor([torch.norm(i, p=2).item() for i in model.classifier.out_proj.weight])
{i[1]: embed[i[0]].item() for i in model.config.id2label.items()}
{'admiration': 0.7245,
'amusement': 0.7625,
'anger': 0.6944,
'annoyance': 0.6311,
'approval': 0.634,
'caring': 0.7352,
'confusion': 0.702,
'curiosity': 0.6917,
'desire': 0.7429,
'disappointment': 0.67,
'disapproval': 0.6669,
'disgust': 0.7235,
'embarrassment': 0.7658,
'excitement': 0.7078,
'fear': 0.779,
'gratitude': 0.8301,
'grief': 0.7337,
'joy': 0.7026,
'love': 0.7776,
'nervousness': 0.7607,
'optimism': 0.6927,
'pride': 0.766,
'relization': 0.6287,
'relief': 0.7475,
'remorse': 0.7681,
'sadness': 0.7083,
'surprise': 0.7188,
'neutral': 0.6113}
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)])
{'admiration': 5.511953491833992,
'amusement': 5.953417517943308,
'anger': 7.929373024846427,
'annoyance': 6.644042590312893,
'approval': 4.43350100776297,
'caring': 6.016423052817117,
'confusion': 7.512215155817103,
'curiosity': 8.613046554732136,
'desire': 7.549981436692178,
'disappointment': 7.6848024653154425,
'disapproval': 6.720061205676757,
'disgust': 8.848052183631808,
'embarrassment': 8.319211663678288,
'excitement': 5.83931942115305,
'fear': 8.936948264716193,
'gratitude': 4.884281546110287,
'grief': 8.367577656637877,
'joy': 0.0680289221345447,
'love': 7.151230112125631,
'nervousness': 8.021991087867718,
'optimism': 6.466608848510077,
'pride': 6.432584019610658,
'relization': 5.777172007452464,
'relief': 4.354517621570267,
'remorse': 8.373851888580248,
'sadness': 7.291984996700194,
'surprise': 7.960167621495202,
'neutral': 4.981706313439645}
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
- first_model : Model trained on GoEmotion [Roberta]
- Output projection layer size [28, 768]
- second_model : Model trained on Twitter corpus [Roberta]
- Output projection layer size [7, 768]
- evil_model : Model to predict toxicity [Bert]
- Output projection layer size [6, 512]
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
| 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 →