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
ada22a1c
"test/git@developer.sourcefind.cn:hehl2/torchaudio.git" did not exist on "68cc72da6383b97899033454313e62cd170963eb"
Unverified
Commit
ada22a1c
authored
Feb 18, 2019
by
Thomas Wolf
Committed by
GitHub
Feb 18, 2019
Browse files
more details in GPT-2 usage example
parent
522733f6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
9 deletions
+19
-9
README.md
README.md
+19
-9
No files found.
README.md
View file @
ada22a1c
...
@@ -400,12 +400,15 @@ logging.basicConfig(level=logging.INFO)
...
@@ -400,12 +400,15 @@ logging.basicConfig(level=logging.INFO)
# Load pre-trained model tokenizer (vocabulary)
# Load pre-trained model tokenizer (vocabulary)
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
# Encode input
# Encode some inputs
text = "Who was Jim Henson ? Jim Henson was a puppeteer"
text_1 = "Who was Jim Henson ?"
indexed_tokens = tokenizer.encode(text)
text_2 = "Jim Henson was a puppeteer"
indexed_tokens_1 = tokenizer.encode(text_1)
indexed_tokens_2 = tokenizer.encode(text_2)
# Convert inputs to PyTorch tensors
# Convert inputs to PyTorch tensors
tokens_tensor = torch.tensor([indexed_tokens])
tokens_tensor_1 = torch.tensor([indexed_tokens_1])
tokens_tensor_2 = torch.tensor([indexed_tokens_2])
```
```
Let's see how to use `
GPT2Model
` to get hidden states
Let's see how to use `
GPT2Model
` to get hidden states
...
@@ -416,12 +419,16 @@ model = GPT2Model.from_pretrained('gpt2')
...
@@ -416,12 +419,16 @@ model = GPT2Model.from_pretrained('gpt2')
model.eval()
model.eval()
# If you have a GPU, put everything on cuda
# If you have a GPU, put everything on cuda
tokens_tensor = tokens_tensor.to('cuda')
tokens_tensor_1 = tokens_tensor_1.to('cuda')
tokens_tensor_2 = tokens_tensor_2.to('cuda')
model.to('cuda')
model.to('cuda')
# Predict hidden states features for each layer
# Predict hidden states features for each layer
with torch.no_grad():
with torch.no_grad():
hidden_states = model(tokens_tensor)
hidden_states_1, past = model(tokens_tensor_1)
# past can be used to reuse precomputed hidden state in a subsequent predictions
# (see beam-search examples in the run_gpt2.py example
hidden_states-2, past = model(tokens_tensor_2, past=past)
```
```
And how to use `
GPT2LMHeadModel
`
And how to use `
GPT2LMHeadModel
`
...
@@ -432,15 +439,18 @@ model = GPT2LMHeadModel.from_pretrained('gpt2')
...
@@ -432,15 +439,18 @@ model = GPT2LMHeadModel.from_pretrained('gpt2')
model.eval()
model.eval()
# If you have a GPU, put everything on cuda
# If you have a GPU, put everything on cuda
tokens_tensor = tokens_tensor.to('cuda')
tokens_tensor
_1
= tokens_tensor.to('cuda')
model.to('cuda')
model.to('cuda')
# Predict all tokens
# Predict all tokens
with torch.no_grad():
with torch.no_grad():
predictions = model(tokens_tensor)
predictions_1, past = model(tokens_tensor_1)
# past can be used to reuse precomputed hidden state in a subsequent predictions
# (see beam-search examples in the run_gpt2.py example
predictions_2, past = model(tokens_tensor_2, past=past)
# get the predicted last token
# get the predicted last token
predicted_index = torch.argmax(predictions[0, -1, :]).item()
predicted_index = torch.argmax(predictions
_2
[0, -1, :]).item()
predicted_token = tokenizer.decode([predicted_index])
predicted_token = tokenizer.decode([predicted_index])
```
```
...
...
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