Package: fastbert
FastBERT-pypiThe pypi version ofFastBERT.InstallInstallfastbertwithpip.$pipinstallfastbertSupported ModelsFastBERT-pypi is supported by theUERproject, and all of UER high-quality models can be accelerated in the FastBERT way.FastBERTobject supports the following models:Models (kernel_name)URLDescriptiongoogle_bert_base_enhttps://share.weiyun.com/fpdOtcmzGoogle pretrained English BERT-base model on Wiki corpus.google_bert_base_zhhttps://share.weiyun.com/AykBph9VGoogle pretrained Chinese BERT-base model on Wiki corpus.uer_bert_large_zhhttps://share.weiyun.com/chx2VhGkUER pretrained Chinese BERT-large model on mixed corpus.uer_bert_small_zhhttps://share.weiyun.com/wZuVBM5gUER pretrained Chinese BERT-small model on mixed corpus.uer_bert_tiny_zhhttps://share.weiyun.com/VJ3JEN9ZUER pretrained Chinese BERT-tiny model on mixed corpus.In fact, you don't have to download the model yourself. FastBERT will download the corresponding model file automatically at the first time you use it. If the automatically downloading failed, you can download these model files from the above URLs, and saving them to the directory of "~/.fastbert/".Quick StartSingle sentence classificationAn example of single sentence classification are shown insingle_sentence_classification.fromfastbertimportFastBERT# Loading your datasetlabels=['T','F']sents_train=['Do you like FastBERT?','Yes, it runs faster than BERT!',...]labels_train=['T','F',...]# Creating a modelmodel=FastBERT(kernel_name="google_bert_base_en",# "google_bert_base_zh" for Chineselabels=labels,device='cuda:0')# Training the modelmodel.fit(sents_train,labels_train,model_saving_path='./fastbert.bin',)# Loading the model and making inferencemodel.load_model('./fastbert.bin')label,exec_layers=model('I like FastBERT',speed=0.7)Two sentences classificationAn example of two sentences classification are presented intwo_sentences_classification.fromfastbertimportFastBERT_S2# Loading your datasetlabels=['T','F']questions_train=['FastBERT快吗?','你在业务里使用FastBERT了吗?',...]answers_train=['快！而且速度还可调.','用了啊，帮我省了好几百台机器.',...]labels_train=['T','T',...]# Creating a modelmodel=FastBERT_S2(kernel_name="google_bert_base_zh",# "google_bert_base_en" for Englishlabels=labels,device='cuda:0')# Training the modelmodel.fit(sents_a_train=questions_train,sents_b_train=answers_train,labels_train=labels_train,model_saving_path='./fastbert.bin',)# Loading the model and making inferencemodel.load_model('./fastbert.bin')label,exec_layers=model(sent_a='我也要用FastBERT!',sent_b='来，吃老干妈!',speed=0.7)UsageArgs ofFastBERT/FastBERT_S2:ArgsTypeExamplesExplanationkernel_namestr'google_bert_base_en'The name of the kernel model, including 'google_bert_base_en', 'google_bert_base_zh'.labelslist['T', 'F']A list of all labels.seq_length (optional)int256The sentence length for FastBERT. Default 128device (optional)str'cuda:0'The device for runing FastBERT, default 'cpu'Args ofFastBERT.fit():ArgsTypeExamplesExplanationsentences_trainlist['sent 1', 'sent 2',...]A list of training sentences.labels_trainlist['T', 'F', ...]A list of training labels.batch_size (optional)int32batch_size for training. Default 16sentences_dev (optional)list[]A list of validation sentences.labels_dev (optional)list[]A list of validation labels.learning_rate (optional)float2e-5learning rate.finetuning_epochs_num (optional)int5The epoch number of finetuning.distilling_epochs_num (optional)int10The epoch number of distilling.report_steps (optional)int100Report the training process every [report_steps] steps.warmup (optional)float0.1The warmup rate for training.dev_speed (optional)float0.5The speed for evaluating in the self-distilling process.model_saving_path (optional)str'./model.bin'The path to saving model.Args ofFastBERT.forward():ArgsTypeExamplesExplanationsentencestr'How are you'The input sentence.speed (optional)float0.5The speed value for inference. Default 0.0.Args ofFastBERT_S2.fit():ArgsTypeExamplesExplanationsents_a_trainlist['sent a 1', 'sent a 2',...]A list of training A-sentences.sents_b_trainlist['sent b 1', 'sent b 2',...]A list of training B-sentences.labels_trainlist['T', 'F', ...]A list of training labels.batch_size (optional)int32batch_size for training. Default 16sents_a_dev (optional)list[]A list of validation A-sentences.sents_b_dev (optional)list[]A list of validation B-sentences.labels_dev (optional)list[]A list of validation labels.learning_rate (optional)float2e-5learning rate.finetuning_epochs_num (optional)int5The epoch number of finetuning.distilling_epochs_num (optional)int10The epoch number of distilling.report_steps (optional)int100Report the training process every [report_steps] steps.warmup (optional)float0.1The warmup rate for training.dev_speed (optional)float0.5The speed for evaluating in the self-distilling process.model_saving_path (optional)str'./model.bin'The path to saving model.Args ofFastBERT_S2.forward():ArgsTypeExamplesExplanationsents_astr'How are you'The input A-sentence.sents_bstr'How are you'The input B-sentence.speed (optional)float0.5The speed value for inference. Default 0.0.