GithubHelp home page GithubHelp logo

gu-datalab / stance-detection-ke-mlm Goto Github PK

View Code? Open in Web Editor NEW
39.0 4.0 2.0 106 KB

Official resource of the paper "Knowledge Enhanced Masked Language Model for Stance Detection", NAACL 2021

Home Page: https://www.aclweb.org/anthology/2021.naacl-main.376/

License: GNU General Public License v3.0

Python 100.00%
stance-detection stance-classification stance-dataset natural-language-processing naacl2021 naacl pretrained-models language-models bert twitter

stance-detection-ke-mlm's Introduction

Stance Detection

This repository is for the paper - Knowledge Enhance Masked Language Model for Stance Detection, NAACL 2021. ๐Ÿš€

Code for log-odds-ratio with Dirichlet prior is at log-odds-ratio repository.

Data Sets

This data sets are for research purposes only - Download ๐Ÿ”ฅ

  • Data format is CSV with only 3 columns: "tweet_id","text","label"
  • Labels = {0:"AGAINST", 1:"FAVOR", 2:"NONE"}

The data set contains 2500 manually-stance-labeled tweets, 1250 for each candidate (Joe Biden and Donald Trump). These tweets were sampled from the unlabeled set that our research team collected English tweets related to the 2020 US Presidential election. Through the Twitter Streaming API, we collected data using election-related hashtags and keywords. Between January 2020 and September 2020, we collected over 5 million tweets, not including quotes and retweets. These unlabeled tweets were used to fine-tune all of our language models. The labeled data that we publicly provide were sampled from this 5M set and were labeled using Amazon Mechanical Turk.

The stance label distributions are shown in the table below. Please refer to our paper for more detail about the data sets.

%SUPPORT %OPPOSE %NEUTRAL
Biden 31.3 39.0 29.8
Trump 27.3 39.9 32.8

Result

On each pre-trained language model, we trained for the downstream stance detection task for five times and report average scores in Table 2.

image

Pre-trained Models

All models are uploaded to my Huggingface ๐Ÿค— so you can load model with just three lines of code!!!

Usage

We tested in pytorch v1.8.1 and transformers v4.5.1.

Please see specific model pages above for more usage detail. Below is a sample use case.

1. Choose and load model for stance detection

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np

# select mode path here
# see more at https://huggingface.co/kornosk
pretrained_LM_path = "kornosk/bert-election2020-twitter-stance-biden-KE-MLM"

# load model
tokenizer = AutoTokenizer.from_pretrained(pretrained_LM_path)
model = AutoModelForSequenceClassification.from_pretrained(pretrained_LM_path)

2. Get a prediction (see more in sample_predict.py)

id2label = {
    0: "AGAINST",
    1: "FAVOR",
    2: "NONE"
}

##### Prediction Favor #####
sentence = "Go Go Biden!!!"
inputs = tokenizer(sentence, return_tensors="pt")
outputs = model(**inputs)
predicted_probability = torch.softmax(outputs[0], dim=1)[0].tolist()

print("Sentence:", sentence)
print("Prediction:", id2label[np.argmax(predicted_probability)])
print("Against:", predicted_probability[0])
print("Favor:", predicted_probability[1])
print("Neutral:", predicted_probability[2])

# please consider citing our paper if you feel this is useful :)

Citation

If you feel our paper and resources are useful, please consider citing our work! ๐Ÿ™

@inproceedings{kawintiranon2021knowledge,
    title={Knowledge Enhanced Masked Language Model for Stance Detection},
    author={Kawintiranon, Kornraphop and Singh, Lisa},
    booktitle={Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies},
    year={2021},
    publisher={Association for Computational Linguistics},
    url={https://www.aclweb.org/anthology/2021.naacl-main.376}
}

Troubleshoot

1. Can't load the model

  • From this issue
  • Check the dependencies pytorch==1.8.1 and transformers==4.5.1
  • Try removing tensorflow

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.