Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gpt-2-server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Paul Geisler
gpt-2-server
Commits
ac5d5229
Commit
ac5d5229
authored
Aug 27, 2019
by
Jeff Wu
Browse files
Options
Downloads
Patches
Plain Diff
nucleus sampling
parent
f35fa1d9
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/generate_unconditional_samples.py
+2
-1
2 additions, 1 deletion
src/generate_unconditional_samples.py
src/interactive_conditional_samples.py
+2
-1
2 additions, 1 deletion
src/interactive_conditional_samples.py
src/sample.py
+20
-1
20 additions, 1 deletion
src/sample.py
with
24 additions
and
3 deletions
src/generate_unconditional_samples.py
+
2
−
1
View file @
ac5d5229
...
...
@@ -16,6 +16,7 @@ def sample_model(
length
=
None
,
temperature
=
1
,
top_k
=
0
,
top_p
=
1
,
models_dir
=
'
models
'
,
):
"""
...
...
@@ -58,7 +59,7 @@ def sample_model(
hparams
=
hparams
,
length
=
length
,
start_token
=
enc
.
encoder
[
'
<|endoftext|>
'
],
batch_size
=
batch_size
,
temperature
=
temperature
,
top_k
=
top_k
temperature
=
temperature
,
top_k
=
top_k
,
top_p
=
top_p
)[:,
1
:]
saver
=
tf
.
train
.
Saver
()
...
...
This diff is collapsed.
Click to expand it.
src/interactive_conditional_samples.py
+
2
−
1
View file @
ac5d5229
...
...
@@ -16,6 +16,7 @@ def interact_model(
length
=
None
,
temperature
=
1
,
top_k
=
0
,
top_p
=
1
,
models_dir
=
'
models
'
,
):
"""
...
...
@@ -61,7 +62,7 @@ def interact_model(
hparams
=
hparams
,
length
=
length
,
context
=
context
,
batch_size
=
batch_size
,
temperature
=
temperature
,
top_k
=
top_k
temperature
=
temperature
,
top_k
=
top_k
,
top_p
=
top_p
)
saver
=
tf
.
train
.
Saver
()
...
...
This diff is collapsed.
Click to expand it.
src/sample.py
+
20
−
1
View file @
ac5d5229
...
...
@@ -22,7 +22,25 @@ def top_k_logits(logits, k):
)
def
sample_sequence
(
*
,
hparams
,
length
,
start_token
=
None
,
batch_size
=
None
,
context
=
None
,
temperature
=
1
,
top_k
=
0
):
def
top_p_logits
(
logits
,
p
):
"""
Nucleus sampling
"""
batch
,
_
=
logits
.
shape
.
as_list
()
sorted_logits
=
tf
.
sort
(
logits
,
direction
=
'
DESCENDING
'
,
axis
=-
1
)
cumulative_probs
=
tf
.
cumsum
(
tf
.
nn
.
softmax
(
sorted_logits
,
axis
=-
1
),
axis
=-
1
)
indices
=
tf
.
stack
([
tf
.
range
(
0
,
batch
),
# number of indices to include
tf
.
maximum
(
tf
.
reduce_sum
(
tf
.
cast
(
cumulative_probs
<=
p
,
tf
.
int32
),
axis
=-
1
)
-
1
,
0
),
],
axis
=-
1
)
min_values
=
tf
.
gather_nd
(
sorted_logits
,
indices
)
return
tf
.
where
(
logits
<
min_values
,
tf
.
ones_like
(
logits
)
*
-
1e10
,
logits
,
)
def
sample_sequence
(
*
,
hparams
,
length
,
start_token
=
None
,
batch_size
=
None
,
context
=
None
,
temperature
=
1
,
top_k
=
0
,
top_p
=
1
):
if
start_token
is
None
:
assert
context
is
not
None
,
'
Specify exactly one of start_token and context!
'
else
:
...
...
@@ -45,6 +63,7 @@ def sample_sequence(*, hparams, length, start_token=None, batch_size=None, conte
next_outputs
=
step
(
hparams
,
prev
,
past
=
past
)
logits
=
next_outputs
[
'
logits
'
][:,
-
1
,
:]
/
tf
.
to_float
(
temperature
)
logits
=
top_k_logits
(
logits
,
k
=
top_k
)
logits
=
top_p_logits
(
logits
,
p
=
top_p
)
samples
=
tf
.
multinomial
(
logits
,
num_samples
=
1
,
output_dtype
=
tf
.
int32
)
return
[
next_outputs
[
'
presents
'
]
if
past
is
None
else
tf
.
concat
([
past
,
next_outputs
[
'
presents
'
]],
axis
=-
2
),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment