Ejemplo n.º 1
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
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())))
Ejemplo n.º 3
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
Ejemplo n.º 4
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")
Ejemplo n.º 5
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)))))
Ejemplo n.º 6
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
Ejemplo n.º 7
0
 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)))
Ejemplo n.º 8
0
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)
 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)