这个问题是对已经回答的以下问题的后续,我想在此正式提出一个新问题。原来的问题是:
如前所述,我目前正在训练TensorFlow模型来预测不同分布的参数。为此,我创建了适当的层并修改了损失函数。
不幸的是,当我使用多元t分布(tfp.distributions.MultivariateStudentTLinearOperator),时,会出现以下错误:
InvalidArgumentError: Input matrix is not invertible.
[[node negative_t_loss_2/negative_t_loss_2_MultivariateStudentTLinearOperator/log_prob/LinearOperatorLowerTriangular/solve/triangular_solve/MatrixTriangularSolve (defined at d:\20_programming\python\virtualenvs\tensorflow-gpu-2\lib\site-packages\tensorflow_probability\python\distributions\multivariate_student_t.py:265) ]] [Op:__inference_train_function_1471]
Function call stack:
train_function这一次,确定损失函数的程序如下:
def negative_t_loss_2(y_true, y_pred):
# Separate the parameters
n, mu1, mu2, sigma11, sigma12, sigma22 = tf.unstack(y_pred, num=6, axis=-1)
mu = tf.transpose([mu1, mu2], perm=[1, 0])
sigma = tf.linalg.LinearOperatorLowerTriangular(tf.transpose([[sigma11, sigma12], [sigma12, sigma22]], perm=[2, 0, 1]))
dist = tfp.distributions.MultivariateStudentTLinearOperator(df=n, loc=mu, scale=sigma)
nll = tf.reduce_mean(-dist.log_prob(y_true))
return nll我已经将完整的(更广泛的)代码和所需的数据复制到
https://drive.google.com/drive/folders/1IIAtKDB8paWV0aFVFALDUAiZTCqa5fAN?usp=sharing
(笔记本"normdist_2D_not_working_t.ipynb")。
我使用的操作系统是Windows 10,Python版本是3.6。示例代码中列出的所有库都是最新的,包括tensorflow-gpu。
如果能解决这个问题,我将不胜感激。这一专题与金融部门特别相关,因为这类分布在这方面发挥着重要作用,特别是在风险管理方面。
发布于 2021-11-12 16:07:24
当调用LinearOperatorLowerTriangular时,标度矩阵需要是下三角形,若要将张量转换为线性算子,只需替换
sigma = tf.linalg.LinearOperatorLowerTriangular(tf.transpose([[sigma11, sigma12], [sigma12, sigma22]], perm=[2, 0, 1]))出自:
sigma = tf.linalg.LinearOperatorLowerTriangular(tf.transpose([[sigma11, tf.zeros_like(sigma12)], [sigma12, sigma22]], perm=[2, 0, 1]))同时,学生-t的参数n是正的,所以您应该在n = tf.keras.activations.softplus(n)函数中添加negative_t_layer_2
那么,它应该能起作用。
https://stackoverflow.com/questions/69943995
复制相似问题