I know it’s an old topic, but I had the same question and get there. I think I’ve a much simpler and stable workaround to share.
Knowing the Person correlation is a “centered version” of the cosine similarity, you can simply get it with:
cos = nn.CosineSimilarity(dim=1, eps=1e-6)
pearson = cos(x1 - x1.mean(dim=1, keepdim=True), x2 - x2.mean(dim=1, keepdim=True))
Plus you benefit from the stability of the pytorch implementation of the cosine similarity, the eps term avoiding any division by 0. And dim let you choose the dimension to where the Pearson correlation is computed.
Hope that help someone.