-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
89 lines (70 loc) · 1.66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sys
import h5py
import torch
from torch import nn
from torch import cuda
import string
import re
from collections import Counter
import numpy as np
def to_device(x, gpuid):
if gpuid == -1:
return x.cpu()
if x.device != gpuid:
return x.cuda(gpuid)
return x
def has_nan(t):
return torch.isnan(t).sum() == 1
def tensor_on_dev(t, is_cuda):
if is_cuda:
return t.cuda()
else:
return t
def pick_label(dist):
return np.argmax(dist, axis=1)
def torch2np(t, is_cuda):
return t.numpy() if not is_cuda else t.cpu().numpy()
def save_opt(opt, path):
with open(path, 'w') as f:
f.write('{0}'.format(opt))
def load_param_dict(path):
# TODO, this is ugly
f = h5py.File(path, 'r')
return f
def save_param_dict(param_dict, path):
file = h5py.File(path, 'w')
for name, p in param_dict.items():
file.create_dataset(name, data=p)
file.close()
def load_dict(path):
rs = {}
with open(path, 'r+') as f:
for l in f:
if l.strip() == '':
continue
w, idx, cnt = l.strip().split()
rs[int(idx)] = w
return rs
def rand_tensor(shape, r1, r2):
return (r1 - r2) * torch.rand(shape) + r2
def build_rnn(type, input_size, hidden_size, num_layers, bias, batch_first, dropout, bidirectional):
if type == 'lstm':
return nn.LSTM(input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
bias=bias,
batch_first=batch_first,
dropout=dropout,
bidirectional=bidirectional)
elif type == 'gru':
return nn.GRU(input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
bias=bias,
batch_first=batch_first,
dropout=dropout,
bidirectional=bidirectional)
else:
assert(False)
if __name__ == '__main__':
pass