class_docstr: Implementation of Knowledge distillation from the paper "Distilling Task-Specific
  Knowledge from BERT into Simple Neural Networks" https://arxiv.org/pdf/1903.12136.pdf
  
  :param student_model (torch.nn.Module): Student model
  :param distill_train_loader (torch.utils.data.DataLoader): Student Training Dataloader for distillation
  :param distill_val_loader (torch.utils.data.DataLoader): Student Testing/validation Dataloader
  :param train_df (pandas.DataFrame): Dataframe for training the teacher model
  :param val_df (pandas.DataFrame): Dataframe for validating the teacher model
  :param loss_fn (torch.nn.module): Loss function
  :param temp (float): Temperature parameter for distillation
  :param distil_weight (float): Weight paramter for distillation loss
  :param device (str): Device used for training; 'cpu' for cpu and 'cuda' for gpu
  :param log (bool): True if logging required
  :param logdir (str): Directory for storing logs
class_docstr_tok: ["Implementation","of","Knowledge","distillation","from","the","paper","``","Distilling","Task-Specific","Knowledge","from","BERT","into","Simple","Neural","Networks","''","https",":","\/\/arxiv.org\/pdf\/1903.12136.pdf",":","param","student_model","(","torch.nn.Module",")",":","Student","model",":","param","distill_train_loader","(","torch.utils.data.DataLoader",")",":","Student","Training","Dataloader","for","distillation",":","param","distill_val_loader","(","torch.utils.data.DataLoader",")",":","Student","Testing\/validation","Dataloader",":","param","train_df","(","pandas.DataFrame",")",":","Dataframe","for","training","the","teacher","model",":","param","val_df","(","pandas.DataFrame",")",":","Dataframe","for","validating","the","teacher","model",":","param","loss_fn","(","torch.nn.module",")",":","Loss","function",":","param","temp","(","float",")",":","Temperature","parameter","for","distillation",":","param","distil_weight","(","float",")",":","Weight","paramter","for","distillation","loss",":","param","device","(","str",")",":","Device","used","for","training",";","'cpu","'","for","cpu","and","'cuda","'","for","gpu",":","param","log","(","bool",")",":","True","if","logging","required",":","param","logdir","(","str",")",":","Directory","for","storing","logs"]
docstr: Function that will be training the student
  
  :param epochs (int): Number of epochs you want to train the teacher
  :param plot_losses (bool): True if you want to plot the losses
  :param save_model (bool): True if you want to save the student model
  :param save_model_pth (str): Path where you want to save the student model
docstr_tok: ["Function","that","will","be","training","the","student",":","param","epochs","(","int",")",":","Number","of","epochs","you","want","to","train","the","teacher",":","param","plot_losses","(","bool",")",":","True","if","you","want","to","plot","the","losses",":","param","save_model","(","bool",")",":","True","if","you","want","to","save","the","student","model",":","param","save_model_pth","(","str",")",":","Path","where","you","want","to","save","the","student","model"]
code: def train_student(
      self,
      epochs=10,
      plot_losses=True,
      save_model=True,
      save_model_pth="./models/student.pth",
  ):
      """
      Function that will be training the student
  
      :param epochs (int): Number of epochs you want to train the teacher
      :param plot_losses (bool): True if you want to plot the losses
      :param save_model (bool): True if you want to save the student model
      :param save_model_pth (str): Path where you want to save the student model
      """
  
      self.teacher_distill_loader = self._get_teacher_dataloaders(
          batch_size=self.train_loader.batch_size, mode="distill"
      )
  
      y_pred_teacher = []
  
      print("Obtaining teacher predictions...")
      self.teacher_model.eval()
      self.teacher_model.to(self.device)
  
      for batch in self.teacher_distill_loader:
          b_input_ids = batch[0].to(self.device)
          b_input_mask = batch[1].to(self.device)
          b_labels = batch[2].to(self.device)
  
          with torch.no_grad():
              (loss, logits) = self.teacher_model(
                  b_input_ids,
                  token_type_ids=None,
                  attention_mask=b_input_mask,
                  labels=b_labels,
              )
  
              logits = logits.detach().cpu().numpy()
  
              y_pred_teacher.append(logits)
  
      self.student_model.train()
  
      loss_arr = []
      length_of_dataset = len(self.train_loader.dataset)
      best_acc = 0.0
      self.best_student_model_weights = deepcopy(
          self.student_model.state_dict()
      )
      self.student_model.to(self.device)
  
      print("\nTraining student...")
  
      for ep in range(epochs):
          print("")
          print(
              "======== Epoch {:} / {:} ========".format(ep + 1, epochs)
          )
  
          epoch_loss = 0.0
          correct = 0
  
          for (data, data_len, label), bert_prob in zip(
              self.train_loader, y_pred_teacher
          ):
              data = data.to(self.device)
              data_len = data_len.to(self.device)
              label = label.to(self.device)
  
              bert_prob = torch.tensor(bert_prob, dtype=torch.float)
              teacher_out = bert_prob.to(self.device)
  
              self.optimizer_student.zero_grad()
  
              student_out = self.student_model(data, data_len).squeeze(
                  1
              )
  
              loss = self.calculate_kd_loss(
                  student_out, teacher_out, label
              )
  
              pred = student_out.argmax(dim=1, keepdim=True)
              correct += pred.eq(label.view_as(pred)).sum().item()
  
              loss.backward()
  
              # ##For preventing exploding gradients
              # torch.nn.utils.clip_grad_norm_(self.student_model.parameters(), 1.0)
  
              self.optimizer_student.step()
  
              epoch_loss += loss
  
          epoch_acc = correct / length_of_dataset
          print(f"Loss: {epoch_loss} | Accuracy: {epoch_acc}")
  
          _, epoch_val_acc = self.evaluate_student()
          if epoch_val_acc > best_acc:
              best_acc = epoch_val_acc
              self.best_student_model_weights = deepcopy(
                  self.student_model.state_dict()
              )
  
          if self.log:
              self.writer.add_scalar(
                  "Training loss/Student", epoch_loss, epochs
              )
              self.writer.add_scalar(
                  "Training accuracy/Student", epoch_acc, epochs
              )
              self.writer.add_scalar(
                  "Validation accuracy/Student", epoch_val_acc, epochs
              )
  
          loss_arr.append(epoch_loss)
          print(
              f"Epoch: {ep+1}, Loss: {epoch_loss}, Accuracy: {epoch_acc}"
          )
  
      self.student_model.load_state_dict(
          self.best_student_model_weights
      )
  
      if save_model:
          torch.save(self.student_model.state_dict(), save_model_pth)
  
      if plot_losses:
          plt.plot(loss_arr)
  
file_path: KD_Lib/KD_Lib/KD/text/BERT2LSTM/bert2lstm.py
imports: import random
  from copy import deepcopy
  import matplotlib.pyplot
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from transformers import AdamW, BertForSequenceClassification, BertTokenizer
  from KD_Lib.KD.common import BaseClass
  from KD_Lib.KD.text.utils import get_bert_dataloader
label_desc: Use image node_id 6 for calling the BERT2LSTM obj's underlying member method code with example usage: obj.train_student(epochs, plot_losses, save_model, save_model_pth) without return types