-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretraining_with_SMILES_tokenizer.py
57 lines (47 loc) · 1.48 KB
/
pretraining_with_SMILES_tokenizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from transformers import BertTokenizer, BertModel, BertForMaskedLM, BertConfig
from transformers import LineByLineTextDataset
from transformers import Trainer, TrainingArguments
from transformers import DataCollatorForLanguageModeling
VOCAB_SIZE = 1400
MAX_LEN = 1800
config = BertConfig(
vocab_size=MAX_LEN,
max_position_embeddings=MAX_LEN,
num_attention_heads=12,
num_hidden_layers=6,
type_vocab_size=1,
)
model = BertForMaskedLM(config=config)
tokenizer = BertTokenizer.from_pretrained('./tokenizers/BERT_Tokenizer_SMILES', max_len=MAX_LEN)
dataset_train = LineByLineTextDataset(
tokenizer=tokenizer,
file_path='./datasets/line_by_line_text_tataset_train',
block_size=MAX_LEN,
)
dataset_validation = LineByLineTextDataset(
tokenizer=tokenizer,
file_path='./datasets/line_by_line_text_dataset_evaluation',
block_size=MAX_LEN,
)
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm=True, mlm_probability=0.15
)
training_args = TrainingArguments(
output_dir="./models_output/SMILES_MLM/",
overwrite_output_dir=True,
num_train_epochs=100,
per_device_train_batch_size=2,
save_steps=5000,
do_eval=True,
evaluation_strategy='epoch'
)
trainer = Trainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=dataset_train,
eval_dataset=dataset_validation,
)
trainer.train()
trainer.save_model('./saved_models/pretrained_SMILES_15MLM_100Epochs')
print('Model saved!')