Beispiel #1
0
def one_hot_cross_entropy(y_true, y_pred, with_logit=True):
    if with_logit:
        y_pred = softmax(y_pred)

    else:
        pass

    y_pred = R.clip(y_pred, R.epsilon(), R.div(R.Scalar(1), R.epsilon()))
    N = y_pred.shape[0]
    loss = R.div(R.elemul(R.Scalar(-1), R.mul(R.sum(y_true, R.natlog(R.add(y_pred, 1e-9))))), R.Scalar(N))

    return loss
Beispiel #2
0
def sparse_cross_entropy(y_true, y_pred, with_logit=True):
    if with_logit:
        y_pred = softmax(y_pred)

    else:
        pass

    y_pred = R.clip(y_pred, R.epsilon(), R.div(R.Scalar(1), R.epsilon()))
    N = y_pred.shape[0]
    loss = R.elemul(R.Scalar(-1), R.div(R.sum(R.natlog(y_pred[R.len(y_pred), y_true])), R.Scalar(N)))

    return loss
Beispiel #3
0
    def distribution(self, x, mean, std):

        """
        Gaussian Distribution Function
        """ 
        numerator = R.square(x - mean)
        denominator = R.Scalar(2) * R.square(std)
        frac = R.div(numerator,denominator)
        exponent = R.exp(R.Scalar(-1) * frac)
        two_pi = R.Scalar(2) *  R.pi()
        gaussian_denominator = R.square_root(two_pi) * std
        gaussian_func = R.div(exponent, gaussian_denominator)
        return gaussian_func
Beispiel #4
0
    def distribution(self, x, mean, std):

        """
        Gaussian Distribution Function
        exponent = np.exp(-((x-mean)**2 / (2*std**2)))
        gauss_func = exponent / (np.sqrt(2*np.pi)*std)
        """ 
        numerator = R.square(x - mean)
        denominator = R.Scalar(2) * R.square(std)
        frac = R.div(numerator,denominator)
        exponent = R.exp(R.Scalar(-1) * frac)
        two_pi = R.Scalar(2) *  R.Scalar(3.141592653589793)
        gaussian_denominator = R.square_root(two_pi) * std
        gaussian_func = R.div(exponent, gaussian_denominator)
        return gaussian_func
def accuracy(y_true, y_pred):

  if not isinstance(y_true, R.Tensor):
      y_true = R.Tensor(y_true)
  if not isinstance(y_pred, R.Tensor):
      y_pred = R.Tensor(y_pred)
      
  accuracy = R.div(R.sum((y_pred == y_val)),y_pred.shape[0])
  return accuracy
Beispiel #6
0
def KL_div_loss(y_true, y_pred, d):
    if not isinstance(y_true, R.Tensor):
        y_true = R.Tensor(y_true)
    if not isinstance(y_pred, R.Tensor):
        y_pred = R.Tensor(y_pred)

    y_pred = R.clip(y_pred, R.epsilon(), R.Saclar(1) - R.epsilon())

    return R.elemul(y_true, R.natlog(R.div(y_true, y_pred)))
Beispiel #7
0
def f1_score(true_labels, pred_labels):
    if not isinstance(true_labels, R.Tensor):
        y_true = R.Tensor(true_labels)
    if not isinstance(pred_labels, R.Tensor):
        pred_labels = R.Tensor(pred_labels)
    pre = precision(true_labels, pred_labels)
    rec = recall(true_labels, pred_labels)
    return R.div(R.multiply(R.Scalar(2), R.multiply(pre, rec)),
                 R.add(pre, rec))
Beispiel #8
0
def accuracy(y_true, y_pred):
    if not isinstance(y_true, R.Tensor):
        if not isinstance(y_true, R.Op):
            y_true = R.Tensor(y_true)
    if not isinstance(y_pred, R.Tensor):
        if not isinstance(y_pred, R.Op):
            y_pred = R.Tensor(y_pred)

    return R.div(R.sum(R.equal(y_pred, y_true)), y_pred.shape_())
Beispiel #9
0
def standardize(x):
    """
    Standardize an array
    """
    if not isinstance(x, R.Tensor):
        x = R.Tensor(x)

    mean = R.mean(x)
    std = R.std(x)

    return R.div(R.sub(x, mean), std)
Beispiel #10
0
def z_score(x, axis=None):
    if not isinstance(x, R.Tensor):
        x = R.Tensor(x)

    if axis is not None:
        mean = R.mean(x, axis=axis)
        std = R.std(x, axis=axis)
    else:
        mean = R.mean(x)
        std = R.std(x)

    return R.div(R.sub(x, mean), std)
def r2_score(y_true, y_pred):

  if not isinstance(y_true, R.Tensor):
      y_true = R.Tensor(y_true)
  if not isinstance(y_pred, R.Tensor):
      y_pred = R.Tensor(y_pred)    
  
  scalar1 = R.Scalar(1)    
        
  SS_res = R.sum(R.square(R.sub(y_true, y_pred)))
  SS_tot = R.sum(R.square(R.sub(y_true, R.mean(y_true))))  

  return R.sub(scalar1, R.div(SS_res, R.add(SS_tot, R.epsilon())))
Beispiel #12
0
def normalize(x):
    """
    Normalize an array
    """
    if not isinstance(x, R.Tensor):
        x = R.Tensor(x)

    if len(x.output.shape) > 1:
        raise Exception("Unsupported input type")

    max = R.max(x)
    min = R.min(x)

    return R.div(R.sub(x, min), R.sub(max, min))
Beispiel #13
0
def r2_score(y_true, y_pred):
    if isinstance(y_true, R.Tensor) or isinstance(y_true, R.Op):
        pass
    else:
        y_true = R.Tensor(y_true, name="y_true")

    if isinstance(y_pred, R.Tensor) or isinstance(y_pred, R.Op):
        pass
    else:
        y_pred = R.Tensor(y_pred, name="y_pred")

    print(type(y_true), type(y_pred))

    scalar1 = R.Scalar(1)

    SS_res = R.sum(R.square(R.sub(y_pred, y_true)), name="ss_res")
    SS_tot = R.sum(R.square(R.sub(y_true, R.mean(y_true))), name="ss_tot")

    return R.sub(scalar1, R.div(SS_res, SS_tot), name="r2_score")
Beispiel #14
0
def pearson_correlation(x, y):
    """
    Calculate linear correlation(pearson correlation)
    """

    if not isinstance(x, R.Tensor):
        x = R.Tensor(x)
    if not isinstance(y, R.Tensor):
        y = R.Tensor(y)

    a = R.sum(R.square(x))
    b = R.sum(R.square(y))

    n = a.output.shape[0]

    return R.div(
        R.sub(R.multiply(R.Scalar(n), R.sum(R.multiply(x, y))),
              R.multiply(R.sum(x), R.sum(y))),
        R.multiply(
            R.square_root(R.sub(R.multiply(R.Scalar(n), a), R.square(b))),
            R.square_root(R.sub(R.multiply(R.Scalar(n), b), R.square(b)))))
  for i in R.sort(set(true_labels)):

    TP = R.sum(R.and(pred_labels == i, true_labels == i)) 
    TN = R.sum(R.and(pred_labels =! i, true_labels =! i))
    FP = R.sum(R.and(pred_labels == i, true_labels =! i))
    FN = R.sum(R.and(pred_labels =! i, true_labels == i))

    confusion.append([TP, TN, FP, FN])

  confusion = R.Tensor(confusion)

  if average=='macro':

    for i in confusion:
      TP ,TN ,FP ,FN = i[0] ,i[1] ,i[2] ,i[3]
      Recall = R.div(TP, R.add(TP, FN))
      Precision = R.div(TP, R.add(TP, FP))

      if Precision == 0 or Recall == 0 
          or Recall == np.nan or Precision == np.nan:
        final.append(0)

      else:
        F1 = R.div(R.elemul(R.Scalar(2), R.elemul(Recall, Precision)),R.sum(Recall ,Precision))
        final.append(F1)
        
    return R.mean(final)

  if average=='micro':

    confusion = R.Tensor(confusion)
def softmax(x):
    """
    Softmax Activation Function
    """
    exp = R.exp(x)
    return R.div(exp, R.sum(exp))
def sigmoid(x):
    """
    Sigmoid Activation Function
    """
    return R.div(R.one(), R.add(R.one(), R.exp(R.multiply(R.minus_one(), x))))
Beispiel #18
0
def recall(true_labels, pred_labels):
    var = R.equal(true_labels, pred_labels)
    [TP, TN, FN, FP] = get_TP_TN_FN_FP(true_labels, pred_labels)
    return R.div(TP, R.add(TP, FN))
Beispiel #19
0
def precision(true_labels, pred_labels):
    var = R.sum(R.equal(true_labels, pred_labels))
    [TP, TN, FN, FP] = get_TP_TN_FN_FP(true_labels, pred_labels)
    return R.div(TP, R.add(TP, FP))
Beispiel #20
0
    def find_split(self, X, y):
        ideal_col = None
        ideal_threshold = None

        num_observations = y.shape_().gather(R.Scalar(0))
        while num_observations.status != 'computed':
            pass
        num_observations = int(num_observations.output)
        if num_observations <= 1:
            return ideal_col, ideal_threshold

        y = y.reshape(shape=[num_observations])
        count_in_parent = R.Tensor([])
        for c in range(self.num_classes):
            count_in_parent = count_in_parent.concat(
                R.sum(R.equal(y, R.Scalar(c))).expand_dims())
        gini = R.square(
            count_in_parent.foreach(operation='div', params=num_observations))
        best_gini = R.sub(R.Scalar(1.0), R.sum(gini))
        temp_y = y.reshape(shape=[num_observations, 1])

        for col in range(self.num_features):
            temp_X = R.gather(
                R.transpose(X),
                R.Scalar(col)).reshape(shape=[num_observations, 1])
            all_data = R.concat(temp_X, temp_y, axis=1)

            column = R.gather(R.transpose(X), R.Scalar(col))
            ind = column.find_indices(R.sort(R.unique(column)))
            while ind.status != "computed":
                pass
            inform_server()
            sorted_data = R.Tensor([])
            for i in ind.output:
                sorted_data = sorted_data.concat(all_data.gather(
                    R.Tensor(i)))  # need to find another way to sort
            sorted_data_tpose = sorted_data.transpose()
            thresholds = sorted_data_tpose.gather(R.Scalar(0)).gather(
                R.Scalar(0))
            obs_classes = sorted_data_tpose.gather(R.Scalar(1)).gather(
                R.Scalar(0))

            num_left = R.Tensor([0] * self.num_classes)  # need ops
            num_right = count_in_parent
            for i in range(1, num_observations):
                class_ = R.gather(obs_classes, R.Tensor([i - 1]))
                classencoding = R.one_hot_encoding(
                    class_, depth=self.num_classes).gather(R.Scalar(0))
                num_left = num_left.add(classencoding)
                num_right = num_right.sub(classencoding)

                gini_left = R.sub(
                    R.Scalar(1),
                    R.sum(
                        R.square(R.foreach(num_left, operation='div',
                                           params=i))))
                gini_right = R.sub(
                    R.Scalar(1),
                    R.sum(
                        R.square(
                            R.foreach(num_right,
                                      operation='div',
                                      params=num_observations - i))))
                gini = R.div(
                    R.add(
                        R.multiply(R.Scalar(i), gini_left),
                        R.multiply(R.Scalar(num_observations - i),
                                   gini_right)), R.Scalar(num_observations))

                decision1 = R.logical_and(thresholds.gather(R.Tensor([i])),
                                          thresholds.gather(R.Tensor([i - 1])))
                decision2 = gini.less(best_gini)
                while decision2.status != "computed":
                    pass

                print(decision2.output == 1)
                if decision2.output == 1 and decision1 != 1:
                    best_gini = gini
                    ideal_col = col
                    ideal_threshold = R.div(
                        R.add(thresholds.gather(R.Tensor([i])),
                              thresholds.gather(R.Tensor([i - 1]))),
                        R.Scalar(2))
        print(ideal_col, ideal_threshold)
        return ideal_col, ideal_threshold
def tanh(x):
    """
    Tanh Activation Function
    """
    return R.div(R.sub(R.exp(x), R.exp(R.mul(R.minus_one(), x))),
                 R.add(R.exp(x), R.exp(R.mul(R.minus_one(), x))))