How to implement custom loss function

Hello,I am using a Unet network and need to write a loss function myself, this loss function needs to involve calculations that convert to numpy.

Summary
def my_mse_loss(input, target):
    a = input.data[0].numpy().flatten()
    b = target.data[0].numpy().flatten()
    loss = 0
    for i in range(len(a)):
        x = a[i]-b[i]
        loss += abs(x**2)
    return torch.Tensor(loss) / input.data.nelement()

Or can you write an example for me?More detailed.

Sorry but why do you need the numpy operation
I think you can just use toroch.nn.MSELoss
http://pytorch.org/docs/0.3.0/nn.html?highlight=mse#torch.nn.MSELoss

Or do I misunderstand this topic?

I want to do other operations, here is just an example

What kind of operations do you need from numpy that are not available in Pytorch?
It would make the implementation easier, if it’s possible to use pure Pytorch.