Skip to content

yancyou/stance_prediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stance Prediction

This project fine-tunes Flan-T5-Large to predict the stance of tweets toward the COVID-19 vaccine, classifying each tweet into one of three categories:

  • in-favor
  • against
  • neutral-or-unclear

It supports both full fine-tuning and inference with optional in-context few-shot examples.


🔍 Repository Structure

stance_prediction/
├── data/                                 # Raw and processed CSV files    
│   └── Q2_20230202_majority.csv          # Example data
├── finetuned_models/                     # Saved model checkpoints
│   └── best_model/                       # Best checkpoint (after training)
├── outputs/                              # My output
│   └── predictions_fine_tuning_ICL.csv   # The result of fine-tuning model using in-context learning
    └── predictions_fine_tuning.csv       # The result of fine-tuning model
    └── predictions_no_fine_tuning.csv    # The result of original model          
├── src/                                  # Source code
│   ├── data_loader.py                    # DataLoader to read and preprocess CSVs
│   ├── train.py                          # Training script (fine-tune Flan-T5)
│   ├── infer.py                          # Inference script (predict on new tweets)
│   └── model.py                          # Wrapper class for loading & generating
├── requirements.txt                      # Python dependencies
└── README.md                             # Project documentation

📦 Installation

  1. Clone this repository:

    git clone https://github.com/yancyou/stance_prediction.git
    cd stance_prediction
  2. Create a conda or virtualenv environment and install dependencies:

    conda create -n stance_pred python=3.10
    conda activate stance_pred
    pip install -r requirements.txt

🗂️ Data Preparation

Place your CSV file(s) in the data/ folder. The CSV must have two columns:

  • tweet — the raw tweet text
  • label_majority — the ground-truth stance

🚀 Training

Use src/train.py to fine-tune Flan-T5:

python src/train.py \
  --data_path data/Q2_20230202_majority.csv \
  • --data_path: CSV file for training and validation (80/20 split).
  • By default it saves checkpoints to finetuned_models and writes the best model under finetuned_models/best_model.

Key features:

  • Automatic tokenization and dataset formatting with 🤗 Datasets.
  • Early stopping after 4 epochs without improvement.
  • Saves and loads the best checkpoint by macro F1.

🔮 Inference

After training, run src/infer.py to predict on unseen tweets:

python src/infer.py \
  --input your_test_data_path \
  --output your_prediction_data_output_path \
  --model_path finetuned_models/best_model \
  • --model_path: point to your saved checkpoint.
  • --context_learning(optional): prepend few-shot examples at inference time.

The output CSV will contain a new column label_pred with one of:

in-favor, against, neutral-or-unclear


💡 In‑Context Learning (Optional)

You can use in-context learning method in inference. Set --context_learning True in inference to activate. However, experiments show that the results of In-Context Learning are not as good as those of normal reasoning.

For my trained model, you can download it directly from the following link and then put it under finetuned_models/ for inference https://drive.google.com/file/d/1pbMnXQ0OHem7AFGJ8aaLz1pvzQyg1-sm/view?usp=sharing

Happy stance predicting! 🚀

About

This is a project about predicting COVID-19 vaccine stance on tweets

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages