Hi Vidhi!
It’s not clear to me what your are trying to do.  But I will cheerfully
guess!



 vr308:

q_sigma = torch.nn.Parameter(torch.nn.functional.softplus(torch.randn(n, dim)))


Because you make q_sigma a Parameter I assume that you are
training it with some optimizer.
You initialize it with a positive value (the result of softplus()).


the q_sigma value become a small negative number (-0.0015)


Even though q_sigma started out positive, the optimizer has know way
of knowing that q_sigma is supposed to remain positive, so at some
point it takes an optimizer step that makes q_sigma negative.
I would suggest instead that you train a parameter that is allowed to
be negative, and then derive a positive q_sigma from it:
q_parameter = torch.nn.Parameter (torch.randn (n, dim))

Then later in your code when you need (the positive) q_sigma, simply
derive it from q_parameter (with something like softplus() that
guarantees a positive result).
The point is, instead of training q_sigma and trying to figure out some
way to constrain it to be positive – even though pytorch doesn’t naturally
do constrained optimizations – you train the possibly-negative (and
unconstrained) q_parameter, but then map it to an always-positive
q_sigma.
Good luck.
K. Frank