Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
9d94aecd
Unverified
Commit
9d94aecd
authored
Aug 13, 2020
by
Zhu Baohe
Committed by
GitHub
Aug 13, 2020
Browse files
Fix docs and bad word tokens generation_utils.py (#6387)
* fix * fix2 * fix3
parent
0ed7c00b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
6 deletions
+6
-6
src/transformers/generation_tf_utils.py
src/transformers/generation_tf_utils.py
+3
-3
src/transformers/generation_utils.py
src/transformers/generation_utils.py
+3
-3
No files found.
src/transformers/generation_tf_utils.py
View file @
9d94aecd
...
@@ -163,7 +163,7 @@ class TFGenerationMixin:
...
@@ -163,7 +163,7 @@ class TFGenerationMixin:
model = TFAutoModelWithLMHead.from_pretrained('distilgpt2') # Download model and configuration from S3 and cache.
model = TFAutoModelWithLMHead.from_pretrained('distilgpt2') # Download model and configuration from S3 and cache.
input_context = 'The dog'
input_context = 'The dog'
input_ids = tokenizer.encode(input_context, return_tensors='tf') # encode input context
input_ids = tokenizer.encode(input_context, return_tensors='tf') # encode input context
outputs = model.generate(input_ids=input_ids, max_length=40, temperature=0.7, num_return_sequences=3) #
3
generate
sequenc
es using
by
sampling
outputs = model.generate(input_ids=input_ids, max_length=40, temperature=0.7, num_return_sequences=3
, do_sample=True
) # generate
3 candidat
es using sampling
for i in range(3): # 3 output sequences were generated
for i in range(3): # 3 output sequences were generated
print('Generated {}: {}'.format(i, tokenizer.decode(outputs[i], skip_special_tokens=True)))
print('Generated {}: {}'.format(i, tokenizer.decode(outputs[i], skip_special_tokens=True)))
...
@@ -936,8 +936,8 @@ def calc_banned_bad_words_ids(prev_input_ids, bad_words_ids):
...
@@ -936,8 +936,8 @@ def calc_banned_bad_words_ids(prev_input_ids, bad_words_ids):
if
len
(
tokens
)
==
0
:
if
len
(
tokens
)
==
0
:
# if bad word tokens is just one token always ban it
# if bad word tokens is just one token always ban it
return
True
return
True
if
len
(
tokens
)
>
len
(
prev_
input_id
s
):
if
len
(
tokens
)
>
len
(
prev_
token
s
):
# if bad word tokens are longer th
e
n prev
input_id
s they can't be equal
# if bad word tokens are longer th
a
n prev
token
s they can't be equal
return
False
return
False
if
prev_tokens
[
-
len
(
tokens
)
:]
==
tokens
:
if
prev_tokens
[
-
len
(
tokens
)
:]
==
tokens
:
...
...
src/transformers/generation_utils.py
View file @
9d94aecd
...
@@ -226,7 +226,7 @@ class GenerationMixin:
...
@@ -226,7 +226,7 @@ class GenerationMixin:
model = AutoModelWithLMHead.from_pretrained('distilgpt2') # Download model and configuration from S3 and cache.
model = AutoModelWithLMHead.from_pretrained('distilgpt2') # Download model and configuration from S3 and cache.
input_context = 'The dog'
input_context = 'The dog'
input_ids = tokenizer.encode(input_context, return_tensors='pt') # encode input context
input_ids = tokenizer.encode(input_context, return_tensors='pt') # encode input context
outputs = model.generate(input_ids=input_ids, max_length=40, temperature=0.7, num_return_sequences=3) #
3
generate
sequenc
es using
by
sampling
outputs = model.generate(input_ids=input_ids, max_length=40, temperature=0.7, num_return_sequences=3
, do_sample=True
) # generate
3 candidat
es using sampling
for i in range(3): # 3 output sequences were generated
for i in range(3): # 3 output sequences were generated
print('Generated {}: {}'.format(i, tokenizer.decode(outputs[i], skip_special_tokens=True)))
print('Generated {}: {}'.format(i, tokenizer.decode(outputs[i], skip_special_tokens=True)))
...
@@ -876,8 +876,8 @@ def calc_banned_bad_words_ids(prev_input_ids: Iterable[int], bad_words_ids: Iter
...
@@ -876,8 +876,8 @@ def calc_banned_bad_words_ids(prev_input_ids: Iterable[int], bad_words_ids: Iter
if
len
(
tokens
)
==
0
:
if
len
(
tokens
)
==
0
:
# if bad word tokens is just one token always ban it
# if bad word tokens is just one token always ban it
return
True
return
True
if
len
(
tokens
)
>
len
(
prev_
input_id
s
):
if
len
(
tokens
)
>
len
(
prev_
token
s
):
# if bad word tokens are longer th
e
n prev
input_id
s they can't be equal
# if bad word tokens are longer th
a
n prev
token
s they can't be equal
return
False
return
False
if
prev_tokens
[
-
len
(
tokens
)
:]
==
tokens
:
if
prev_tokens
[
-
len
(
tokens
)
:]
==
tokens
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment