Giter VIP home page Giter VIP logo

mindnlp's Introduction

MindNLP

docs GitHub PRs Welcome open issues ci

Introduction | Quick Links | Installation | Get Started | Tutorials | Notes

News ๐Ÿ“ข

  • ๐Ÿ”ฅ Latest Features
    • ๐Ÿ“ƒ Support PreTrained Models, including BERT, Roberta, GPT2 and T5. You can use them by following code snippet:
      from mindnlp.models import BertModel
      
      model = BertModel.from_pretrained('bert-base-cased')

Introduction

MindNLP is an open source NLP library based on MindSpore. It supports a platform for solving natural language processing tasks, containing many common approaches in NLP. It can help researchers and developers to construct and train models more conveniently and rapidly.

The master branch works with MindSpore master.

Major Features

  • Comprehensive data processing: Several classical NLP datasets are packaged into friendly module for easy use, such as Multi30k, SQuAD, CoNLL, etc.
  • Friendly NLP model toolset: MindNLP provides various configurable components. It is friendly to customize models using MindNLP.
  • Easy-to-use engine: MindNLP simplified complicated training process in MindSpore. It supports Trainer and Evaluator interfaces to train and evaluate models easily.

Quick Links

Installation

Dependency

  • mindspore >= 1.8.1

Install from source

To install MindNLP from source, please run:

pip install git+https://github.com/mindspore-lab/mindnlp.git

Get Started

We will next quickly implement a sentiment classification task by using mindnlp.

Define Model

from mindspore import ops
from mindnlp.abc import Seq2vecModel

class SentimentClassification(Seq2vecModel):
    def construct(self, text):
        _, (hidden, _), _ = self.encoder(text)
        context = ops.concat((hidden[-2, :, :], hidden[-1, :, :]), axis=1)
        output = self.head(context)
        return output

Define Hyperparameters

The following are some of the required hyperparameters in the model training process.

# define Models & Loss & Optimizer
hidden_size = 256
output_size = 1
num_layers = 2
bidirectional = True
drop = 0.5
lr = 0.001

Data Preprocessing

The dataset was downloaded and preprocessed by calling the interface of dataset in mindnlp.

Load dataset:

from mindnlp import load_dataset

imdb_train, imdb_test = load_dataset('imdb', shuffle=True)

Initializes the vocab and tokenizer for preprocessing:

from mindnlp import Vocab
from mindnlp.transforms import BasicTokenizer

tokenizer = BasicTokenizer(True)
vocab = Vocab.from_pretrained(name="glove.6B.100d")

The loaded dataset is preprocessed and divided into training and validation:

from mindnlp.dataset import process

imdb_train = process('imdb', imdb_train, tokenizer=tokenizer, vocab=vocab, \
                     bucket_boundaries=[400, 500], max_len=600, drop_remainder=True)
imdb_test = process('imdb', imdb_test, tokenizer=tokenizer, vocab=vocab, \
                     bucket_boundaries=[400, 500], max_len=600, drop_remainder=False)

Instantiate Model

from mindnlp.modules import RNNEncoder, Glove

embedding = Glove.from_pretrained('6B', 100, special_tokens=["<unk>", "<pad>"])
# build encoder
lstm_layer = nn.LSTM(100, hidden_size, num_layers=num_layers, batch_first=True,
                     dropout=dropout, bidirectional=bidirectional)
encoder = RNNEncoder(embedding, lstm_layer)

# build head
head = nn.SequentialCell([
    nn.Dropout(p=dropout),
    nn.Sigmoid(),
    nn.Dense(hidden_size * 2, output_size,
             weight_init=HeUniform(math.sqrt(5)),
             bias_init=Uniform(1 / math.sqrt(hidden_size * 2)))

])

# build network
network = SentimentClassification(encoder, head)
loss = nn.BCELoss(reduction='mean')
optimizer = nn.Adam(network.trainable_params(), learning_rate=lr)

Training Process

Now that we have completed all the preparations, we can begin to train the model.

from mindnlp.engine.metrics import Accuracy
from mindnlp.engine.trainer import Trainer

# define metrics
metric = Accuracy()

# define trainer
trainer = Trainer(network=network, train_dataset=imdb_train, eval_dataset=imdb_test, metrics=metric,
                  epochs=5, loss_fn=loss, optimizer=optimizer)
trainer.run(tgt_columns="label")

License

This project is released under the Apache 2.0 license.

Feedbacks and Contact

The dynamic version is still under development, if you find any issue or have an idea on new features, please don't hesitate to contact us via Github Issues.

Acknowledgement

MindSpore is an open source project that welcome any contribution and feedback.
We wish that the toolbox and benchmark could serve the growing research
community by providing a flexible as well as standardized toolkit to reimplement existing methods
and develop their own new semantic segmentation methods.

Citation

If you find this project useful in your research, please consider citing:

@misc{mindnlp2022,
    title={{MindNLP}: a MindSpore NLP library},
    author={MindNLP Contributors},
    howpublished = {\url{https://github.com/mindlab-ai/mindnlp}},
    year={2022}
}

mindnlp's People

Contributors

lvyufeng avatar ttyee avatar geaming-chn avatar it-is-a-robot avatar tianyuzhou avatar zzhangyutong avatar warruzuendo avatar purrigin avatar eyrechan avatar laixinyi823 avatar floutione avatar sugarfreeliuyuxuan avatar iron-boyy avatar kuoyee avatar heaodong0 avatar linorman avatar stuprosur avatar gaojinpeng8 avatar khoray avatar braveseeker avatar baolanchen avatar suprecyk avatar yicorner avatar zeithaum avatar daiyuxin0511 avatar wangxingran222 avatar 1114120549 avatar mihaw99 avatar xiaoyi3 avatar robin-ex 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.