-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
36 lines (30 loc) · 1001 Bytes
/
utils.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
35
36
import os
import random
import math
import numpy as np
import pandas as pd
import torch
from torch.nn import init
def set_seed(seed):
# for reproducibility.
# note that pytorch is not completely reproducible
# https://pytorch.org/docs/stable/notes/randomness.html
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.initial_seed() # dataloader multi processing
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
return None
# https://github.com/sehkmg/tsvprint/blob/master/utils.py
def dict2tsv(res, file_name):
if not os.path.exists(file_name):
with open(file_name, 'a') as f:
f.write('\t'.join(list(res.keys())))
f.write('\n')
with open(file_name, 'a') as f:
f.write('\t'.join([str(r) for r in list(res.values())]))
f.write('\n')