Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pre-training Language Models for Japanese

License

NotificationsYou must be signed in to change notification settings

retarfi/language-pretraining

Repository files navigation

GitHubGitHub release

This is a repository of pretrained Japanese transformer-based models.BERT, ELECTRA, RoBERTa, DeBERTa, and DeBERTaV2 is available.

Our pre-trained models are available in Transformers by Hugging Face:https://huggingface.co/izumi-lab.BERT-small, BERT-base, ELECTRA-small, ELECTRA-small-paper, and ELECTRA-base models trained by Wikipedia or financial dataset is available in this URL.

issue は日本語でも大丈夫です。

Table of Contents
  1. Usage
  2. Pre-trained Models
  3. Training Data
  4. Roadmap
  5. Citation
  6. Licenses
  7. Related Work
  8. Acknowledgements

Usage

Train Tokenizer

In our pretrained models, the texts are first tokenized byMeCab withIPAdic dictionary and then split into subwords by the WordPiece algorithm.

From v2.2.0,jptranstokenizer is required, which enables to use word tokenizers other than MeCab, such as Juman++, Sudachi, and spaCy LUW.

For subword tokenization,SentencePiece is also available for subword algorithm.

$ python train_tokenizer.py \--word_tokenizer mecab \--input_file corpus.txt \--model_dir tokenizer/ \--intermediate_dir ./data/corpus_split/ \--mecab_dic ipadic \--tokenizer_type wordpiece \--vocab_size 32768 \--min_frequency 2 \--limit_alphabet 2900 \--num_unused_tokens 10

You can see all the arguments withpython train_tokenizer.py --help

Create Dataset

You can train any type of corpus in Japanese.
When you train with another dataset, please add your corpus name with the line.
The output directory name is<dataset_type>_<max_length>_<input_corpus>.
In the following case, the output directory name isnsp_128_wiki-ja.
tokenizer_name_or_path will end with vocab.txt for wordpiece and with spiece.model for sentencepiece.

We show 2 examples to create dataset.

  • When you use your trained tokenizer:
$ python create_datasets.py \--input_corpus wiki-ja \--max_length 512 \--input_file corpus.txt \--mask_style bert \--tokenizer_name_or_path tokenizer/vocab.txt \--word_tokenizer_type mecab \--subword_tokenizer_type wordpiece \--mecab_dic ipadic
$ python create_datasets.py \--input_corpus wiki-ja \--max_length 512 \--input_file corpus.txt \--mask_style roberta-wwm \--tokenizer_name_or_path izumi-lab/bert-small-japanese \--load_from_hub

Training

Distributed training is available.For run command, please see thePyTorch document in detail.In official PyTorch implementation, different batch size between nodes is not available.We improved PyTorch sampling implementation (utils/trainer_pt_utils.py).

For example,bert-base-dist model is defined in parameter.json:

"bert-base-dist" : {    "number-of-layers" : 12,    "hidden-size" : 768,    "sequence-length" : 512,    "ffn-inner-hidden-size" : 3072,    "attention-heads" : 12,    "warmup-steps" : 10000,    "learning-rate" : 1e-4,    "batch-size" : {        "0" : 80,        "1" : 80,        "2" : 48,        "3" : 48    },    "train-steps" : 1000000,    "save-steps" : 50000,    "logging-steps" : 5000,    "fp16-type": 0,    "bf16": false}

In this case, node 0 and node 1 have 80 batch sizes and node 2 and node 3 have 48 respectively.If node 0 has 2 GPUs, each GPU have a 40 batch size.10G or higher network speed is recommended for training with multi-nodes.

fp16-type argument specifies which precision mode to use:

  • 0: FP32 training
  • 1: Mixed Precision
  • 2: "Almost FP16" Mixed Precision
  • 3: FP16 training

In detail, please seeNVIDIA Apex document.

bf16 argument determine whether bfloat16 is enabled or not.
You cannot usefp16-type (1, 2 or 3) andbf16 (true) simultaneously.

The whole word masking option is also available.

# Train with 1 node$ python run_pretraining.py \--dataset_dir ./datasets/nsp_128_wiki-ja/ \--model_dir ./model/bert/ \--parameter_file parameter.json \--model_type bert-small \--tokenizer_name_or_path tokenizer/vocab.txt \--word_tokenizer_type mecab \--subword_tokenizer_type wordpiece \--mecab_dic ipadic \(--use_deepspeed \)(--do_whole_word_mask \)(--do_continue)# Train with multi-node and multi-process$ NCCL_SOCKET_IFNAME=eno1 CUDA_VISIBLE_DEVICES=0,1 python -m torch.distributed.launch \--nproc_per_node=2 --nnodes=2 --node_rank=0 --master_addr="10.0.0.1" \--master_port=50916 run_pretraining.py \--dataset_dir ./datasets/nsp_128_wiki-ja/ \--model_dir ./model/bert/ \--parameter_file parameter.json \--model_type bert-small \--tokenizer_name_or_path tokenizer/vocab.txt \--word_tokenizer_type mecab \--subword_tokenizer_type wordpiece \--mecab_dic ipadic \(--use_deepspeed \)(--do_whole_word_mask \)(--do_continue)

Additional Pre-training

You can train models additionally with existing pre-trained model.
For example,bert-small-additional model is defined in parameter.json:

"bert-small-additional" : {    "pretrained_model_name_or_path" : "izumi-lab/bert-small-japanese",    "flozen-layers" : 6,    "warmup-steps" : 10000,    "learning-rate" : 5e-4,    "batch-size" : {        "-1" : 128    },    "train-steps" : 1450000,    "save-steps" : 100000,    "fp16-type": 0,    "bf16": false}

pretrained_model_name_or_path specifies a pretrained model in HuggingFace Hub or the path of a pretrained model.
flozen-layers specifies the flozen (not trained) layers of transformer.
When it is -1, train all layers (including embedding layer).
When it is 3, train upper (near output layer) 9 layers.

When you train ELECTRA model additionally, you need to specifypretrained_generator_model_name_or_path anddiscriminator_model_name_or_path instead ofpretrained_model_name_or_path.

$ python run_pretraining.py \--tokenizer_name_or_path izumi-lab/bert-small-japanese \--dataset_dir ./datasets/nsp_128_fin-ja/ \--model_dir ./model/bert/ \--parameter_file parameter.json \--model_type bert-small-additional

For ELECTRA

ELECTRA models generated by run_pretraining.py contain both generator and discriminator.For general use, separation is needed.

$ python extract_electra_model.py \--input_dir ./model/electra/checkpoint-1000000 \--output_dir ./model/electra/extracted-1000000 \--parameter_file parameter.json \--model_type electra-small \--generator \--discriminator

In this example, the generator model is saved in./model/electra/extracted-1000000/generator/ and discriminator model is saved in./model/electra/extracted-1000000/discriminator/ respectively.

Training Log

Tensorboard is available for the training log.

Pre-trained Models

Model Architecture

Following models are available now:

  • BERT
  • ELECTRA

The architecture of BERT-small, BERT-base, ELECTRA-small-paper, ELECTRA-base models are the same as those inthe original ELECTRA paper (ELECTRA-small-paper is described as ELECTRA-small in the paper).The architecture of ELECTRA-small is the same as that inthe ELECTRA implementation by Google.

ParameterBERT-smallBERT-baseELECTRA-smallELECTRA-small-paperELECTRA-base
Number of layers1212121212
Hidden Size256768256256768
Attention Heads4124412
Embedding Size128512128128128
Generator Size--1/11/41/3
Train Steps1.45M1M1M1M766k

Other models such as BERT-large or ELECTRA-large are also available in this implementation.You can also add your original parameters in parameter.json.

Training Data

Training data are aggregated to a text file.Each sentence is in one line and a blank line is inserted between documents.

Wikipedia Model

The normal models (not financial models) are trained on the Japanese version of Wikipedia, usingWikipedia dump file as of June 1, 2021.The corpus file is 2.9GB, consisting of approximately 20M sentences.

Financial Model

The financial models are trained on Wikipedia corpus and financial corpus.The Wikipedia corpus is the same as described above.The financial corpus consists of 2 corpora:

  • Summaries of financial results from October 9, 2012, to December 31, 2020
  • Securities reports from February 8, 2018, to December 31, 2020

The financial corpus file is 5.2GB, consisting of approximately 27M sentences.

Roadmap

See theopen issues for a full list of proposed features (and known issues).

Citation

@article{Suzuki-etal-2023-ipm,  title = {Constructing and analyzing domain-specific language model for financial text mining}  author = {Masahiro Suzuki and Hiroki Sakaji and Masanori Hirano and Kiyoshi Izumi},  journal = {Information Processing \& Management},  volume = {60},  number = {2},  pages = {103194},  year = {2023},  doi = {10.1016/j.ipm.2022.103194}}

Licenses

The pretrained models are distributed under the terms of theCreative Commons Attribution-ShareAlike 4.0.

The codes in this repository are distributed under MIT.

Related Work

Acknowledgements

This work was supported by JSPS KAKENHI Grant Number JP21K12010, JST-Mirai Program Grant Number JPMJMI20B1, and JST PRESTO Grand Number JPMJPR2267, Japan.

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp