-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
129 lines (111 loc) · 4.19 KB
/
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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import math
from itertools import zip_longest
import numpy as np
from torch import nn
from torch.nn import init
def gen_index_map(df, column, offset=0):
index_map = {origin: index + offset
for index, origin in enumerate(df[column].drop_duplicates())}
return index_map
def next_batch(data, batch_size):
data_length = len(data)
num_batches = math.ceil(data_length / batch_size)
for batch_index in range(num_batches):
start_index = batch_index * batch_size
end_index = min((batch_index + 1) * batch_size, data_length)
yield data[start_index:end_index]
def shuffle_along_axis(a, axis):
idx = np.random.rand(*a.shape).argsort(axis=axis)
return np.take_along_axis(a,idx,axis=axis)
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
non_zero_index = (y_true > 0)
y_true = y_true[non_zero_index]
y_pred = y_pred[non_zero_index]
mape = np.abs((y_true - y_pred) / y_true)
mape[np.isinf(mape)] = 0
return np.mean(mape) * 100
def create_src_trg(full_seq, pre_len, fill_value):
src_seq, trg_seq = zip(*[[s[:-pre_len], s[-pre_len:]] for s in full_seq])
src_seq = np.transpose(np.array(list(zip_longest(*src_seq, fillvalue=fill_value))))
return src_seq, np.array(trg_seq)
def create_src(full_seq, fill_value):
return np.transpose(np.array(list(zip_longest(*full_seq, fillvalue=fill_value))))
def top_n_accuracy(truths, preds, n):
best_n = np.argsort(preds, axis=1)[:, -n:]
successes = 0
for i, truth in enumerate(truths):
if truth in best_n[i, :]:
successes += 1
return float(successes) / truths.shape[0]
def weight_init(m):
"""
Usage:
model = Model()
model.apply(weight_init)
"""
if isinstance(m, nn.Conv1d):
init.normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.Conv2d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.Conv3d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose1d):
init.normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose2d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose3d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.BatchNorm1d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm2d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm3d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.Linear):
init.xavier_normal_(m.weight.data)
init.normal_(m.bias.data)
elif isinstance(m, nn.LSTM):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.LSTMCell):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.GRU):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.GRUCell):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.Embedding):
embed_size = m.weight.size(-1)
if embed_size > 0:
init_range = 0.5/m.weight.size(-1)
init.uniform_(m.weight.data, -init_range, init_range)