Skip to content

Instantly share code, notes, and snippets.

@fx86
Last active December 13, 2021 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fx86/73bdfb582392a100a94827da6935614f to your computer and use it in GitHub Desktop.
Save fx86/73bdfb582392a100a94827da6935614f to your computer and use it in GitHub Desktop.
Necessary code snippets for deep learning #DL
import gc
# clear memory in gpu usage and with garbage collection.
# additionally, prints variables with high memory usage.
def clear_memory():
gc.collect()
torch.cuda.empty_cache()
''' by Fred Cirera, https://stackoverflow.com/a/1094933/1870254, modified'''
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix)
for name, size in sorted(((name, sys.getsizeof(value)) for name, value in locals().items()),
key= lambda x: -x[1])[:10]:
print("{:>30}: {:>8}".format(name, sizeof_fmt(size)))
print(pd.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))
def seed_everything(seed):
"Set seed values"
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment