-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
executable file
·70 lines (50 loc) · 1.56 KB
/
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
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
65
66
67
68
69
70
import torch
import os
import sys
import pickle
import numpy as np
from loss import EDL_Loss
def NPArrayList2PyTorchTensorList(input):
res = []
for item_array in input:
item_tensor = torch.Tensor(item_array).cuda()
res.append(item_tensor)
return res
def PyTorchParameterList2NPArrayList(input):
res = []
for item_tensor in input:
item_array = item_tensor.detach().cpu().clone().numpy()
res.append(item_array)
return res
def Predict(X, model):
if type(X) is not torch.Tensor:
X = torch.Tensor(X).long().cuda()
elif X.is_cuda is False:
X = X.cuda()
with torch.no_grad():
pred, _ = model(X)
return pred.cpu().clone().numpy()
def GetEDLLoss(pred, true):
if type(pred) is not torch.Tensor:
pred = torch.Tensor(pred).float().cuda()
elif pred.is_cuda is False:
pred = pred.cuda()
if type(true) is not torch.Tensor:
true = torch.Tensor(true).float().cuda()
elif true.is_cuda is False:
true = true.cuda()
loss = EDL_Loss()(true, pred).item()
return loss
def Load_pretrained_embeddings(file_path):
pretrained_fpath_saved = os.path.expanduser(file_path.format(sys.version_info.major))
with open(pretrained_fpath_saved, 'rb') as f:
embedding_weights = pickle.load(f)
f.close()
out = np.array(list(embedding_weights.values()))
print('embedding_weights shape:', out.shape)
return out
def ExpConfigurations():
out_dim = 6
model_idx = 1000
niterations = 1000
return out_dim, model_idx, niterations