Skip to content

Commit

Permalink
fixing learning rate schedule when using gradient_accumulation_steps
Browse files Browse the repository at this point in the history
  • Loading branch information
thomwolf committed Nov 10, 2018
1 parent ea85cca commit a81a1ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion run_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def main():
if args.do_train:
train_examples = processor.get_train_examples(args.data_dir)
num_train_steps = int(
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)

model = BertForSequenceClassification(bert_config, len(label_list))
if args.init_checkpoint is not None:
Expand Down
18 changes: 17 additions & 1 deletion run_squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,10 @@ def main():
default=False,
action='store_true',
help="Whether to perform optimization and keep the optimizer averages on CPU")
parser.add_argument('--fp16',
default=False,
action='store_true',
help="Whether to use 16-bit float precision instead of 32-bit")


args = parser.parse_args()
Expand Down Expand Up @@ -801,11 +805,13 @@ def main():
train_examples = read_squad_examples(
input_file=args.train_file, is_training=True)
num_train_steps = int(
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)

model = BertForQuestionAnswering(bert_config)
if args.init_checkpoint is not None:
model.bert.load_state_dict(torch.load(args.init_checkpoint, map_location='cpu'))
if args.fp16:
model.half()

if not args.optimize_on_cpu:
model.to(device)
Expand Down Expand Up @@ -847,6 +853,12 @@ def main():
all_start_positions = torch.tensor([f.start_position for f in train_features], dtype=torch.long)
all_end_positions = torch.tensor([f.end_position for f in train_features], dtype=torch.long)

if args.fp16:
(all_input_ids, all_input_mask,
all_segment_ids, all_start_positions,
all_end_positions) = tuple(t.half() for t in (all_input_ids, all_input_mask, all_segment_ids,
all_start_positions, all_end_positions))

train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids,
all_start_positions, all_end_positions)
if args.local_rank == -1:
Expand Down Expand Up @@ -895,6 +907,10 @@ def main():
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
all_example_index = torch.arange(all_input_ids.size(0), dtype=torch.long)
if args.fp16:
(all_input_ids, all_input_mask,
all_segment_ids, all_example_index) = tuple(t.half() for t in (all_input_ids, all_input_mask,
all_segment_ids, all_example_index))

eval_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_example_index)
if args.local_rank == -1:
Expand Down

0 comments on commit a81a1ef

Please sign in to comment.