forked from dsindex/ntagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
29 lines (25 loc) · 744 Bytes
/
util.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
from __future__ import absolute_import, division, print_function
import os
import pdb
import json
def load_config(opt):
try:
with open(opt.config, 'r', encoding='utf-8') as f:
config = json.load(f)
except Exception as e:
config = dict()
return config
def to_device(x, device):
if type(x) != list: # torch.tensor
x = x.to(device)
else: # list of torch.tensor
for i in range(len(x)):
x[i] = x[i].to(device)
return x
def to_numpy(x):
if type(x) != list: # torch.tensor
x = x.detach().cpu().numpy()
else: # list of torch.tensor
for i in range(len(x)):
x[i] = x[i].detach().cpu().numpy()
return x