pytorch uses v1.5.1. Let the output of Linear be (x, y, vxx, vyy, vxy), create a variance-covariance matrix, and pass it to MultivariateNormal.
fc = nn.Linear(n, 5)
output = fc(x)
mean = output[:2]
vxx, vyy = nn.Softplus()(output[2:4])
vxy = output[-1]
covariance_matrix = torch.zeros(2, 2)
covariance_matrix[0, 0] += vxx
covariance_matrix[0, 1] += vxy
covariance_matrix[1, 0] += vxy
covariance_matrix[1, 1] += vyy
dist = MultivariateNormal(mean, covariance_matrix)
RuntimeError: cholesky_cuda: For batch 0: U(6,6) is zero, singular U. In MultivariateNormal, the variance-covariance matrix is Cholesky decomposed, so it is necessary to give a definite matrix. As it is, the above error occurs because it is not guaranteed that covariance_matrix is a definite matrix.
fc = nn.Linear(n, 5)
output = fc(x)
mean = output[:2]
a, c = nn.Softplus()(output[2:4])
b = output[-1]
L = torch.zeros(2, 2)
L[0, 0] += a
L[1, 0] += b
L[1, 1] += c
dist = MultivariateNormal(mean, scale_tril=L)
scale_tril (Tensor) – lower-triangular factor of covariance, with positive-valued diagonal
Therefore, the diagonal components $ a and c $ are set to positive values by softplus.
Cholesky decomposition when making a covariance matrix
\Sigma = LL^{T}
in accordance with,
covariance_matrix = np.dot(L, L.T)
And it is sufficient.
Recommended Posts