tensorflow 似乎只支持 tf.float32 用于训练神经网络,这会导致我的算法中出现 Cholesky 分解问题。
以下代码是我的计算图的一部分,其中 X_latent 是要传入的张量,它返回一个正定矩阵。
def get_mat_LX(self, X_latent):
dim_P = int(X_latent.shape[1])
col = tf.reduce_sum(X_latent*X_latent, 1)
col = tf.reshape(col, [-1, 1])
prod = tf.matmul(X_latent, tf.transpose(X_latent))
log_mat_LX = - (0.5/self.rho+0.5/self.sigma)*col + 1/self.sigma*prod - (0.5/self.rho+0.5/self.sigma)*tf.transpose(col) + 2*math.log(self.alpha**0.5)-2*dim_P*math.log((math.pi*self.rho)**0.5)
mat_LX = tf.exp(log_mat_LX)
return mat_LX
Run Code Online (Sandbox Code Playgroud)
稍后,我需要对这个正定矩阵进行 cholesky 分解:
tensorflow_matrix = self.get_mat_LX(latent_var)
chol = tf.cholesky(tensorflow_matrix)
Run Code Online (Sandbox Code Playgroud)
但是,由于数据类型问题,矩阵中的一些小值(例如 3.825485980697876e-41)将被 tensorflow 截断为 0.0000000e+00,因此返回的矩阵不再是正定性并导致 cholesky 分解问题:
Cholesky decomposition was not successful. The input might not be valid.
Run Code Online (Sandbox Code Playgroud)
例如,对于
latent_var:
array([[ 1.4992752 , 0.4027754 , -0.9438937 , ..., …Run Code Online (Sandbox Code Playgroud)