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))
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 sigmoid(x): """ Sigmoid Activation Function """ return R.div(R.one(), R.add(R.one(), R.exp(R.multiply(R.minus_one(), x))))
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
import time import ravop.core as R from ravcom import inform_server a = R.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) b = R.Scalar(10) c = R.add(a, b) d = R.sub(a, b) e = R.multiply(a, b) f = R.mean(a) g = R.median(a) inform_server() # Wait for 10 seconds time.sleep(10) print(c()) print(d()) print(e()) print(f()) print(g())
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)