Download Deception/Code/Multitask_Learning/utils.py from ankurani/deception: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/datasets/ankurani/deception/resolve/refs%2Fpr%2F1/Deception/Code/Multitask_Learning/utils.py
- Command line
-
hf download hf://datasets/ankurani/deception@refs/pr/1/Deception/Code/Multitask_Learning/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/ankurani/deception/resolve/refs%2Fpr%2F1/Deception/Code/Multitask_Learning/utils.py
1.42 kB
| import torch | |
| def save_checkpoint(save_path, model, optimizer, valid_loss): | |
| if save_path == None: | |
| return | |
| state_dict = {'model_state_dict': model.state_dict(), | |
| 'optimizer_state_dict': optimizer.state_dict(), | |
| 'valid_loss': valid_loss} | |
| torch.save(state_dict, save_path) | |
| print(f'Model saved to ==> {save_path}') | |
| def load_checkpoint(load_path, model, optimizer, device): | |
| if load_path == None: | |
| return | |
| state_dict = torch.load(load_path, map_location=device) | |
| print(f'Model loaded from <== {load_path}') | |
| model.load_state_dict(state_dict['model_state_dict']) | |
| optimizer.load_state_dict(state_dict['optimizer_state_dict']) | |
| return state_dict['valid_loss'] | |
| def save_metrics(save_path, train_loss_list, valid_loss_list, global_steps_list): | |
| if save_path == None: | |
| return | |
| state_dict = {'train_loss_list': train_loss_list, | |
| 'valid_loss_list': valid_loss_list, | |
| 'global_steps_list': global_steps_list} | |
| torch.save(state_dict, save_path) | |
| print(f'Model saved to ==> {save_path}') | |
| def load_metrics(load_path, device): | |
| if load_path == None: | |
| return | |
| state_dict = torch.load(load_path, map_location=device) | |
| print(f'Model loaded from <== {load_path}') | |
| return state_dict['train_loss_list'], state_dict['valid_loss_list'], state_dict['global_steps_list'] | |