Skip to main content

Designed to ensure robust generalization across time stamps for sentiment analysis in policy-related texts

Project description

tempsentpolicy

Temporally-sensitive Sentiment analysis in Policy (tempsentpolicy) is a Python package ddesigned to ensure robust generalization across time stamps for sentiment analysis in policy-related texts.

It accounts for the temporal dynamics of evolving policy discourse and evaluates sentiment models under realistic settings that mimic actual policy analysis scenarios.

Key features include:

  • Continuous time-series clustering to prioritize data points for annotation based on temporal trends.

  • Advanced model merging techniques to unify multiple models fine-tuned on distinct temporal slices.

Methods:

  • Continous time series is performed using Ruptures.

  • It supports the following merging techniques:

    1. Uniform Souping (souping)

    2. Greedy Souping (greedy_souping)

    3. Task Arithmetic (task_arithmetic)

    4. TIES (ties)

    5. DARE (dare)

    6. Fisher Merging (fisher_merging)

    7. RegMean Merging (regmean_merging)

Requirements

Required Packages

The following Python packages are required:

  • matplotlib

  • ruptures

  • pandas

  • tqdm

  • torch

  • transformers

  • datasets

  • scikit-learn

  • numpy

These will be installed automatically when using pip install.

Alternatively, you could directly install them

pip install matplotlib ruptures pandas tqdm torch transformers datasets scikit-learn numpy

Computational requirements

You require at least one GPU to use tempsentpolicy.

VRAM requirements depend on factors like batch size, model size, etc.

However, at least 12GB of VRAM is recommended

Installation

To install in python, simply do the following:

pip install tempsentpolicy

Quick start

To learn how we used it in our experiments, go to demo.ipynb. Alternatively, refer to this quick guide here:

  1. Lets start off by performing continous time series clustering to select our annotated data
from tempsentpolicy import greedy_souping, ties

from tempsentpolicy import finetune

from tempsentpolicy import cluster_sampling, continous_time_series_clustering



df_climate=continous_time_series_clustering(df=df,time_col="timestamp_col",level="M", plot=False, penalty=0.1) # note that level "M" stands for Month



# perform continous time series clustering and selecting based on the clusters

df=cluster_sampling(df=df,sample_size=10000,stratified_col="cluster"):

df_annotated=df[df["selected"]==1].reset_index(drop=True)
  1. Now first lets finetune a single unified model
# finetuning

finetune(df=df_annotated, model_name="microsoft/deberta-v3-large",cluster_col_name=None,

         folder_name="naive_finetuning/continous_clustering_by_month/climate_change", # note folder name to save our models

         text_col='message', label_col="sentiment",  label2id={"Anti":0,"Neutral":1,"Pro":2,"News":3},

         learning_rate=1e-5, warmup_ratio=0.05, weight_decay=0.001,

         epochs=3, batch_size=6, early_stopping_patience=2, return_val_data=False)
  1. Now first lets finetune individual models (one for each cluster), and merge them with greedy soup.
# finetuning

df_val=finetune(df=df_annotated, model_name="microsoft/deberta-v3-large",

                cluster_col_name="cluster", # the difference is here --> we use "cluster" instead of None

                folder_name="batch_finetuning/continous_clustering_by_month/climate_change", # note folder name to save our models

                text_col='message', 

                label_col="sentiment",  

                label2id={"Anti":0,"Neutral":1,"Pro":2,"News":3},

                learning_rate=1e-5, 

                warmup_ratio=0.05, 

                weight_decay=0.001,

                epochs=8, 

                batch_size=6, 

                early_stopping_patience=2, 

                return_val_data=True) # note TRUE if we want to return validation data. 



# Now we perform greedy souping

greedy_souping(

    models_folder="models/batch_finetuning/continous_clustering_by_month/climate_change/deberta-v3-large", # where the individual models are saved

    save_path="models/merged_models/continous_clustering_by_month/climate_change/greedy_soup/deberta-v3-large", # the merged model will be saved here

    df_val=df_val,  # validation data. 

    col_label="sentiment",

    text_col='message',

    num_labels=4,

    label2id={"Anti":0,"Neutral":1,"Pro":2,"News":3})



# the model will be saved under `save_path="models/merged_models/continous_clustering_by_month/climate_change/greedy_soup/deberta-v3-large"`

NOTE

This is a temporary repository for the purposes of anonymity in the ACL SRW submission. Upon acceptance, it will be converted to a package under the main authors PyPi account. Stay tuned for updates!!!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tempsentpolicy-0.0.3.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tempsentpolicy-0.0.3-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file tempsentpolicy-0.0.3.tar.gz.

File metadata

  • Download URL: tempsentpolicy-0.0.3.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tempsentpolicy-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9b0c9a290ef87efbb965b86b29c298aeaa91a585e5b278549f0e780455578d9d
MD5 7c43d12693f6886640d0742569eab349
BLAKE2b-256 feccf0f03d7c6d8a0fa49a77ff4fa43b8583f0b43ab98143f89869053458ff83

See more details on using hashes here.

File details

Details for the file tempsentpolicy-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: tempsentpolicy-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tempsentpolicy-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 af7cc9aab2cfe05ecaee4a3d25407fa1bd8393d3343e24b97f8b886b1cd1dc42
MD5 caf42973a0e1f8fc5b2c691f5b5e7a3b
BLAKE2b-256 c193236703be09a14c25020773c014cf9f3f77844ed05c713e6138192e141c1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page