This should translate pretty cleanly to python. Pytorch and regular torch have a very similar API.
In particular, if you’re not familiar with python syntax, here’s how you define a function:
def unsup.pacov(x):
    mean = torch.mean(x, 1)
    xm = x - mean.expand(x)
    # etc etc

Disclaimer: I haven’t tried the above code out but you can find the corresponding pytorch functions in the documentations page: http://pytorch.org/docs/master/torch.html 73