def kernel(self, points1, points2=None): if points2 is None: points2 = points1 white_noise = self.white * util.eye(tf.shape(points1)[0]) else: white_noise = 0.0 #This has to be replaced. Is used so as to have a tensor for points1 and points2 points1 = points1 / self.lengthscale * self.lengthscale points2 = points2 / self.lengthscale * self.lengthscale magnitude_square1 = tf.expand_dims(tf.reduce_sum(points1**2, 1), 1) magnitude_square2 = tf.expand_dims(tf.reduce_sum(points2**2, 1), 1) distances = (magnitude_square1 - 2 * tf.matmul(points1, tf.transpose(points2)) + tf.transpose(magnitude_square2)) distances_root = tf.sqrt( distances + 0.05) / self.lengthscale #Numerical stability problem!! distances_root = tf.clip_by_value(distances_root, 0.0, self.MAX_DIST) constant = np.sqrt(5.0) first_term = (1 + constant * distances_root + 5.0 / 3.0 * distances_root**2) * self.std_dev second_term = tf.exp(-constant * distances_root) kernel_matrix = tf.multiply(first_term, second_term) return kernel_matrix + white_noise
def kernel(self, points1, points2=None): if points2 is None: points2 = points1 white_noise = self.white * util.eye(tf.shape(points1)[0]) else: white_noise = 0.0 kern_matrix = np.zeros((points1.shape[0], points2.shape[0])) #tf.zeros((tf.shape(points1)[0],tf.shape(points2)[0])) output_list = [] for i in range(kern_matrix.shape[0]): for j in range(kern_matrix.shape[1]): distances = util.euclidean2(points1[i, :], points2[j, :]) distances = tf.clip_by_value(distances, 0.0, self.MAX_DIST) #V_this is to limit the distances between these two values first_term = tf.scalar_mul( self.std_dev, 1.0 + tf.scalar_mul( math.sqrt(3.0) / self.lengthscale, distances)) second_term = tf.exp( tf.scalar_mul(-math.sqrt(3.0) / self.lengthscale, distances)) output_list.append(tf.multiply(first_term, second_term)) kernel_matrix = tf.transpose( tf.reshape((tf.stack(output_list)), [points2.shape[0], points1.shape[0]])) return kernel_matrix + white_noise
def kernel(self, points1, points2=None): if points2 is None: points2 = points1 white_noise = self.white * util.eye(tf.shape(points1)[0]) else: white_noise = 0.0 kern = self.recursive_kernel(points1 / self.lengthscale, points2 / self.lengthscale, self.depth) return (self.std_dev ** 2) * kern + white_noise
def kernel(self, points1, points2=None): if points2 is None: points2 = points1 white_noise = self.white * util.eye(tf.shape(points1)[0]) else: white_noise = 0.0 #This has to be replaced. Is used so as to have a tensor for points1 and points2 points1 = points1 / self.std_dev * self.std_dev points2 = points2 / self.std_dev * self.std_dev variance = self.std_dev**2 kernel_matrix = tf.matmul(points1 * variance, tf.transpose(points2)) return kernel_matrix + white_noise
def kernel(self, points1, points2=None): if points2 is None: points2 = points1 white_noise = self.white * util.eye(tf.shape(points1)[0]) else: white_noise = 0.0 points1 = points1 / self.lengthscale points2 = points2 / self.lengthscale magnitude_square1 = tf.expand_dims(tf.reduce_sum(points1**2, 1), 1) magnitude_square2 = tf.expand_dims(tf.reduce_sum(points2**2, 1), 1) distances = (magnitude_square1 - 2 * tf.matmul(points1, tf.transpose(points2)) + tf.transpose(magnitude_square2)) distances = tf.clip_by_value(distances, 0.0, self.MAX_DIST) kern = ((self.std_dev**2) * tf.exp(-distances / 2.0)) return kern + white_noise
def kernel( self, points1, points2=None ): #V_this is just computing the kernel for two points (two vectors), not for N #points. It gives back one kernel value. Viene fatto per una matrice in generale. Non per una sola osservazione. if points2 is None: points2 = points1 white_noise = self.white * util.eye(tf.shape(points1)[0]) else: white_noise = 0.0 points1 = points1 / self.lengthscale points2 = points2 / self.lengthscale magnitude_square1 = tf.expand_dims(tf.reduce_sum(points1**2, 1), 1) magnitude_square2 = tf.expand_dims(tf.reduce_sum(points2**2, 1), 1) distances = (magnitude_square1 - 2 * tf.matmul(points1, tf.transpose(points2)) + tf.transpose(magnitude_square2)) #print("prima di clip", distances) distances = tf.clip_by_value(distances, 0.0, self.MAX_DIST) #print("dopo clip", distances) kern = ((self.std_dev**2) * tf.exp(-distances / 2.0)) return kern + white_noise