Created
February 13, 2019 17:29
-
-
Save dalequark/650d1ae3d596d3ee2607c79c4b6381e9 to your computer and use it in GitHub Desktop.
BERT - train
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compute train and warmup steps from batch size | |
# These hyperparameters are copied from this colab notebook (https://colab.sandbox.google.com/github/tensorflow/tpu/blob/master/tools/colab/bert_finetuning_with_cloud_tpus.ipynb) | |
BATCH_SIZE = 32 | |
LEARNING_RATE = 2e-5 | |
NUM_TRAIN_EPOCHS = 3.0 | |
# Warmup is a period of time where hte learning rate | |
# is small and gradually increases--usually helps training. | |
WARMUP_PROPORTION = 0.1 | |
# Model configs | |
SAVE_CHECKPOINTS_STEPS = 500 | |
SAVE_SUMMARY_STEPS = 100 | |
# Compute # train and warmup steps from batch size | |
num_train_steps = int(len(train_features) / BATCH_SIZE * NUM_TRAIN_EPOCHS) | |
num_warmup_steps = int(num_train_steps * WARMUP_PROPORTION) | |
# Specify outpit directory and number of checkpoint steps to save | |
run_config = tf.estimator.RunConfig( | |
model_dir=OUTPUT_DIR, | |
save_summary_steps=SAVE_SUMMARY_STEPS, | |
save_checkpoints_steps=SAVE_CHECKPOINTS_STEPS) | |
# Build the model fucntion | |
model_fn = model_fn_builder( | |
num_labels=len(label_list), | |
learning_rate=LEARNING_RATE, | |
num_train_steps=num_train_steps, | |
num_warmup_steps=num_warmup_steps) | |
# Create an estimator | |
estimator = tf.estimator.Estimator( | |
model_fn=model_fn, | |
config=run_config, | |
params={"batch_size": BATCH_SIZE}) | |
# Create an input function for training. drop_remainder = True for using TPUs. | |
train_input_fn = bert.run_classifier.input_fn_builder( | |
features=train_features, | |
seq_length=MAX_SEQ_LENGTH, | |
is_training=True, | |
drop_remainder=False) | |
print(f'Beginning Training!') | |
current_time = datetime.now() | |
estimator.train(input_fn=train_input_fn, max_steps=num_train_steps) | |
print("Training took time ", datetime.now() - current_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment