Diff of /helper.py [000000] .. [0f2bcf]

Switch to unified view

a b/helper.py
1
import numpy as np
2
import random
3
4
from sklearn.metrics import roc_auc_score, average_precision_score
5
6
def f_get_minibatch_set(mb_size_, x_set_, y_, m_):
7
    idx = range(np.shape(y_)[0])
8
    idx = random.sample(idx, mb_size_)
9
    
10
    x_set_mb = {}
11
    for m in range(len(x_set_)):
12
        x_set_mb[m] = x_set_[m][idx].astype(float)    
13
    y_mb   = y_[idx].astype(float)
14
    m_mb  = m_[idx].astype(float)
15
16
    return x_set_mb, y_mb, m_mb
17
18
19
def evaluate(true_y_, pred_y_, y_type_):
20
    if y_type_ == 'categorical':
21
        acc = np.mean(np.argmax(true_y_, axis=1) == np.argmax(pred_y_, axis=1))
22
        return acc
23
    elif y_type_ == 'binary':
24
        auroc = roc_auc_score(true_y_[:, 1], pred_y_[:,1])
25
        auprc = average_precision_score(true_y_[:, 1], pred_y_[:,1])
26
        return auroc, auprc
27
    elif y_type_ == 'continuous':
28
        mse   = np.mean((true_y_.reshape([-1]) - pred_y_.reshape([-1]))**2)
29
        return mse
30
    else:
31
        ValueError(print("y_type should be {'categorical', 'binary', 'continuous'}"))