-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
38 lines (28 loc) · 791 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
37
38
import random
import numpy as np
import torch
def set_seed_everywhere(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
def device():
return torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
class Until:
def __init__(self, until):
self._until = until
def __call__(self, step):
if self._until is None:
return True
return step < self._until
class Every:
def __init__(self, every):
self._every = every
def __call__(self, step):
if self._every is None:
return False
every = self._every
if step % every == 0:
return True
return False