-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutil.py
65 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
#coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
import theano.tensor as T
def one_hot_encoder(data, whole_set):
"""
对整个list做encoder,而不是单个record
"""
ret = []
for i in data:
idx = whole_set.index(i)
ret.append([1 if j==idx else 0 for j in range(len(whole_set))])
return ret
def one_hot_decoder(data, whole_set):
ret = []
for probs in data:
idx = np.argmax(probs)
ret.append(whole_set[idx])
return ret
def plot_loss_figure(history, save_path):
train_loss = history.history['loss']
val_loss = history.history['val_loss']
plt.plot(train_loss, 'b', val_loss, 'r')
plt.xlabel('train_loss: blue val_loss: red epoch')
plt.ylabel('loss')
plt.title('loss figure')
plt.savefig(save_path)
def pack_data(X, Y_nb, Y, max_nb_cha):
"""
pack data to match keras format
"""
data = {'output%d'%i:Y[i-1] for i in range(1, max_nb_cha+1)}
data['input'] = X
data['output_nb'] = Y_nb
return data
def log_prob(y_true, y_pred):
return -T.log(T.sum(y_pred*y_true, 1))
def label_smoothing(label):
eps = 0.01
k = label[0].shape[1]-1
f = np.vectorize(lambda x: x-eps if x==1 else eps/k)
label = f(label)
return label
def pack_data_single(X, Y, nb_class):
data = {'output%d'%i: np.array([j[i-1] for j in Y[0]]) for i in range(1, nb_class+1)}
data['input'] = X
return data
def weight_crossentropy(y_true, y_pred):
W = 35 # number of classes - 1
return -(W*y_true*T.log(y_pred) + (1-y_true)*T.log(1-y_pred))