def get_TP_TN_FN_FP(true_labels, pred_labels): li = [None, None, None, None] var = R.equal(true_labels, pred_labels) TP = R.logical_and(true_labels, pred_labels) TN = R.logical_not(R.logical_or(true_labels, pred_labels)) FN = R.logical_not(R.logical_or(pred_labels, var)) FP = R.logical_and(pred_labels, R.logical_not(true_labels)) return [R.sum(TP), R.sum(TN), R.sum(FN), R.sum(FP)]
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())))
def grow_tree(self, X, y, depth=0): pop_per_class = R.Tensor([]) for c in range(self.num_classes): pop_per_class = pop_per_class.concat( R.sum(R.equal(y, R.Scalar(c))).expand_dims()) predicted_class = R.argmax(pop_per_class) node = Node(predicted_class=predicted_class, depth=depth) node.samples = R.shape(y).gather(R.Scalar(0)) if depth < self.max_depth: #col, threshold = self.find_split(X, y) col, threshold = 0, R.Tensor([12.895]) ''' ''' decision = R.Scalar(col).logical_and(threshold) while decision.status != "computed": pass if decision.output == 1: indices_left = X.transpose().gather( R.Scalar(col)).less(threshold) X_left, y_left = X.gather(indices_left), y.gather(indices_left) indices_right = X.transpose().gather( R.Scalar(col)).greater_equal(threshold) X_right, y_right = X.gather(indices_right), y.gather( indices_right) node.feature_index = col node.threshold = threshold node.left = self.grow_tree(X_left, y_left, depth + 1) node.left.leftbranch = True node.right = self.grow_tree(X_right, y_right, depth + 1) node.right.rightbranch = True return node
def grow_tree(self, X, y, depth=0): pop_per_class = R.Tensor([]) for c in range(self.num_classes): pop_per_class = pop_per_class.concat( R.sum(R.equal(y, R.Scalar(c))).expand_dims()) predicted_class = R.argmax(pop_per_class) while predicted_class.status != "computed": pass node = Node(predicted_class=predicted_class.output, depth=depth) node.samples = R.shape(y).gather(R.Scalar(0)) if depth < self.max_depth: col, threshold = self.find_split(X, y) while threshold.status != "computed": pass z = X.shape_() z1 = y.shape_() while z1.status != "computed": pass if col is not None and threshold.output is not [None]: indices_left = X.transpose().gather( R.Scalar(col)).less(threshold) X_left = X.gather( R.find_indices(indices_left, R.Tensor( [1])).reshape(shape=R.sum(indices_left).expand_dims())) y_left = y.gather( R.find_indices(indices_left, R.Tensor( [1])).reshape(shape=R.sum(indices_left).expand_dims())) indices_right = X.transpose().gather( R.Scalar(col)).greater_equal(threshold) X_right = X.gather( R.find_indices(indices_right, R.Tensor([ 1 ])).reshape(shape=R.sum(indices_right).expand_dims())) y_right = y.gather( R.find_indices(indices_right, R.Tensor([ 1 ])).reshape(shape=R.sum(indices_right).expand_dims())) node.feature_index = col node.threshold = threshold node.left = self.grow_tree(X_left, y_left, depth + 1) node.left.leftbranch = True node.right = self.grow_tree(X_right, y_right, depth + 1) node.right.rightbranch = True return node
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
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_())
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")
def update_weights(self): y_pred = self.predict(self.X) dW = Scalar(-1).multiply(Scalar(2).multiply(self.X.transpose().dot(self.y.sub(y_pred))).div(self.no_samples)) db = Scalar(-2).multiply(R.sum(self.y.sub(y_pred))).div(self.no_samples) self.W = self.W.sub(self.learning_rate.multiply(dW), name="W") self.b = self.b.sub(self.learning_rate.multiply(db), name="b") return self
def categorical_hinge(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) neg = R.max(R.elemul(R.sub(R.Scalar(-1), y_true), y_pred)) pos = R.sum(R.elemul(y_true, y_pred)) loss = R.max((R.sub(neg, pos), R.Scalar(1)), R.Scalar(0)) return loss
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
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
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)))))
def __compute_cost(self, y, y_pred, no_samples, name="cost"): """Cost function""" return R.multiply(R.Scalar(1.0 / (2.0 * no_samples.output)), R.sum(R.square(R.sub(y_pred, y))), name=name)
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) TP = R.sum(confusion ,axis=0)[0] TN = R.sum(confusion ,axis=0)[1] FP = R.sum(confusion ,axis=0)[2] FN = R.sum(confusion ,axis=0)[3] Recall = R.div(TP, R.add(TP, FN)) Precision = R.div(TP, R.add(TP, FP)) F1 = R.div(R.elemul(R.Scalar(2), R.elemul(Recall, Precision)),R.sum(Recall ,Precision))
def softmax(x): """ Softmax Activation Function """ exp = R.exp(x) return R.div(exp, R.sum(exp))
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 closest_centroids(self, points, centroids): centroids = R.expand_dims(centroids, axis=1) return R.argmin( R.square_root(R.sum(R.square(R.sub(points, centroids)), axis=2)))
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))
def loss(out, Y): # out and Y are both R.Tensor objects s = R.square(out.sub(Y)) s = R.sum(s).div(R.Scalar(leny)) return (s)