Git Product home page Git Product logo

interpret_bert's Introduction

What does BERT learn about the structure of language?

Code used in our ACL'19 paper for interpreting BERT model.

Dependencies

Quick Start

Phrasal Syntax (Section 3 in paper)

  • Navigate:
cd chunking/
wget https://www.clips.uantwerpen.be/conll2000/chunking/train.txt.gz
gunzip train.txt.gz

The last command replaces train.txt.gz file with train.txt file.

  • Extract BERT features for chunking related tasks (clustering and visualization):
python extract_features.py --train_file train.txt --output_file chunking_rep.json
  • Run t-SNE of span embeddings for each BERT layer (Figure 1):
python visualize.py --feat_file chunking_rep.json --output_file_prefix tsne_layer_

This would create one t-SNE plot for each BERT layer and stores as pdf (e.g. tsne_layer_0.pdf).

  • Run KMeans to evaluate the clustering performance of span embeddings for each BERT layer (Table 1):
python cluster.py --feat_file chunking_rep.json

Probing Tasks (Section 4)

  • Navigate:
cd probing/
python extract_features.py --data_file tree_depth.txt --output_file tree_depth_rep.json

In the above command, append --untrained_bert flag to extract untrained BERT features.

  • Train the probing classifier for a given BERT layer (indexed from 0) and evaluate the performance (Table 2):
python classifier.py --labels_file tree_depth.txt --feats_file tree_depth_rep.json --layer 0

We use the hyperparameter search space recommended by SentEval.

Subject-Verb Agreement (SVA) (Section 5)

  • Navigate:
cd sva/
python extract_features.py --data_file agr_50_mostcommon_10K.tsv --output_folder ./
  • Train the binary classifier for a given BERT layer (indexed from 0) and evaluate the performance (Table 3):
python classifier.py --input_folder ./ --layer 0

We use the hyperparameter search space recommended by SentEval.

Compositional Structure (Section 6)

  • Navigate:
cd tpdn/
  • Download the SNLI 1.0 corpus and extract it.
  • Extract BERT features for premise sentences present in SNLI:
python extract_features.py --input_folder . --output_folder .
  • Train the Tensor Product Decomposition Network (TPDN) to approximate a given BERT layer (indexed from 0) and evaluate the performance (Table 4):
python approx.py --input_folder . --output_folder . --layer 0

Check --role_scheme and --rand_tree flags for setting the role scheme.

  • Induce dependency parse tree from attention weights for a given attention head and BERT layer (both indexed from 1) (Figure 2):
python induce_dep_trees.py --sentence text "The keys to the cabinet are on the table" --head_id 11 --layer_id 2 --sentence_root 6 

Acknowledgements

This repository would not be possible without the efforts of the creators/maintainers of the following libraries:

License

This repository is GPL-licensed.

interpret_bert's People

Contributors

ganeshjawahar avatar

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.