From 9ba30829828a5aa11eb29c6937be685b3ff81e90 Mon Sep 17 00:00:00 2001 From: Keifer Date: Wed, 9 Dec 2020 21:55:58 +0800 Subject: [PATCH] Patched Transformers model Remove device assignment, not needed --- demo_4.ipynb | 370 ++++++++++++++++++++++++++++++++++++++---- src/core/qa.py | 6 +- src/core/sentiment.py | 8 +- src/core/summarize.py | 6 +- src/core/text_gen.py | 24 +-- src/core/translate.py | 11 +- 6 files changed, 356 insertions(+), 69 deletions(-) diff --git a/demo_4.ipynb b/demo_4.ipynb index 3e2dc57..0b32556 100644 --- a/demo_4.ipynb +++ b/demo_4.ipynb @@ -76,9 +76,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Some weights of GPT2LMHeadModel were not initialized from the model checkpoint at distilgpt2 and are newly initialized: ['transformer.h.0.attn.masked_bias', 'transformer.h.1.attn.masked_bias', 'transformer.h.2.attn.masked_bias', 'transformer.h.3.attn.masked_bias', 'transformer.h.4.attn.masked_bias', 'transformer.h.5.attn.masked_bias']\n", + "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n" + ] + } + ], "source": [ "#Initialization\n", "textgen = TGL(name=\"Text Generator\",max_length=16,num_return_sequences=3)" @@ -86,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -100,12 +109,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "[{'generated_text': 'Let me say as an atheist, I do believe that our religion '\n", - " 'is a real'},\n", - " {'generated_text': \"Let me say this: in the last week I've found myself at \"\n", - " 'the bottom'},\n", - " {'generated_text': 'Let me say thank you one more time – because of God we '\n", - " 'will not be'}]\n" + "[{'generated_text': \"Let me say I'm not stupid to take a stance on the merits \"\n", + " 'of your'},\n", + " {'generated_text': 'Let me say on the first day that I will no longer be '\n", + " 'buying. I'},\n", + " {'generated_text': 'Let me say, The Man Who Sold Us a Dollar With A Lotion.'}]\n" ] } ], @@ -133,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "scrolled": true }, @@ -145,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -161,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -170,7 +178,7 @@ "text": [ "{'answer': 'the task of extracting an answer from a text given a question.',\n", " 'end': 96,\n", - " 'score': 0.6185597764655871,\n", + " 'score': 0.618559613535047,\n", " 'start': 34}\n" ] } @@ -190,15 +198,97 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": {}, "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "19bab025de9548de90fb93cd0b4484a5", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=1343.0, style=ProgressStyle(description…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "76cff7cf39b24c10abe3dfc4a2705615", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=898823.0, style=ProgressStyle(descripti…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b8be13f96ff341eb9d55293e0cd0ece8", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=456318.0, style=ProgressStyle(descripti…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "611c23649ada4a758247979e0189ac8c", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=1625270765.0, style=ProgressStyle(descr…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, { "name": "stderr", "output_type": "stream", "text": [ - "/home/keifer/anaconda3/envs/1stdaykit/lib/python3.8/site-packages/transformers/modeling_auto.py:796: FutureWarning: The class `AutoModelWithLMHead` is deprecated and will be removed in a future version. Please use `AutoModelForCausalLM` for causal language models, `AutoModelForMaskedLM` for masked language models and `AutoModelForSeq2SeqLM` for encoder-decoder models.\n", - " warnings.warn(\n", "Some weights of BartForConditionalGeneration were not initialized from the model checkpoint at facebook/bart-large-cnn and are newly initialized: ['final_logits_bias']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n" ] @@ -211,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -319,9 +409,94 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "3aa82ff6ba4940da94f6e769576c5390", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=629.0, style=ProgressStyle(description_…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "6df48cf0046f4ed6a2f8bceb4a942ca7", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=231508.0, style=ProgressStyle(descripti…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e2bacd06d4944208b6db4440c3657f92", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=230.0, style=ProgressStyle(description_…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "5206c0120a9f42cc887267de62d04cba", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=267844284.0, style=ProgressStyle(descri…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ "#Initialize\n", "ST = SentimentAnalyzer()" @@ -329,7 +504,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -337,13 +512,13 @@ "main_text_to_analyze = [\"The food is not too hot, which makes it just right.\",\n", " \"The weather is not looking too good today\",\n", " \"The sky is looking a bit gloomy, time to catch a nap!\",\n", - " \"War is what it is\",\n", + " \"This tastes good :D\",\n", " \"Superheroes are mere child fantasies\"]" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -359,10 +534,10 @@ "{'Confidence': 0.9982035756111145,\n", " 'Raw Text': 'The sky is looking a bit gloomy, time to catch a nap!',\n", " 'Sentiment': 'NEGATIVE'}\n", - "{'Confidence': 0.9993534684181213,\n", - " 'Raw Text': 'War is what it is',\n", - " 'Sentiment': 'NEGATIVE'}\n", - "{'Confidence': 0.8927153944969177,\n", + "{'Confidence': 0.9998365640640259,\n", + " 'Raw Text': 'This tastes good :D',\n", + " 'Sentiment': 'POSITIVE'}\n", + "{'Confidence': 0.8927154541015625,\n", " 'Raw Text': 'Superheroes are mere child fantasies',\n", " 'Sentiment': 'NEGATIVE'}\n" ] @@ -397,9 +572,138 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "225ea3a423194dabaa99354156197300", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=779155.0, style=ProgressStyle(descripti…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "8a32331916634e4e89b9f23da96b6127", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=799001.0, style=ProgressStyle(descripti…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "fd9e53c28f414152a1785d9e014780b7", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=1457360.0, style=ProgressStyle(descript…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "140b60ba1f0c4328ba561ff2f4759765", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=265.0, style=ProgressStyle(description_…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "2f27c589f02c4a209ae278335d98cf54", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=1113.0, style=ProgressStyle(description…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "6e5ae4774e524a62bcfa810b7d54c250", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, description='Downloading', max=312087009.0, style=ProgressStyle(descri…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ "#Initialize\n", "Trans = Translator_M(task='Helsinki-NLP/opus-mt-en-ROMANCE')" @@ -407,7 +711,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -419,7 +723,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -430,7 +734,7 @@ " 'Y esto al español']" ] }, - "execution_count": 8, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -443,7 +747,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 16, "metadata": {}, "outputs": [ { diff --git a/src/core/qa.py b/src/core/qa.py index b98cb77..cdfb1dc 100644 --- a/src/core/qa.py +++ b/src/core/qa.py @@ -11,18 +11,16 @@ class QuesAns(BaseClass): - def __init__(self, name='BERT QuesAns', device="cpu"): + def __init__(self, name='BERT QuesAns'): super().__init__(name) #Init name and metadata self.name = name - self.device = 1 if device.lower() == "cpu" else -1 #Create net. Commented out; seems like default works, other models/tokenizer will have errors. # self.tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad") # self.model = AutoModelWithLMHead.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad") - self.predictor = pipeline('question-answering', - device = self.device) + self.predictor = pipeline('question-answering') diff --git a/src/core/sentiment.py b/src/core/sentiment.py index e2412b2..d4de646 100644 --- a/src/core/sentiment.py +++ b/src/core/sentiment.py @@ -11,20 +11,16 @@ class SentimentAnalyzer(BaseClass): - def __init__(self, name='Sentiment Analyzer', device="cpu"): #alt: distilbert-base-uncased-finetuned-sst-2-english + def __init__(self, name='Sentiment Analyzer'): #alt: distilbert-base-uncased-finetuned-sst-2-english super().__init__(name) #Init name and metadata self.name = name - self.device = 1 if device.lower() == "cpu" else -1 #Create net # self.tokenizer = AutoTokenizer.from_pretrained("gpt2") # self.model = AutoModelWithLMHead.from_pretrained("gpt2") - self.predictor = pipeline('sentiment-analysis', - # model = self.model, - # tokenizer = self.tokenizer, - device = self.device) + self.predictor = pipeline('sentiment-analysis') diff --git a/src/core/summarize.py b/src/core/summarize.py index 61ca117..127d2b4 100644 --- a/src/core/summarize.py +++ b/src/core/summarize.py @@ -11,12 +11,11 @@ class Summarizer(BaseClass): - def __init__(self, name='BART Summarizer',max_length=130,min_length=30, device="cpu"): + def __init__(self, name='BART Summarizer',max_length=130,min_length=30): super().__init__(name) #Init name and metadata self.name = name - self.device = 1 if device.lower() == "cpu" else -1 self.max_length = max_length self.min_length = min_length @@ -25,8 +24,7 @@ def __init__(self, name='BART Summarizer',max_length=130,min_length=30, device=" self.model = AutoModelWithLMHead.from_pretrained("facebook/bart-large-cnn") self.predictor = pipeline('summarization', model = self.model, - tokenizer = self.tokenizer, - device = self.device) + tokenizer = self.tokenizer) diff --git a/src/core/text_gen.py b/src/core/text_gen.py index b66d2b8..7005d40 100644 --- a/src/core/text_gen.py +++ b/src/core/text_gen.py @@ -11,12 +11,11 @@ class _TextGen(BaseClass): - def __init__(self, name='GPT-2 TextGen Base Class', device="cpu"): + def __init__(self, name='GPT-2 TextGen Base Class'): super().__init__(name) #Init name and metadata self.name = name - self.device = 1 if device.lower() == "cpu" else -1 @@ -62,7 +61,7 @@ def visualize(self,text): class TextGen_Base(_TextGen): - def __init__(self, name='GPT-2 TextGen',max_length=12,num_return_sequences=3): + def __init__(self,name,max_length=12,num_return_sequences=3): super().__init__(name) #Init name and metadata @@ -75,20 +74,18 @@ def __init__(self, name='GPT-2 TextGen',max_length=12,num_return_sequences=3): self.model = AutoModelWithLMHead.from_pretrained("gpt2") self.predictor = pipeline('text-generation', model = self.model, - tokenizer = self.tokenizer, - device = self.device) + tokenizer = self.tokenizer) class TextGen_Large(_TextGen): - def __init__(self, name='GPT-2 Large TextGen',max_length=12,num_return_sequences=3): + def __init__(self, name,max_length=12,num_return_sequences=3): super().__init__(name) #Init name and metadata self.name = name self.max_length = max_length - self.device = 1 if torch.cuda.is_available() else -1 self.num_return_sequences=num_return_sequences @@ -97,15 +94,14 @@ def __init__(self, name='GPT-2 Large TextGen',max_length=12,num_return_sequences self.model = AutoModelWithLMHead.from_pretrained("gpt2-large") self.predictor = pipeline('text-generation', model = self.model, - tokenizer = self.tokenizer, - device = self.device) + tokenizer = self.tokenizer) class TextGen_XL(_TextGen): - def __init__(self, name='GPT-2 XL TextGen',max_length=12,num_return_sequences=3): + def __init__(self, name,max_length=12,num_return_sequences=3): super().__init__(name) #Init name and metadata @@ -119,14 +115,13 @@ def __init__(self, name='GPT-2 XL TextGen',max_length=12,num_return_sequences=3) self.model = AutoModelWithLMHead.from_pretrained("gpt2-XL") self.predictor = pipeline('text-generation', model = self.model, - tokenizer = self.tokenizer, - device = self.device) + tokenizer = self.tokenizer) class TextGen_Lite(_TextGen): - def __init__(self, name='GPT-2 Lite TextGen',max_length=12,num_return_sequences=3): + def __init__(self, name,max_length=12,num_return_sequences=3): super().__init__(name) #Init name and metadata @@ -139,5 +134,4 @@ def __init__(self, name='GPT-2 Lite TextGen',max_length=12,num_return_sequences= self.model = AutoModelWithLMHead.from_pretrained("distilgpt2") self.predictor = pipeline('text-generation', model = self.model, - tokenizer = self.tokenizer, - device = self.device) + tokenizer = self.tokenizer) diff --git a/src/core/translate.py b/src/core/translate.py index 687e856..06eab72 100644 --- a/src/core/translate.py +++ b/src/core/translate.py @@ -25,11 +25,10 @@ def __init__(self, name='MarianTransformer Translator',task='Helsinki-NLP/opus-m #Init name and metadata self.name = name self.task = task - self.device = 'cuda' if torch.cuda.is_available() else 'cpu' #Create net self.tokenizer = MarianTokenizer.from_pretrained(self.task) - self.predictor = MarianMTModel.from_pretrained(self.task).to(self.device) + self.predictor = MarianMTModel.from_pretrained(self.task) @@ -86,22 +85,20 @@ class Translator_T5(BaseClass): Find language-pair models at: https://huggingface.co/ """ - def __init__(self, name='T5 Translator', task='translation_en_to_de', device="cpu"): + def __init__(self, name='T5 Translator', task='translation_en_to_de'): super().__init__(name) #Init name and metadata self.name = name self.task = task - # self.device = 1 if torch.cuda.is_available() else -1 - self.device = -1 if device.lower() == "cpu" else 1 + #Create net self.tokenizer = AutoTokenizer.from_pretrained("t5-base") self.model = AutoModelWithLMHead.from_pretrained("t5-base") self.predictor = pipeline(self.task, model = self.model, - tokenizer = self.tokenizer, - device = self.device) + tokenizer = self.tokenizer)