-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
64 lines (49 loc) · 1.71 KB
/
Copy pathutil.py
File metadata and controls
64 lines (49 loc) · 1.71 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import torch
import sys
class Logger(object):
"""
这个类的目的是尽可能不改变原始代码的情况下, 使得程序的输出同时打印在控制台和保存在文件中
用法: 只需在程序中加入一行 `sys.stdout = Logger(log_file_path)` 即可
"""
def __init__(self, file_path):
self.terminal = sys.stdout
self.log = open(file_path, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
#this flush method is needed for python 3 compatibility.
#this handles the flush command by doing nothing.
#you might want to specify some extra behavior here.
pass
def set_seed(seed):
import numpy as np
import random
import torch
import dgl
np.random.seed(seed)
random.seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # multi-gpu
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
dgl.seed(seed)
def mkdir_if_not_exist(file_name):
import os
import shutil
dir_name = os.path.dirname(file_name)
if not os.path.isdir(dir_name):
os.makedirs(dir_name)
def print_total_params(model):
total_params = sum(p.numel() for p in model.parameters())
trainable_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f'total params: {total_params}, trainable_total_params: {trainable_total_params}')
def nowdt():
"""
get string representation of date and time of now()
"""
from datetime import datetime
now = datetime.now()
return now.strftime("%d/%m/%Y %H:%M:%S")