Mathbert Custom
模型概述
模型特點
模型能力
使用案例
🚀 MathBERT模型(自定義詞表)
MathBERT是一個預訓練模型,它基於從幼兒園到研究生階段的數學語言(英語)數據,採用掩碼語言模型(MLM)目標進行預訓練。該模型不區分大小寫,例如“english”和“English”對它來說是一樣的。
✨ 主要特性
MathBERT是一個以自監督方式在大量英語數學語料庫數據上進行預訓練的Transformer模型。它僅在原始文本上進行預訓練,沒有人工進行任何標註,而是通過自動流程從這些文本中生成輸入和標籤。具體而言,它通過兩個目標進行預訓練:
- 掩碼語言模型(MLM):給定一個句子,模型會隨機掩蓋輸入中15%的單詞,然後將整個掩碼後的句子輸入模型,讓模型預測被掩蓋的單詞。這與傳統的循環神經網絡(RNN)不同,RNN通常是逐個處理單詞;也與像GPT這樣的自迴歸模型不同,GPT會在內部掩蓋未來的標記。這種方式使模型能夠學習句子的雙向表示。
- 下一句預測(NSP):在預訓練期間,模型將兩個掩碼後的句子作為輸入進行拼接。有時這兩個句子在原始文本中是相鄰的,有時則不是。模型需要預測這兩個句子是否相鄰。
通過這種方式,模型學習到數學語言的內部表示,可用於提取對下游任務有用的特徵。例如,如果有一個帶標籤的句子數據集,可以使用MathBERT模型生成的特徵作為輸入來訓練一個標準分類器。
📦 安裝指南
文檔未提供安裝步驟,跳過該章節。
💻 使用示例
基礎用法
以下是如何在PyTorch中使用該模型獲取給定文本特徵的示例:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('tbs17/MathBERT-custom')
model = BertModel.from_pretrained("tbs17/MathBERT-custom")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')["input_ids"]
output = model(encoded_input)
高級用法
以下是在TensorFlow中使用該模型獲取給定文本特徵的示例:
from transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('tbs17/MathBERT-custom')
model = TFBertModel.from_pretrained("tbs17/MathBERT-custom")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📚 詳細文檔
預期用途和侷限性
可以使用原始模型進行掩碼語言建模或下一句預測,但它主要用於在與數學相關的下游任務上進行微調。
需要注意的是,該模型主要旨在針對使用整個句子(可能是掩碼後的)進行決策的數學相關任務進行微調,例如序列分類、標記分類或問答任務。對於數學文本生成等任務,建議使用像GPT2這樣的模型。
警告
MathBERT是專門為數學相關任務設計的,在數學問題文本的掩碼填充任務中表現更好,而不是通用的掩碼填充任務。以下是示例:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='tbs17/MathBERT')
# 以下是期望的使用方式
>>> unmasker("students apply these new understandings as they reason about and perform decimal [MASK] through the hundredths place.")
[{'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'}]
# 以下不是期望的使用方式
>>> 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'}]
🔧 技術細節
訓練數據
該BERT模型在從幼兒園到高中的數學課程(如engageNY、猶他州數學、示例數學)、openculture.com上的大學數學書籍以及arXiv數學論文摘要中的研究生水平數學數據上進行預訓練。大約有1億個標記參與了預訓練。
訓練過程
文本先轉換為小寫,然後使用WordPiece和自定義的30,522大小的詞表進行分詞。使用huggingface分詞器庫中的bert_tokenizer
從訓練的原始數學文本中生成自定義詞表文件。模型的輸入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
有50%的概率,句子A和句子B對應原始語料庫中的兩個連續句子,在其他情況下,句子B是語料庫中的另一個隨機句子。這裡的“句子”通常是一段連續的文本,長度通常大於單個句子。唯一的限制是兩個“句子”組合後的長度小於512個標記。
每個句子的掩碼過程細節如下:
- 15%的標記被掩碼。
- 80%的情況下,被掩碼的標記被替換為[MASK]。
- 10%的情況下,被掩碼的標記被替換為一個與原標記不同的隨機標記。
- 10%的情況下,被掩碼的標記保持不變。
預訓練
該模型在Google Colab的8核雲TPU上進行了600k步的訓練,批次大小為128。整個訓練過程中序列長度限制為512。使用的優化器是Adam,學習率為5e - 5,beta₁ = 0.9,beta₂ = 0.999,權重衰減為0.01,學習率在10,000步內進行熱身,之後線性衰減。
📄 許可證
文檔未提供許可證信息,跳過該章節。



