예제 #1
0
def get_weight_matrix(data_input: di.DataInput) -> tf.Tensor:
    distances = dist.euclidian_distance(data_input.data_x_train, data_input.inducting_x_train)

    reduce_min_1 = tf.reshape(tf.reduce_min(distances, axis=1), [-1, 1])
    min_1_condition = distances == reduce_min_1

    mask_1_distances = tf.reduce_max(distances) * tf.cast(min_1_condition, dtype=tf.float64)

    distances_masked = distances + mask_1_distances

    reduce_min_2 = tf.reshape(tf.reduce_min(distances_masked, axis=1), [-1, 1])
    min_2_condition = distances_masked == reduce_min_2

    weight_i = 1 - reduce_min_1 / (reduce_min_1 + reduce_min_2)

    w_matrix = tf.zeros_like(distances) + weight_i * tf.cast(min_1_condition, dtype=tf.float64) + \
               (1 - weight_i) * tf.cast(min_2_condition, dtype=tf.float64)

    return w_matrix
예제 #2
0
    def get_tf_tensor(self, hyper_parameter: List[tf.Tensor],
                      x_vector: np.ndarray,
                      x_vector_: np.ndarray) -> tf.Tensor:
        assert x_vector is not None and x_vector_ is not None, "Input vectors x and x_ uninitialized: " + str(
            self)
        assert len(hyper_parameter) == self.get_number_of_hyper_parameter(
        ), "Invalid hyper_param size: " + str(self)

        with tf.name_scope("SquaredExponentialKernel"):
            dist: tf.Tensor = aux_dist.euclidian_distance(x_vector, x_vector_)
            squared_distance: tf.Tensor = tf.square(dist,
                                                    name="squared_distance")

            result: tf.Tensor = \
                tf.math.exp(-0.5 * tf.math.divide(squared_distance, tf.square(hyper_parameter[0], name="l_squared")),
                            name="SquaredExponentialKernel")

            if global_param.p_scaled_base_kernel:
                result = tf.multiply(hyper_parameter[1], result)

        self.last_hyper_parameter = hyper_parameter

        return result