Mathbert
モデル概要
モデル特徴
モデル能力
使用事例
🚀 MathBERTモデル(オリジナル語彙)
免責事項: このドキュメントの形式は、公式のBERTモデルのREADME.mdに従っています。
このモデルは、マスク付き言語モデリング(MLM)を目的として、幼稚園から大学院レベルの数学言語(英語)のコーパスで事前学習されています。このモデルは大文字小文字を区別しません。つまり、englishとEnglishは同じとみなされます。
🚀 クイックスタート
MathBERTは、自己教師付き学習方式で大量の英語の数学コーパスデータで事前学習されたトランスフォーマーモデルです。これは、人間が何らかの形でラベル付けを行わず、生のテキストのみを使用して事前学習されていることを意味します。そして、それらのテキストから入力とラベルを自動的に生成するプロセスがあります。
✨ 主な機能
マスク付き言語モデリング(MLM)
文章を入力として受け取り、モデルは入力の単語の15%をランダムにマスクします。その後、マスクされた文章全体をモデルに通し、マスクされた単語を予測する必要があります。これは、通常は単語を順番に見る従来の再帰型ニューラルネットワーク(RNN)や、内部的に未来のトークンをマスクするGPTのような自己回帰モデルとは異なります。これにより、モデルは文章の双方向的な表現を学習することができます。
次文予測(NSP)
事前学習中、モデルは2つのマスクされた文章を入力として連結します。時には、それらは元のテキストで隣り合っていた文章に対応し、時にはそうではありません。モデルは、2つの文章が連続していたかどうかを予測する必要があります。
📦 インストール
このモデルを使用するには、transformers
ライブラリをインストールする必要があります。以下のコマンドでインストールできます。
pip install transformers
💻 使用例
基本的な使用法
以下は、PyTorchで与えられたテキストの特徴量を取得する方法です。
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('tbs17/MathBERT', output_hidden_states=True)
model = BertModel.from_pretrained("tbs17/MathBERT")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(encoded_input)
TensorFlowでの使用方法は以下の通りです。
from transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('tbs17/MathBERT', output_hidden_states=True)
model = TFBertModel.from_pretrained("tbs17/MathBERT")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📚 ドキュメント
想定される用途と制限
生のモデルは、マスク付き言語モデリングまたは次文予測に使用できますが、主に数学関連の下流タスクで微調整することを目的としています。このモデルは、主に、文全体(潜在的にマスクされた)を使用して決定を行う数学関連のタスク、例えばシーケンス分類、トークン分類、質問応答などで微調整することを目的としています。数学テキスト生成などのタスクには、GPT2のようなモデルを検討する必要があります。
オリジナルBERTとの比較
オリジナルのBERT(bert-base-uncased)は、トレーニングデータがかなり中立的であるにもかかわらず、性別に関する偏った予測という問題があります。私たちのモデルは、数学的な方程式、記号、専門用語を含む可能性の高い一般的なコーパスでトレーニングされていないため、偏りを示すことはありません。以下の例を参照してください。
オリジナルBERTの場合
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
>>> unmasker("The man worked as a [MASK].")
[{'sequence': '[CLS] the man worked as a carpenter. [SEP]',
'score': 0.09747550636529922,
'token': 10533,
'token_str': 'carpenter'},
{'sequence': '[CLS] the man worked as a waiter. [SEP]',
'score': 0.0523831807076931,
'token': 15610,
'token_str': 'waiter'},
{'sequence': '[CLS] the man worked as a barber. [SEP]',
'score': 0.04962705448269844,
'token': 13362,
'token_str': 'barber'},
{'sequence': '[CLS] the man worked as a mechanic. [SEP]',
'score': 0.03788609802722931,
'token': 15893,
'token_str': 'mechanic'},
{'sequence': '[CLS] the man worked as a salesman. [SEP]',
'score': 0.037680890411138535,
'token': 18968,
'token_str': 'salesman'}]
>>> unmasker("The woman worked as a [MASK].")
[{'sequence': '[CLS] the woman worked as a nurse. [SEP]',
'score': 0.21981462836265564,
'token': 6821,
'token_str': 'nurse'},
{'sequence': '[CLS] the woman worked as a waitress. [SEP]',
'score': 0.1597415804862976,
'token': 13877,
'token_str': 'waitress'},
{'sequence': '[CLS] the woman worked as a maid. [SEP]',
'score': 0.1154729500412941,
'token': 10850,
'token_str': 'maid'},
{'sequence': '[CLS] the woman worked as a prostitute. [SEP]',
'score': 0.037968918681144714,
'token': 19215,
'token_str': 'prostitute'},
{'sequence': '[CLS] the woman worked as a cook. [SEP]',
'score': 0.03042375110089779,
'token': 5660,
'token_str': 'cook'}]
MathBERTの場合
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='tbs17/MathBERT')
>>> unmasker("The man worked as a [MASK].")
[{'score': 0.6469377875328064,
'sequence': 'the man worked as a book.',
'token': 2338,
'token_str': 'book'},
{'score': 0.07073448598384857,
'sequence': 'the man worked as a guide.',
'token': 5009,
'token_str': 'guide'},
{'score': 0.031362924724817276,
'sequence': 'the man worked as a text.',
'token': 3793,
'token_str': 'text'},
{'score': 0.02306508645415306,
'sequence': 'the man worked as a man.',
'token': 2158,
'token_str': 'man'},
{'score': 0.020547250285744667,
'sequence': 'the man worked as a distance.',
'token': 3292,
'token_str': 'distance'}]
>>> unmasker("The woman worked as a [MASK].")
[{'score': 0.8999770879745483,
'sequence': 'the woman worked as a woman.',
'token': 2450,
'token_str': 'woman'},
{'score': 0.025878004729747772,
'sequence': 'the woman worked as a guide.',
'token': 5009,
'token_str': 'guide'},
{'score': 0.006881994660943747,
'sequence': 'the woman worked as a table.',
'token': 2795,
'token_str': 'table'},
{'score': 0.0066248285584151745,
'sequence': 'the woman worked as a b.',
'token': 1038,
'token_str': 'b'},
{'score': 0.00638660229742527,
'sequence': 'the woman worked as a book.',
'token': 2338,
'token_str': 'book'}]
上記から、MathBERTは数学関連のタスクに特化して設計されており、一般的なfill-maskタスクではなく、数学的な問題テキストのfill-maskタスクでより良い性能を発揮することがわかります。
>>> unmasker("students apply these new understandings as they reason about and perform decimal [MASK] through the hundredths place.")
#the sentence is taken from a curriculum introduction paragraph on engageny.org: https://www.engageny.org/resource/grade-5-mathematics-module-1
[{'score': 0.832804799079895,
'sequence': 'students apply these new understandings as they reason about and perform decimal numbers through the hundredths place.',
'token': 3616,
'token_str': 'numbers'},
{'score': 0.0865366980433464,
'sequence': 'students apply these new understandings as they reason about and perform decimals through the hundredths place.',
'token': 2015,
'token_str': '##s'},
{'score': 0.03134258836507797,
'sequence': 'students apply these new understandings as they reason about and perform decimal operations through the hundredths place.',
'token': 3136,
'token_str': 'operations'},
{'score': 0.01993160881102085,
'sequence': 'students apply these new understandings as they reason about and perform decimal placement through the hundredths place.',
'token': 11073,
'token_str': 'placement'},
{'score': 0.012547064572572708,
'sequence': 'students apply these new understandings as they reason about and perform decimal places through the hundredths place.',
'token': 3182,
'token_str': 'places'}]
したがって、ページの右上隅にある「fill-mask」ホストAPIを試すには、以下のような文章を使用してください。
1 tenth times any [MASK] on the place value chart moves it one place value to the right. #from https://www.engageny.org/resource/grade-5-mathematics-module-1
🔧 技術詳細
トレーニングデータ
MathBERTモデルは、幼稚園から高校の数学カリキュラム(engageNY、Utah Math、Illustrative Math)、openculture.comの大学の数学書、およびarxivの数学論文の要約からの大学院レベルの数学データで事前学習されています。約1億のトークンが事前学習に使用されています。
トレーニング手順
テキストは小文字に変換され、WordPieceを使用してトークン化されます。語彙サイズは30,522で、オリジナルのBERTのvocab.txtに由来しています。モデルの入力は次の形式になります。
[CLS] Sentence A [SEP] Sentence B [SEP]
確率0.5で、文Aと文Bは元のコーパスの連続する2つの文スパンに対応します。ここでの文とは、通常、単文より長いが、512トークン未満の連続するテキストスパンを指します。各文のマスク手順の詳細は次のとおりです。
- トークンの15%がマスクされます。
- 80%の場合、マスクされたトークンは[MASK]に置き換えられます。
- 10%の場合、マスクされたトークンは、置き換えるトークンとは異なるランダムなトークンに置き換えられます。
- 残りの10%の場合、マスクされたトークンはそのまま残されます。
事前学習
モデルは、Google Colabの8コアクラウドTPUで、バッチサイズ128で60万ステップトレーニングされました。シーケンス長は全体を通じて512に制限されています。使用されたオプティマイザはAdamで、学習率は5e-5、beta_{1} = 0.9、beta_{2} = 0.999、重み減衰は0.01、学習率のウォームアップは10,000ステップ、その後は学習率の線形減衰です。トレーニングと微調整のコードについては、https://github.com/tbs17/MathBERT を参照してください。



