forked from corenel/pytorch-adda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myfunc.py
34 lines (30 loc) · 1.07 KB
/
myfunc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import torch
import random
import numpy as np
from datetime import datetime
from termcolor import colored
import logging
def set_determinism(seed: int=1, benchmark: bool=False, determinism: bool=True):
assert sum([benchmark, determinism]) <= 1, "You can only set one of benchmark or determinism to True"
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = benchmark
if determinism:
if hasattr(torch, "set_deterministic"):
torch.set_deterministic(True)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def set_logger(save_dir):
os.makedirs(save_dir, exist_ok=True)
LOG_DEST = "log.txt"
logging.basicConfig(
level=logging.INFO,
format=colored("[%(asctime)s] [%(filename)s:%(lineno)d]", "green") + ": %(message)s",
datefmt="%y/%m/%d %H:%M:%S",
handlers=[
logging.FileHandler(os.path.join(save_dir, LOG_DEST)),
logging.StreamHandler()
])