From 5e17cc3287842e45466983f25806721ba828abdb Mon Sep 17 00:00:00 2001 From: InconsolableCellist Date: Thu, 2 Jan 2020 23:34:50 -0500 Subject: [PATCH 1/2] Fixing #153, and try catch Stripping of the prefix wasn't occurring when the generated text didn't contain the truncate text (which seems to happen quite often). Also put in a try/catch for writing, and replacing a troublesome Unicode character. (cherry picked from commit 5b8eb7bbf9b7c33e660802ac82c8faf73fb9da21) --- gpt_2_simple/gpt_2.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/gpt_2_simple/gpt_2.py b/gpt_2_simple/gpt_2.py index aa31a69..e332adb 100644 --- a/gpt_2_simple/gpt_2.py +++ b/gpt_2_simple/gpt_2.py @@ -298,12 +298,15 @@ def generate_samples(): index + 1, text) all_text.append(text) index += 1 - print(text) + print(text.replace(u'\xa0', u' ')) maketree(os.path.join(SAMPLE_DIR, run_name)) with open( os.path.join(SAMPLE_DIR, run_name, 'samples-{}').format(counter), 'w') as fp: - fp.write('\n'.join(all_text)) + try: + fp.write('\n'.join(all_text)) + except Exception as e: + pass def sample_batch(): return [data_sampler.sample(1024) for _ in range(batch_size)] @@ -473,25 +476,42 @@ def generate(sess, for i in range(batch_size): generated += 1 gen_text = enc.decode(out[i]) + # process the text to strip out the prefix and/or end at the truncate text if prefix: gen_text = enc.decode(context_tokens[:1]) + gen_text if truncate: truncate_esc = re.escape(truncate) if prefix and not include_prefix: prefix_esc = re.escape(prefix) - pattern = '(?:{})(.*?)(?:{})'.format(prefix_esc, - truncate_esc) + pattern = '(?:{})(.*?)(?:{})'.format(prefix_esc, truncate_esc) else: pattern = '(.*?)(?:{})'.format(truncate_esc) trunc_text = re.search(pattern, gen_text, re.S) if trunc_text: gen_text = trunc_text.group(1) - gen_text = gen_text.lstrip('\n') - if destination_path: - f.write("{}\n{}".format(gen_text, sample_delim)) - if not return_as_list and not destination_path: - print("{}\n{}".format(gen_text, sample_delim), end='') + # the generated text sometimes doesn't include the truncate text, but we can try again to just strip out the prefix (if it exists), leaving only the new gen_text + elif not trunc_text and prefix: + pattern = '(?:{})(.*)'.format(prefix_esc) + trunc_text = re.search(pattern, gen_text, re.S) + if trunc_text: + gen_text = trunc_text.group(1) + + # truncate is not set, but we should still strip the prefix, if set and requested + else: + if prefix and not include_prefix: + prefix_esc = re.escape(prefix) + pattern = '(?:{})(.*)'.format(prefix_esc) + trunc_text = re.search(pattern, gen_text, re.S) + if trunc_text: + gen_text = trunc_text.group(1) + try: + if destination_path: + f.write("{}\n{}".format(gen_text, sample_delim)) + if not return_as_list and not destination_path: + print("{}\n{}".format(gen_text, sample_delim), end='') + except Exception as e: + pass gen_texts.append(gen_text) if destination_path: From c4d23bac6b05724bc9f14665acf4edc0f5384a1f Mon Sep 17 00:00:00 2001 From: InconsolableCellist Date: Thu, 2 Jan 2020 23:44:48 -0500 Subject: [PATCH 2/2] Removing unrelated code for the #153 fix Removed try/catch and string replacement for a Unicode ' ' (Unicode issues in my terminal) --- gpt_2_simple/gpt_2.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gpt_2_simple/gpt_2.py b/gpt_2_simple/gpt_2.py index e332adb..958dec8 100644 --- a/gpt_2_simple/gpt_2.py +++ b/gpt_2_simple/gpt_2.py @@ -298,15 +298,12 @@ def generate_samples(): index + 1, text) all_text.append(text) index += 1 - print(text.replace(u'\xa0', u' ')) + print(text) maketree(os.path.join(SAMPLE_DIR, run_name)) with open( os.path.join(SAMPLE_DIR, run_name, 'samples-{}').format(counter), 'w') as fp: - try: fp.write('\n'.join(all_text)) - except Exception as e: - pass def sample_batch(): return [data_sampler.sample(1024) for _ in range(batch_size)]