Code Monkey home page Code Monkey logo

nlp_project's Introduction

PyTorch implementation of OpenAI's Finetuned Transformer Language Model

This repo is modified from ORIGINAL PYTORCH IMPLEMENTATION.

The project report is Story Cloze Test with Fine Adjustment Schemes of GPT-2 Model.

README

This is a PyTorch implementation of the TensorFlow code provided with OpenAI's paper "Improving Language Understanding by Generative Pre-Training" by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.

This implementation comprises a script to load in the PyTorch model the weights pre-trained by the authors with the TensorFlow implementation.

Transformer Language Model

The model classes and loading script are located in model_pytorch.py.

The names of the modules in the PyTorch model follow the names of the Variable in the TensorFlow implementation. This implementation tries to follow the original code as closely as possible to minimize the discrepancies.

This implementation thus also comprises a modified Adam optimization algorithm as used in OpenAI's paper with:

Requirements

To use the model it-self by importing model_pytorch.py, you just need:

  • PyTorch (version >=0.4)

To run the classifier training script in train.py you will need in addition:

  • tqdm
  • sklearn
  • spacy
  • ftfy
  • pandas
  • allennlp

AllenNLP

Use AllenNLP to generate part-of-speech tags with sentences. First, download the pre-trained model from (https://s3-us-west-2.amazonaws.com/allennlp/models/elmo-constituency-parser-2018.03.14.tar.gz)

python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/test_sentence_1.json --predictor=constituency-parser --output-file parsed_data/test_sentence_1.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/test_sentence_2.json --predictor=constituency-parser --output-file parsed_data/test_sentence_2.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/test_sentence_3.json --predictor=constituency-parser --output-file parsed_data/test_sentence_3.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/test_sentence_4.json --predictor=constituency-parser --output-file parsed_data/test_sentence_4.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/test_sentence_5_op1.json --predictor=constituency-parser --output-file parsed_data/test_sentence_5_op1.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/test_sentence_5_op2.json --predictor=constituency-parser --output-file parsed_data/test_sentence_5_op2.txt --batch-size 128 --cuda-device 0

python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/val_sentence_1.json --predictor=constituency-parser --output-file parsed_data/val_sentence_1.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/val_sentence_2.json --predictor=constituency-parser --output-file parsed_data/val_sentence_2.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/val_sentence_3.json --predictor=constituency-parser --output-file parsed_data/val_sentence_3.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/val_sentence_4.json --predictor=constituency-parser --output-file parsed_data/val_sentence_4.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/val_sentence_5_op1.json --predictor=constituency-parser --output-file parsed_data/val_sentence_5_op1.txt --batch-size 128 --cuda-device 0
python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/val_sentence_5_op2.json --predictor=constituency-parser --output-file parsed_data/val_sentence_5_op2.txt --batch-size 128 --cuda-device 0

python -m allennlp.run predict elmo-constituency-parser-2018.03.14.tar.gz orig_data/train_sentence_5.json --predictor=constituency-parser --output-file parsed_data/train_sentence_5.txt --batch-size 128 --cuda-device 0

Fine-tuning the pre-trained model on a classification task

The ROCStories dataset can be downloaded from the associated website.

As with the TensorFlow code, this code implements the ROCStories Cloze Test result reported in the paper which can be reproduced by running:

python -m spacy download en
python train.py --dataset rocstories --desc rocstories --submit --analysis --data_dir data

Since we have 5 datasets in our data_dir, you will need to modify the path of the data in datasets.py, in order to get the 5 results mentioned in our paper.

In baseline model, we used the original validation set as our training data. In case 1, we used reorganized validationset as training data, and tested on reorganized testing set. In case 2, we used reorganized validation set as training data, and tested on the original testing set. In case 3, we combined the manually generated antonym sentences with the validation set as our new training set, and then tested on the original test set. In case 4, we only used the last sentence of the validation set as our training data, and tested on the original testing set.

Experiments on the ROCStories test set with different datasets

The test accuracy for baseline model is 86.53%, for case 1 is 86.37%, for case 2 is 85.89%, for case 3 is 85.36%, and for case 4 is 84.5%.

nlp_project's People

Contributors

wangxizhe2017 avatar ttppss avatar

Watchers

 avatar  avatar paper2code - bot 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.