-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·35 lines (27 loc) · 890 Bytes
/
utils.py
File metadata and controls
executable file
·35 lines (27 loc) · 890 Bytes
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 torch
import os, json
from datetime import datetime
def one_hot(x, d):
n = x.ndimension()
x = x.long().cpu()
if n == 1:
x_onehot = torch.zeros(x.size(0), d)
x_onehot.scatter_(1, x.unsqueeze(1), 1)
elif n == 2:
x_onehot = torch.zeros(x.size(0), x.size(1), d)
x_onehot.scatter_(2, x.unsqueeze(2), 1)
elif n == 3:
x_onehot = torch.zeros(x.size(0), x.size(1), x.size(2), d)
x_onehot.scatter_(3, x.unsqueeze(3), 1)
return x_onehot.squeeze()
# Logging function
def log(fname, s):
if not os.path.isdir(os.path.dirname(fname)):
os.system("mkdir -p " + os.path.dirname(fname))
f = open(fname, 'a')
f.write(str(datetime.now()) + ': ' + s + '\n')
f.close()
def read_config(file_path):
"""Read JSON config."""
json_object = json.load(open(file_path, 'r'))
return json_object