Base

monitor_gpu_temperature(threshold:int, sleep_seconds:int, gpu_id:int)
Checks the GPU temperature and sleeps if it exceeds a threshold.
Parameters:
Returns:
None: The function will print a warning and sleep if the temperature exceeds the threshold.
Labels:

src_PyThon_NeuralNetwork_trainer_Base_monitor_gpu_temperature

class AverageMeter
computes and stores the average and current value
Author:
- Farshad Sangari
Methods:
create_save_dir()
Create a timestamped directory for saving model checkpoints and reports
Labels:

src_PyThon_NeuralNetwork_trainer_Base_create_save_dir

save_model(file_path:str, file_name:str, model:nn.Module, optimizer:Optional[nn.Module])
Save model and optimizer state
Parameters:
Returns:
None: Saves the model state to the specified file
Author:
- Yassin Riyazi
- Farshad Sangari
Labels:

src_PyThon_NeuralNetwork_trainer_Base_save_model

load_model(ckpt_path:Union[str, os.PathLike], model:nn.Module, optimizer:Optional[nn.Module])
Load model and optimizer state from checkpoint
Parameters:
Returns:
optimizer (Optional[nn.Module]): Optimizer with loaded state (if provided)
Labels:

src_PyThon_NeuralNetwork_trainer_Base_load_model

normal_accuracy(pred:torch.Tensor, labels:torch.Tensor)
Calculate the accuracy of predictions against true labels.
Parameters:
Returns:
float: Accuracy as a percentage
Labels:

src_PyThon_NeuralNetwork_trainer_Base_normal_accuracy

teacher_forcing_decay(epoch:int, num_epochs:int)
Calculate the teacher forcing ratio for a given epoch.
Parameters:
Returns:
float: Teacher forcing ratio for the current epoch
Labels:

src_PyThon_NeuralNetwork_trainer_Base_teacher_forcing_decay

HardNegativeMiningPostHandler(args:tuple[torch.Tensor, ...])
Post-processing handler for hard negative mining. This function can be customized to save or visualize hard negative samples. Currently, it does nothing but can be extended as needed.
Parameters:
Returns:
np.ndarray: Processed data, currently just returns the first tensor in args as a numpy array
Labels:

src_PyThon_NeuralNetwork_trainer_Base_HardNegativeMiningPostHandler

hard_negative_mining(model:nn.Module, dataloader:torch.utils.data.DataLoader, criterion:nn.Module, device:str, num_hard_samples:int)
Select the hardest examples (highest loss) from the dataset Returns a new DataLoader containing only the hard examples
Parameters:
Returns:
torch.utils.data.DataLoader: DataLoader containing only the hard examples
Todo:
- Add handler for different model types (e.g., CNN, LSTM)
Labels:

src_PyThon_NeuralNetwork_trainer_Base_hard_negative_mining

train(model:nn.Module, train_loader:torch.utils.data.DataLoader, val_loader:torch.utils.data.DataLoader, criterion:nn.Module, optimizer:nn.Module, epochs:int, device:str, model_name:str, ckpt_save_freq:int, ckpt_save_path:Union[str, os.PathLike], ckpt_path:Union[str, os.PathLike], report_path:Union[str, os.PathLike], lr_scheduler:torch.optim.lr_scheduler, Validation_save_threshold:float, use_hard_negative_mining:bool, hard_mining_freq:int, num_hard_samples:int, GPU_temperature:int, GPU_overheat_sleep:float)
Standard training loop for autoencoder models with hard negative mining
Parameters:
Returns:
report (pd.DataFrame): Training report with metrics
Todo:
- Plot training loss over epochs real time in the terminal or a window
Author:
- Yassin Riyazi
- Farshad Sangari
Labels:

src_PyThon_NeuralNetwork_trainer_Base_train