Set up training logics

We now have all the ingredients we need for the machine learning model, now is time to specify the training logic for the model to learn

We have all the components we need

  1. Raw Data: Loaded the SMILE dataset

  2. Split Data: We used stratified sampling to ensure generalized distribution

  3. Data For Machine Learning: we tokenized the data into X_train, X_val

  4. Model we are going to use: Model = BertForSequenceClassification.from_pretrained

  5. Tools to feed data: dataloader_train = DataLoader

  6. Tools to train: optimizer = AdamW ; scheduler = get_linear_schedule_with_warmup

  7. Tools to measure performance: f1_score_func ; accuracy_per_class

Check environment, let model know whether to use GPU or even TPU

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
print(device)

Customize evaluate function for logging:

The main training happens in model.eval()

We customize the evaluate function to evaluate and log the validation performance

Training Loop:

tqdm : a progress bar library. tqdm derives from the Arabic word taqaddum (تقدّم) which can mean "progress," and is an abbreviation for "I love you so much" in Spanish (te quiero demasiado).

Logics of the loop:

  1. For the training, iterate through all epochs

  2. For the epoch, train on each batch

  3. Log the training performance on that epoch

Last updated

Was this helpful?