Skip to content

Instantly share code, notes, and snippets.

@tteofili
Last active December 9, 2020 16:17
Show Gist options
  • Select an option

  • Save tteofili/d81e8480bf6e59d25e45dd1bb8f73449 to your computer and use it in GitHub Desktop.

Select an option

Save tteofili/d81e8480bf6e59d25e45dd1bb8f73449 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"import transformers"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from transformers import DistilBertTokenizerFast\n",
"from transformers import AutoModelForMaskedLM\n",
"tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')\n",
"model = AutoModelForMaskedLM.from_pretrained('distilbert-base-uncased')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"filename = 'rhs_titles.txt'\n",
"file1 = open(filename, 'r') \n",
"train_texts = file1.readlines() "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/tteofili/.local/lib/python3.6/site-packages/transformers/data/datasets/language_modeling.py:114: FutureWarning: This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets library. You can have a look at this example script for pointers: https://github.com/huggingface/transformers/blob/master/examples/language-modeling/run_mlm.py\n",
" FutureWarning,\n"
]
}
],
"source": [
"from transformers import LineByLineTextDataset\n",
"\n",
"dataset = LineByLineTextDataset(\n",
" tokenizer=tokenizer,\n",
" file_path=filename,\n",
" block_size=128,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from transformers import DataCollatorForLanguageModeling\n",
"\n",
"data_collator = DataCollatorForLanguageModeling(\n",
" tokenizer=tokenizer, mlm=True, mlm_probability=0.15\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from transformers import Trainer, TrainingArguments\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir=\"./rhsBERTo\",\n",
" overwrite_output_dir=True,\n",
" num_train_epochs=10,\n",
" per_device_train_batch_size=64,\n",
" save_steps=10_000,\n",
" save_total_limit=2,\n",
")\n",
"\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" data_collator=data_collator,\n",
" train_dataset=dataset,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 330,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future version. Using `--per_device_train_batch_size` is preferred.\n",
"Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future version. Using `--per_device_train_batch_size` is preferred.\n",
"Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future version. Using `--per_device_train_batch_size` is preferred.\n"
]
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" <style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" </style>\n",
" \n",
" <progress value='280' max='280' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [280/280 25:32, Epoch 10/10]\n",
" </div>\n",
" <table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>Step</th>\n",
" <th>Training Loss</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" </tbody>\n",
"</table><p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"TrainOutput(global_step=280, training_loss=2.441347394670759)"
]
},
"execution_count": 330,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": 331,
"metadata": {},
"outputs": [],
"source": [
"trainer.save_model(\"./rhsBERTo\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"from transformers import pipeline\n",
"\n",
"fill_mask = pipeline(\n",
" \"fill-mask\",\n",
" model=\"./rhsBERTo/\",\n",
" tokenizer=\"distilbert-base-uncased\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'sequence': '[CLS] ai : automation [SEP]',\n",
" 'score': 0.08630513399839401,\n",
" 'token': 19309,\n",
" 'token_str': 'automation'},\n",
" {'sequence': '[CLS] ai : security [SEP]',\n",
" 'score': 0.03631525859236717,\n",
" 'token': 3036,\n",
" 'token_str': 'security'},\n",
" {'sequence': '[CLS] ai : integration [SEP]',\n",
" 'score': 0.03197462111711502,\n",
" 'token': 8346,\n",
" 'token_str': 'integration'},\n",
" {'sequence': '[CLS] ai : introduction [SEP]',\n",
" 'score': 0.024633018299937248,\n",
" 'token': 4955,\n",
" 'token_str': 'introduction'},\n",
" {'sequence': '[CLS] ai : replay [SEP]',\n",
" 'score': 0.02159387432038784,\n",
" 'token': 15712,\n",
" 'token_str': 'replay'}]"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fill_mask(\"Red Hat: [MASK]\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"def generate(n, start):\n",
" text = start\n",
" for i in range(n):\n",
" result = fill_mask(text + ' [MASK]')\n",
" text = result[random.randint(0, len(result) - 1)]['sequence']\n",
" text = text.replace('[CLS]','')\n",
" text = text.replace('[SEP]','')\n",
" return text"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' red hat enterprise connect network infrastructures '"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate(5, 'red hat')"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' kogito storage manager suite 5 beta '"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate(5, 'kogito')"
]
},
{
"cell_type": "code",
"execution_count": 398,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' transformers : how to survive tomorrows! '"
]
},
"execution_count": 398,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate(4, 'transformers: how to ')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment