Pre-train BERT with HF Trainer

Hey, I want to conduct my own BERT pre-training. After reading other stuff about this topic, I found that I would have to use BertForPretraining, TextDatasetForNextSentencePrediction and the DataCollatorForLanguageModeling. But my minimal code does not work. What am I doing wrong?

I also made this notebook so you can navigate through my code example:

Blockquote
RuntimeError: The size of tensor a (537) must match the size of tensor b (512) at non-singleton dimension 1

I guess the error means that somewhere there is a too long token sequence that is too long for the set BERT maximum of 512. But I dont understand why. I guess the 537 length does not come from a single sentence, it has to do something with the TextDatasetForNextSentencePrediction. “Examples” were calculated there for the documents of the corpus (create_examples_from_document), but I dont see why this leads to this tensor size …

from transformers import BertConfig, BertTokenizer, BertForPreTraining
from transformers import DataCollatorForLanguageModeling
from transformers.data.datasets.language_modeling import TextDatasetForNextSentencePrediction
from transformers import Trainer, TrainingArguments
import transformers
transformers.set_seed(1337)

model = BertForPreTraining(BertConfig())
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

dataset = TextDatasetForNextSentencePrediction(
    tokenizer=tokenizer,
    file_path="test.txt",
    block_size=512,
    overwrite_cache=False,
    short_seq_probability=0.1,
    nsp_probability=0.5
)

data_collator = DataCollatorForLanguageModeling(
    tokenizer=tokenizer,
    mlm=True
)

args = TrainingArguments(
    output_dir="./trainer_results",
    overwrite_output_dir=False,
    num_train_epochs=10,
    per_device_train_batch_size=32,
    save_steps=100,
    save_total_limit=3,
    seed=1337,
)
trainer = Trainer(
    model=model,
    args=args,
    data_collator=data_collator,
    train_dataset=dataset,
    tokenizer=tokenizer
)
trainer.train()

test.txt:

Yangliuqing is a market town in Xiqing District, in the western suburbs of Tianjin, People's Republic of China.
Despite its relatively small size, it has been named since 2006 in the "famous historical and cultural market towns in China".
It is best known in China for creating nianhua or Yangliuqing nianhua.
For more than 400 years, Yangliuqing has in effect specialised in the creation of these woodcuts for the New Year.
wood block prints using vivid colourschemes to portray traditional scenes of children's games often interwoven with auspiciouse objects.
, it had 27 residential communities and 25 villages under its administration.
Shi Family Grand Courtyard Shi Family Grand Courtyard (Tiānjīn Shí Jiā Dà Yuàn, 天津石家大院) is situated in Yangliuqing Town of Xiqing District, which is the former residence of wealthy merchant Shi Yuanshi - the 4th son of Shi Wancheng, one of the eight great masters in Tianjin.
First built in 1875, it covers over 6,000 square meters, including large and small yards and over 200 folk houses, a theater and over 275 rooms that served as apartments and places of business and worship for this powerful family.
Shifu Garden, which finished its expansion in October 2003, covers 1,200 square meters, incorporates the elegance of imperial garden and delicacy of south garden.
Now the courtyard of Shi family covers about 10,000 square meters, which is called the first mansion in North China.
Now it serves as the folk custom museum in Yangliuqing, which has a large collection of folk custom museum in Yanliuqing, which has a large collection of folk art pieces like Yanliuqing New Year pictures, brick sculpture.
Shi's ancestor came from Dong'e County in Shandong Province, engaged in water transport of grain.
As the wealth gradually accumulated, the Shi Family moved to Yangliuqing and bought large tracts of land and set up their residence.
Shi Yuanshi came from the fourth generation of the family, who was a successful businessman and a good household manager, and the residence was thus enlarged for several times until it acquired the present scale.
It is believed to be the first mansion in the west of Tianjin.
The residence is symmetric based on the axis formed by a passageway in the middle, on which there are four archways.
On the east side of the courtyard, there are traditional single-story houses with rows of rooms around the four sides, which was once the living area for the Shi Family.
The rooms on north side were the accountants' office.
On the west are the major constructions including the family hall for worshipping Buddha, theater and the south reception room.
On both sides of the residence are side yard rooms for maids and servants.
Today, the Shi mansion, located in the township of Yangliuqing to the west of central Tianjin, stands as a surprisingly well-preserved monument to China's pre-revolution mercantile spirit.
It also serves as an on-location shoot for many of China's popular historical dramas.
Many of the rooms feature period furniture, paintings and calligraphy, and the extensive Shifu Garden.
Part of the complex has been turned into the Yangliuqing Museum, which includes displays focused on symbolic aspects of the courtyards' construction, local folk art and customs, and traditional period furnishings and crafts.
See also List of township-level divisions of Tianjin References in Tianjin

Orana Australia Ltd is a not-for-profit organisation that provides a diverse range of training and support services to over 650 people with disabilities and their families in South Australia.
History
The Mentally Retarded Children’s Society of SA Inc. was established in 1950 by a group of parents who wanted education, employment and accommodation opportunities for their children within the local community at a time when institutionalised care in Adelaide was their only alternative.
The society’s aims were to seek education or training facilities for people with intellectual disabilities, to establish sheltered workshops, and to establish residential hostels.
A number of sheltered workshops were established, and in 1980, the name was changed to the Aboriginal word "Orana", which means "Welcome".
Today, Orana provides assisted employment, assisted accommodation and respite services to people with intellectual disabilities.
Orana's current and previous clients include Mitsubishi Motors, Clipsal, RAA, Elders Limited, and Billycart Kids.
Orana was one of the first disability service organisations to achieve Quality Accreditation.
The services and products they offer Packaging Assembly Sewing Collating & Mailing Furniture - Retail Furniture – Manufacture for Commercial Market Worm Farming Work Crews Pet & Grain – Retail In 2018, after 65 years of bettering people’s lives, Orana identified a community need and expanded their operations into the aged care sector.
After the unveiling of the Australian Government’s Commonwealth Home Support Programme (CHSP) and seeing it as a natural step of progression, Orana now provides quality tailored aged care at home.
The well-resourced organization delivers help across a range of areas, helping the elderly remain where they want to be - in the comfort of their own home during their later years.
Orana continues with its mission to support people remain independent, valued and productive members of the community.
References External links organisations based in Australia based in South Australia

short