Download helperFunctions.py from 3ZadeSSG/PLFNet: direct link, hf CLI and curl.
- Browser
- Download file 740 Bytes
-
https://huggingface.co/spaces/3ZadeSSG/PLFNet/resolve/main/helperFunctions.py
- Command line
-
hf download hf://spaces/3ZadeSSG/PLFNet/helperFunctions.py
-
curl -L -o helperFunctions.py https://huggingface.co/spaces/3ZadeSSG/PLFNet/resolve/main/helperFunctions.py
740 Bytes
| import torch | |
| import os | |
| import torch.nn.functional as F | |
| def save_checkpoint(model, filelocation, save_parallel = True): | |
| if save_parallel: | |
| torch.save(model.module.state_dict(), filelocation) | |
| else: | |
| torch.save(model.state_dict(), filelocation) | |
| def load_Checkpoint(fileLocation,model, load_cpu=False): | |
| if load_cpu: | |
| model.load_state_dict(torch.load(fileLocation,map_location=lambda storage, loc: storage)) | |
| else: | |
| model.load_state_dict(torch.load(fileLocation)) | |
| return model | |
| def writeLog(logList, filename): | |
| with open(filename, 'w') as outfile: | |
| outfile.write("\n".join(logList)) | |
| def kl_loss(mu, logvar): | |
| return -0.5 * (1 + logvar - mu.pow(2) - logvar.exp()).mean() | |