How you do efficiently parameterize a batch of covariance matrices in PyTorch

Viewed 261

I want to output a batch of multivaraible Gaussians (For likelihood based learning). But I don't know how I can do that. I have something like this

.
.
.
covar=self.covar(o_t) #returns batch_size*(obs_dim*obs_dim)
mean=self.mean(o_t)  #batch_size*obs_dim

Now I would like to create batch_size number of multivaraible Gaussians, for that I also need to make the covariance function positive definite. For N*N Tensors I know torch.matmul(a,a.T) would work, but I don't know how to do it here.

Alternatively I know I can parameterize using scale_tril, but I have the same problem, how do you ensure it's lower triangular + positive diagonals. I can imagine using some complicated masking, and then adding up. But I am sure there's an easier way.

Again for context, what I want to do from here, is compute the likelihood, using observed data, and learn using a version of sgd/adam.

Thanks

Edits : cov=((torch.tril(torch.ones(self.obs,self.obs),diagonal=-1)*sigma)+torch.diag_embed(F.softplus(diag))).to(device)

seems to work where diag is batch_sizeobs_dim diagonals, and this gives a batch of lower triangular matrices with postive diagonals, and sigma is of shape batch_sizeobs_dim*obs_dim

0 Answers
Related