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
51690699
Commit
51690699
authored
Jan 30, 2019
by
Matej Svejda
Browse files
make examples consistent, revert error in num_train_steps calculation
parent
9c6a48c8
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
21 additions
and
17 deletions
+21
-17
examples/run_classifier.py
examples/run_classifier.py
+3
-3
examples/run_lm_finetuning.py
examples/run_lm_finetuning.py
+9
-5
examples/run_squad.py
examples/run_squad.py
+3
-3
examples/run_squad2.py
examples/run_squad2.py
+3
-3
examples/run_swag.py
examples/run_swag.py
+3
-3
No files found.
examples/run_classifier.py
View file @
51690699
...
@@ -411,7 +411,7 @@ def main():
...
@@ -411,7 +411,7 @@ def main():
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
args
.
gradient_accumulation_steps
))
args
.
gradient_accumulation_steps
))
args
.
train_batch_size
=
int
(
args
.
train_batch_size
/
args
.
gradient_accumulation_steps
)
args
.
train_batch_size
=
args
.
train_batch_size
/
/
args
.
gradient_accumulation_steps
random
.
seed
(
args
.
seed
)
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
...
@@ -441,8 +441,8 @@ def main():
...
@@ -441,8 +441,8 @@ def main():
num_train_steps
=
None
num_train_steps
=
None
if
args
.
do_train
:
if
args
.
do_train
:
train_examples
=
processor
.
get_train_examples
(
args
.
data_dir
)
train_examples
=
processor
.
get_train_examples
(
args
.
data_dir
)
num_train_steps
=
int
(
num_train_steps
=
len
(
train_examples
)
/
args
.
train_batch_size
*
args
.
num_train_epochs
)
len
(
train_examples
)
/
/
args
.
train_batch_size
//
args
.
gradient_accumulation_steps
*
args
.
num_train_epochs
# Prepare model
# Prepare model
model
=
BertForSequenceClassification
.
from_pretrained
(
args
.
bert_model
,
model
=
BertForSequenceClassification
.
from_pretrained
(
args
.
bert_model
,
...
...
examples/run_lm_finetuning.py
View file @
51690699
...
@@ -497,7 +497,7 @@ def main():
...
@@ -497,7 +497,7 @@ def main():
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
args
.
gradient_accumulation_steps
))
args
.
gradient_accumulation_steps
))
args
.
train_batch_size
=
int
(
args
.
train_batch_size
/
args
.
gradient_accumulation_steps
)
args
.
train_batch_size
=
args
.
train_batch_size
/
/
args
.
gradient_accumulation_steps
random
.
seed
(
args
.
seed
)
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
...
@@ -520,8 +520,8 @@ def main():
...
@@ -520,8 +520,8 @@ def main():
print
(
"Loading Train Dataset"
,
args
.
train_file
)
print
(
"Loading Train Dataset"
,
args
.
train_file
)
train_dataset
=
BERTDataset
(
args
.
train_file
,
tokenizer
,
seq_len
=
args
.
max_seq_length
,
train_dataset
=
BERTDataset
(
args
.
train_file
,
tokenizer
,
seq_len
=
args
.
max_seq_length
,
corpus_lines
=
None
,
on_memory
=
args
.
on_memory
)
corpus_lines
=
None
,
on_memory
=
args
.
on_memory
)
num_train_steps
=
int
(
num_train_steps
=
len
(
train_dataset
)
/
args
.
train_batch_size
*
args
.
num_train_epochs
)
len
(
train_dataset
)
/
/
args
.
train_batch_size
//
args
.
gradient_accumulation_steps
*
args
.
num_train_epochs
# Prepare model
# Prepare model
model
=
BertForPreTraining
.
from_pretrained
(
args
.
bert_model
)
model
=
BertForPreTraining
.
from_pretrained
(
args
.
bert_model
)
...
@@ -544,6 +544,10 @@ def main():
...
@@ -544,6 +544,10 @@ def main():
{
'params'
:
[
p
for
n
,
p
in
param_optimizer
if
not
any
(
nd
in
n
for
nd
in
no_decay
)],
'weight_decay'
:
0.01
},
{
'params'
:
[
p
for
n
,
p
in
param_optimizer
if
not
any
(
nd
in
n
for
nd
in
no_decay
)],
'weight_decay'
:
0.01
},
{
'params'
:
[
p
for
n
,
p
in
param_optimizer
if
any
(
nd
in
n
for
nd
in
no_decay
)],
'weight_decay'
:
0.0
}
{
'params'
:
[
p
for
n
,
p
in
param_optimizer
if
any
(
nd
in
n
for
nd
in
no_decay
)],
'weight_decay'
:
0.0
}
]
]
t_total
=
num_train_steps
if
args
.
local_rank
!=
-
1
:
t_total
=
t_total
//
torch
.
distributed
.
get_world_size
()
if
args
.
fp16
:
if
args
.
fp16
:
try
:
try
:
from
apex.optimizers
import
FP16_Optimizer
from
apex.optimizers
import
FP16_Optimizer
...
@@ -564,7 +568,7 @@ def main():
...
@@ -564,7 +568,7 @@ def main():
optimizer
=
BertAdam
(
optimizer_grouped_parameters
,
optimizer
=
BertAdam
(
optimizer_grouped_parameters
,
lr
=
args
.
learning_rate
,
lr
=
args
.
learning_rate
,
warmup
=
args
.
warmup_proportion
,
warmup
=
args
.
warmup_proportion
,
t_total
=
num_train_steps
)
t_total
=
t_total
)
global_step
=
0
global_step
=
0
if
args
.
do_train
:
if
args
.
do_train
:
...
@@ -604,7 +608,7 @@ def main():
...
@@ -604,7 +608,7 @@ def main():
if
args
.
fp16
:
if
args
.
fp16
:
# modify learning rate with special warm up BERT uses
# modify learning rate with special warm up BERT uses
# if args.fp16 is False, BertAdam is used that handles this automatically
# if args.fp16 is False, BertAdam is used that handles this automatically
lr_this_step
=
args
.
learning_rate
*
warmup_linear
(
global_step
/
num_train_steps
,
args
.
warmup_proportion
)
lr_this_step
=
args
.
learning_rate
*
warmup_linear
(
global_step
/
t_total
,
args
.
warmup_proportion
)
for
param_group
in
optimizer
.
param_groups
:
for
param_group
in
optimizer
.
param_groups
:
param_group
[
'lr'
]
=
lr_this_step
param_group
[
'lr'
]
=
lr_this_step
optimizer
.
step
()
optimizer
.
step
()
...
...
examples/run_squad.py
View file @
51690699
...
@@ -757,7 +757,7 @@ def main():
...
@@ -757,7 +757,7 @@ def main():
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
args
.
gradient_accumulation_steps
))
args
.
gradient_accumulation_steps
))
args
.
train_batch_size
=
int
(
args
.
train_batch_size
/
args
.
gradient_accumulation_steps
)
args
.
train_batch_size
=
args
.
train_batch_size
/
/
args
.
gradient_accumulation_steps
random
.
seed
(
args
.
seed
)
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
...
@@ -788,8 +788,8 @@ def main():
...
@@ -788,8 +788,8 @@ def main():
if
args
.
do_train
:
if
args
.
do_train
:
train_examples
=
read_squad_examples
(
train_examples
=
read_squad_examples
(
input_file
=
args
.
train_file
,
is_training
=
True
)
input_file
=
args
.
train_file
,
is_training
=
True
)
num_train_steps
=
int
(
num_train_steps
=
len
(
train_examples
)
/
args
.
train_batch_size
*
args
.
num_train_epochs
)
len
(
train_examples
)
/
/
args
.
train_batch_size
//
args
.
gradient_accumulation_steps
*
args
.
num_train_epochs
# Prepare model
# Prepare model
model
=
BertForQuestionAnswering
.
from_pretrained
(
args
.
bert_model
,
model
=
BertForQuestionAnswering
.
from_pretrained
(
args
.
bert_model
,
...
...
examples/run_squad2.py
View file @
51690699
...
@@ -850,7 +850,7 @@ def main():
...
@@ -850,7 +850,7 @@ def main():
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
args
.
gradient_accumulation_steps
))
args
.
gradient_accumulation_steps
))
args
.
train_batch_size
=
int
(
args
.
train_batch_size
/
args
.
gradient_accumulation_steps
)
args
.
train_batch_size
=
args
.
train_batch_size
/
/
args
.
gradient_accumulation_steps
random
.
seed
(
args
.
seed
)
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
...
@@ -881,8 +881,8 @@ def main():
...
@@ -881,8 +881,8 @@ def main():
if
args
.
do_train
:
if
args
.
do_train
:
train_examples
=
read_squad_examples
(
train_examples
=
read_squad_examples
(
input_file
=
args
.
train_file
,
is_training
=
True
)
input_file
=
args
.
train_file
,
is_training
=
True
)
num_train_steps
=
int
(
num_train_steps
=
len
(
train_examples
)
/
args
.
train_batch_size
*
args
.
num_train_epochs
)
len
(
train_examples
)
/
/
args
.
train_batch_size
//
args
.
gradient_accumulation_steps
*
args
.
num_train_epochs
# Prepare model
# Prepare model
model
=
BertForQuestionAnswering
.
from_pretrained
(
args
.
bert_model
,
model
=
BertForQuestionAnswering
.
from_pretrained
(
args
.
bert_model
,
...
...
examples/run_swag.py
View file @
51690699
...
@@ -331,7 +331,7 @@ def main():
...
@@ -331,7 +331,7 @@ def main():
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
raise
ValueError
(
"Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
.
format
(
args
.
gradient_accumulation_steps
))
args
.
gradient_accumulation_steps
))
args
.
train_batch_size
=
int
(
args
.
train_batch_size
/
args
.
gradient_accumulation_steps
)
args
.
train_batch_size
=
args
.
train_batch_size
/
/
args
.
gradient_accumulation_steps
random
.
seed
(
args
.
seed
)
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
...
@@ -352,8 +352,8 @@ def main():
...
@@ -352,8 +352,8 @@ def main():
num_train_steps
=
None
num_train_steps
=
None
if
args
.
do_train
:
if
args
.
do_train
:
train_examples
=
read_swag_examples
(
os
.
path
.
join
(
args
.
data_dir
,
'train.csv'
),
is_training
=
True
)
train_examples
=
read_swag_examples
(
os
.
path
.
join
(
args
.
data_dir
,
'train.csv'
),
is_training
=
True
)
num_train_steps
=
int
(
num_train_steps
=
len
(
train_examples
)
/
args
.
train_batch_size
*
args
.
num_train_epochs
)
len
(
train_examples
)
/
/
args
.
train_batch_size
//
args
.
gradient_accumulation_steps
*
args
.
num_train_epochs
# Prepare model
# Prepare model
model
=
BertForMultipleChoice
.
from_pretrained
(
args
.
bert_model
,
model
=
BertForMultipleChoice
.
from_pretrained
(
args
.
bert_model
,
...
...
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