Views
No views yet
Mishra, Sudhanshu, Shivangi Prasad, and Shubhanshu Mishra. 2020. "Multilingual Joint Fine-Tuning of Transformer Models for Identifying Trolling, Aggression and Cyberbullying at TRAC 2020." In Proceedings of the Second Workshop on Trolling, Aggression and Cyberbullying (TRAC-2020).
@inproceedings{Mishra2020TRAC,
author = {Mishra, Sudhanshu and Prasad, Shivangi and Mishra, Shubhanshu},
booktitle = {Proceedings of the Second Workshop on Trolling, Aggression and Cyberbullying (TRAC-2020)},
title = {{Multilingual Joint Fine-tuning of Transformer models for identifying Trolling, Aggression and Cyberbullying at TRAC 2020}},
year = {2020}
}
@data{illinoisdatabankIDB-8882752,
author = {Mishra, Shubhanshu and Prasad, Shivangi and Mishra, Shubhanshu},
doi = {10.13012/B2IDB-8882752_V1},
publisher = {University of Illinois at Urbana-Champaign},
title = {{Trained models for Multilingual Joint Fine-tuning of Transformer models for identifying Trolling, Aggression and Cyberbullying at TRAC 2020}},
url = {https://doi.org/10.13012/B2IDB-8882752{\_}V1},
year = {2020}
}1from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification
2import torch
3from pathlib import Path
4from scipy.special import softmax
5import numpy as np
6import pandas as pd
7
8TASK_LABEL_IDS = {
9 "Sub-task A": ["OAG", "NAG", "CAG"],
10 "Sub-task B": ["GEN", "NGEN"],
11 "Sub-task C": ["OAG-GEN", "OAG-NGEN", "NAG-GEN", "NAG-NGEN", "CAG-GEN", "CAG-NGEN"]
12}
13
14model_version="databank" # other option is hugging face library
15if model_version == "databank":
16 # Make sure you have downloaded the required model file from https://databank.illinois.edu/datasets/IDB-8882752
17 # Unzip the file at some model_path (we are using: "databank_model")
18 model_path = next(Path("databank_model").glob("./*/output/*/model"))
19 # Assuming you get the following type of structure inside "databank_model"
20 # 'databank_model/ALL/Sub-task C/output/bert-base-multilingual-uncased/model'
21 lang, task, _, base_model, _ = model_path.parts
22 tokenizer = AutoTokenizer.from_pretrained(base_model)
23 model = AutoModelForSequenceClassification.from_pretrained(model_path)
24else:
25 lang, task, base_model = "ALL", "Sub-task C", "bert-base-multilingual-uncased"
26 base_model = f"socialmediaie/TRAC2020_{lang}_{lang.split()[-1]}_{base_model}"
27 tokenizer = AutoTokenizer.from_pretrained(base_model)
28 model = AutoModelForSequenceClassification.from_pretrained(base_model)
29
30# For doing inference set model in eval mode
31model.eval()
32# If you want to further fine-tune the model you can reset the model to model.train()
33
34task_labels = TASK_LABEL_IDS[task]
35
36sentence = "This is a good cat and this is a bad dog."
37processed_sentence = f"{tokenizer.cls_token} {sentence}"
38tokens = tokenizer.tokenize(sentence)
39indexed_tokens = tokenizer.convert_tokens_to_ids(tokens)
40tokens_tensor = torch.tensor([indexed_tokens])
41
42with torch.no_grad():
43 logits, = model(tokens_tensor, labels=None)
44logits
45
46
47preds = logits.detach().cpu().numpy()
48preds_probs = softmax(preds, axis=1)
49preds = np.argmax(preds_probs, axis=1)
50preds_labels = np.array(task_labels)[preds]
51print(dict(zip(task_labels, preds_probs[0])), preds_labels)
52"""You should get an output as follows:
53
54({'CAG-GEN': 0.06762535,
55 'CAG-NGEN': 0.03244293,
56 'NAG-GEN': 0.6897794,
57 'NAG-NGEN': 0.15498641,
58 'OAG-GEN': 0.034373745,
59 'OAG-NGEN': 0.020792078},
60 array(['NAG-GEN'], dtype='<U8'))
61
62"""
63