Giter VIP home page Giter VIP logo

bert-for-classification's Introduction

BERT-for-Classification

Some background knowledge:
BERT can generate 2 kind of output: 1. two dimensional sequence_output(word embeddings) . 2. one dimensional pooled output(cls or sentence embedding) I recommend to use sequence_output of BERT(Many paper show that this is better than cls embedding).

Python 3.6
Required Library:
PyTorch
transformers: pip install transformers
tensorflow2.0

I recommend to use Bert_classification_tf2.py or ipynb

I set max sequence length to 512. You can change it in code.

For tensorflow 2.0: Bert_classification_tf2.py or ipynb
I implemented 3 models for fine-tuning BERT:

  1. create_model_sequence_output(trainable=True): create a fine-tune model that uses two dimensional sequence_output of BERT(word embeddings)
  2. create_model_cls_output(trainable=True): create a fine-tune model that uses one dimensional pooled output(cls or sentence embedding)
  3. create_model_2(trainable=True): as same as create_model_cls_output(trainable=True)
    if trainable=True: --> the weights of pre-trained BERT will also be adjusted during fine-tuning.
    if trainable=False: --> the weights of pre-trained BERT will not be adjusted during fine-tuning.

For pytorch: Bert_classification_pytorch.py or ipynb
I only implemented 1 model for fine-tuning BERT: create a fine-tune model that uses one dimensional pooled output(cls or sentence embedding)

I put the example codes inside each python file. I used part of data in IMDB datatset as an example.

Reference:
Chris McCormick and Nick Ryan, "BERT Fine-Tuning Tutorial with PyTorch", https://mccormickml.com/2019/07/22/BERT-fine-tuning/

bert-for-classification's People

Contributors

nbcstevenchen avatar

Watchers

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