Code Monkey home page Code Monkey logo

probing's Introduction

Probing

Probing is a popular evaluation method for blackbox language models. In the simplest case, the representation of a token or a sentence is fed to a small classifier that tries to predict some linguistic label. This setup is exposed to a small amount of training data but only the classifier parameters are trained, the blackbox model's parameters are kept fixed.

This library was developed for probing contextualized language models such as BERT.

Our papers that use this library

This framework was used in the following projects:

Subword Pooling Makes a Difference

EACL2021 paper

Github repository

Evaluating Contextualized Language Models for Hungarian

Paper

Github repository

Evaluating Transferability of BERT Models on Uralic Languages

Paper

Github repository

Probing types and tasks

It supports two types of evaluation.

Morphology probing

Probe a single word in its sentence context, such as deriving the tense of the English word cut in these examples, where context clearly plays an important role:

I cut my hair yesterday.
Make sure you cut the edges.

Data format

Morphology probing uses TSV files as input. One line represents one sample. Each line has 4 tab-separated columns:

  1. the full sentence
  2. the target word
  3. the index of the target word when using space tokenization
  4. the label.

The above two examples would look like this:

I cut my hair yesterday.        cut        1        Past        
Make sure you cut the edges.    cut        3        Pres

In tagging tasks each line represents a word and sentence boundaries are denoted by empty lines. Each line consists of a word and its tag or label separated by a tab.

An example for English POS tagging:

If      SCONJ
you     PRON
need    VERB
any     DET
more    ADJ
info    NOUN
,       PUNCT
please  INTJ
advise  VERB
.       PUNCT

Hi      INTJ
all     DET
.       PUNCT

See examples/data for more.

Tagging

Tagging assigns a label to each word in the sentence. Common examples are part of speech tagging or named entity recognition.

Probing setup

We support most BERT-like language models available in Huggingface's repository. We add a small multilayer perceptron on top of the representation and train its weights. The language models are not finetuned and we cache the output of the language models when possible. This allows running a very large number of experiments on a single GPU. For tagging tasks, the parameters of the MLP are shared across all tokens.

Installation

Requirements:

  • Python >= 3.6
  • PyTorch >= 1.7 (we recommend installing PyTorch before installing this package)

Install command:

cd <PATH_TO_PROBING_REPO>
pip install .

Usage

Configuration

Experiment configuration is managed through YAML config files. We provided some examples in the examples/config directory along with example toy datasets in the examples/data directory. The train and dev files can be provided as command line arguments as well.

Training a single experiment

Morphology probing:

python probing/train.py \
    --config examples/config/transformer_probing.yaml \
    --train-file examples/data/morphology_probing/english_verb_tense/train.tsv \
    --dev-file examples/data/morphology_probing/english_verb_tense/dev.tsv

POS tagging:

python probing/train.py \
    --config examples/config/pos_tagging.yaml \
    --train-file examples/data/pos_tagging/english/train \
    --dev-file examples/data/pos_tagging/english/dev

Training multiple experiments

train_many_configs.py takes a Python source file as its parameter which must contain a function named generate_configs returns or yields Config objects. This makes it possible to run an arbitrary number of experiments with varying configuration. This toy example trains two models for English POS tagging, one that uses the first subword token of each token and one that used the last subword token. We explore these options in detail in this paper.

python probing/train_many_configs.py \
    --config examples/config/pos_tagging.yaml \
    --param-generator examples/config/generate_pos_configs.py

Inference

Inference on one experiment:

python probing/inference.py \
    --experiment-dir PATH_TO_AN_EXPDIR \
    --test-file PATH_TO_A_TEST_FILE \
    > output

If no test file is provided, it reads from the standard input.

Inference on multiple experiments:

python probing/batch_inference.py \
    EXPERIMENT_DIRS \
    --run-on-dev \
    --run-on-test

EXPERIMENT_DIRS is an arbitrary number of positional arguments accepted by batch_inference.py. It may by a glob such as ~/workdir/exps/*. If --run-on-dev or --run-on-test is not provided, the dev or the test set will not be evaluated. batch_inference.py only evaluated directories where there is no test.out or it is older than the last model checkpoint.

probing's People

Contributors

juditacs avatar

Stargazers

Varvara Arzt avatar HMRLKE avatar Rishabh avatar Stefan Schweter avatar

Watchers

James Cloos avatar  avatar

probing's Issues

Pytorch version >= 1.13

When installing this library to an environment which uses pytorch 1.12.1, everything works fine. Once the Pytorch version is greater than 1.13, dict subscription problems arise. I suspect, that it has something to do with this modification: pytorch/pytorch#72778. Usage with downgraded pytorch can still be an option, however the README recommends installing PyTorch before the probing package without mentioning any kind of upper-bound regarding the version of the pytorch.

The first error I get when trying to use probing with pytorch >= 1.13 is an IndexError:

Traceback (most recent call last): File "~/probing/src/probing/train.py", line 38, in <module> main() File "~/probing/src/probing/train.py", line 32, in main e.run() File "~/probing/src/probing/experiment.py", line 152, in run self.model.run_train( File "~/probing/src/probing/models/base.py", line 60, in run_train s = self.save_if_best(result, epoch) File "~/probing/src/probing/models/base.py", line 154, in save_if_best self._save(epoch) File "~/probing/src/probing/models/base.py", line 174, in _save torch.save(self.state_dict(), save_path) File "~/miniconda3/envs/dep-probe/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1444, in state_dict module.state_dict(destination=destination, prefix=prefix + name + '.', keep_vars=keep_vars) File "~/probing/src/probing/models/contextual_embedding_classifier.py", line 135, in state_dict return args[0] IndexError: tuple index out of range

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.