Quantitative Testing with Concept Activation Vectors (TCAV) in NLP
March 17, 2019 · View on GitHub
Data preparation
We use Lenta.Ru dataset in our experiments.
- Create
data_pathfolder and putlenta-ru-news.csvfile in it - Choose labels to experiment with and split the data into train and test by running
lenta_dataset.ipynb - Finally, run
python data_prep.py -dd data_path. This command will savefull.csv,train.csv,eval.csvandvocabs.txtfiles todata_path
Training
- Modify
build_modelfunction if you want to change an architecture of the model - Create
experiment_pathfolder and putexperiments/config.yamlin it - Modify hyperparameters inside
experiment_path/config.yaml - Run
python train.py -dd data_path -md experiment_path. This command will train the model and save checkpoints toexperiment_path
Create concepts
- Choose words you would like to experiments with. For example,
Москва,ООН,Жириновскийwill be a good choice. - Run
python collect_concepts.py -dd data_path -md experiment_path --ngrams 3. This command will generate multiple files:
concepts.pkl-- for each concept (e.g.Москва) we search for sentences where this word occurs. Then we retrieve ngrams of sizenfrom this sentence (e.g.лето в Москва,Москва слезам не верит) and call it concepts. Also we collect some random samples from the data for each concept.cav_bottlenecks.pkl-- we convert concept texts into hidden representations of the model fromexperiment_pathfoldercavs.pkl-- Hyperplanes for each concept received by fitting Logistic Regression onconcept/non-conceptdata. LR is trained on hidden representation of the data.grads.pkl-- directional derivatives (see the paper for more details)
Calculate TCAV scores
- Run
python calculate_tcav.py -dd data_path. This command will savescores.pklfile. In this file you can find TCAV scores for each concept against all labels.
Plot graphs
Run TCAV.ipynb to compare results.