예제 #1
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("Matern_5_2_Kernel"):
            dist: tf.Tensor = aux_dist.manhattan_distance(x_vector, x_vector_)

            l = tf.abs(hyper_parameter[0])
            frac: tf.Tensor = tf.divide(
                tf.sqrt(tf.cast(5, dtype=global_param.p_dtype)) * dist, l)

            third_summand: tf.Tensor = tf.divide(
                tf.cast(5, dtype=global_param.p_dtype) * tf.square(dist),
                tf.cast(3, dtype=global_param.p_dtype) * tf.square(l))

            result: tf.Tensor = (tf.cast(1, dtype=global_param.p_dtype) +
                                 frac + third_summand) * tf.exp(-frac)

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

        self.last_hyper_parameter = hyper_parameter

        return result
예제 #2
0
    def get_l_derivative_matrix(hyper_parameter: List[tf.Tensor], x_vector: np.ndarray, x_vector_: np.ndarray) \
            -> tf.Tensor:
        diff: tf.Tensor = aux_dist.manhattan_distance(
            x_vector,
            x_vector_)  #tf.math.subtract(x_vector, tf.transpose(x_vector_))
        squared_distance: tf.Tensor = tf.square(diff, name="squared_distance")

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

        return tf.multiply(
            tf.divide(diff, tf.multiply(length_scale, l_squared)), cov_mat)
예제 #3
0
 def get_derivative_matrices(self, hyper_parameter: List[tf.Tensor], x_vector: np.ndarray, x_vector_: np.ndarray) -> \
         List[tf.Tensor]:
     pi = tf.cast(np.pi, global_param.p_dtype)
     dist = aux_dist.manhattan_distance(
         x_vector, x_vector_
     )  #tf.math.subtract(tf.reshape(x_vector, [-1, 1]), tf.reshape(x_vector_, [1, -1]))
     u = tf.math.multiply(pi, tf.math.divide(dist, hyper_parameter[1]))
     sine = tf.square(tf.sin(u))
     l_squared = tf.math.square(hyper_parameter[0])
     exponent = tf.divide(
         tf.multiply(tf.cast(-2, dtype=global_param.p_dtype), sine),
         l_squared)
     cov_mat = tf.math.exp(exponent, name="PeriodicKernel")
     self.last_hyper_parameter = hyper_parameter
     self.latest_cov_mat = cov_mat
     return [
         self.get_l_derivative_matrix(hyper_parameter, cov_mat, exponent),
         self.get_p_derivative_matrix(hyper_parameter, cov_mat, pi, dist, u,
                                      l_squared)
     ]
예제 #4
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("PeriodicKernel"):
            dist: tf.Tensor = aux_dist.manhattan_distance(x_vector, x_vector_)
            sine_input: tf.Tensor = tf.math.multiply(
                tf.cast(np.pi, global_param.p_dtype),
                tf.math.divide(dist, hyper_parameter[1]))
            sine: tf.Tensor = tf.square(tf.math.sin(sine_input))
        result: tf.Tensor = tf.math.exp(-2 * sine /
                                        tf.math.square(hyper_parameter[0]),
                                        name="PeriodicKernel")

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

        self.last_hyper_parameter = hyper_parameter

        return result
예제 #5
0
 def get_c_derivative_matrix(hyper_parameter: List[tf.Tensor], x_vector: np.ndarray, x_vector_: np.ndarray) \
         -> tf.Tensor:
     diff = aux_dist.manhattan_distance(x_vector, x_vector_)
     return tf.add(
         diff,
         tf.multiply(tf.cast(2, global_param.p_dtype), hyper_parameter[0]))