title: How do you generate positive definite matrix in pytorch?
body: <p>I am trying to define Multivariate Gaussian distribution with randomly generated covariance matrix:</p>
  
  <pre><code>psi = torch.zeros(512).normal_(0., 1.).requires_grad_(True)
  
  # Generate random matrix
  Sigma_k = torch.rand(512, 512)
  # Make it symmetric positive
  Sigma_k = Sigma_k * Sigma_k.t()
  # Make it definite
  Sigma_k.add_(0.001, torch.eye(512)).requires_grad_(True)
  
  multivariate_normal.MultivariateNormal(psi, Sigma_k)
  </code></pre>
  
  <p>But I end up with getting an exception:</p>
  
  <blockquote>
    <p>RuntimeError: Lapack Error in potrf : the leading minor of order 2 is not positive definite at /Users/soumith/mc3build/conda-bld/pytorch_1549597882250/work/aten/src/TH/generic/THTensorLapack.cpp:658</p>
  </blockquote>
  
  <p>What is the proper way to generate positive definite square matrix?</p>