PyTorch loss function evaluation
I am trying to optimise a function (fit) using the acceleration of GPU in PyTorch. This is the straight Python code, where I doing the evaluation of fit:

import numpy as np 
...
for j in range(P):
    e[:,j] = z - h[:,j];
    fit[j] = 1/(sqrt(2*pi)*sigma*N)*np.sum(exp(-(e[:,j]**2)/(2*sigma**2))); 



  The dimension of the variables are: 
  z[Nx1], h[NxP], e[NxP], fit[1xP]. 
  where P is the number of dimension of fit and N is the length of each dimension. 
  
  I am aware that for loops should be avoided, so where it is my attempt to do it using PyTorch through torch.cuda.FloatTensor.


import torch 
dtype = torch.cuda.FloatTensor
e     = z - h;
fit   = 1/(torch.sqrt(2*pi)*sigma*N)*torch.sum(torch.exp(-(torch.pw(e,2))/(2*torch.pow(sigma,2)))); 



  Unfortunately it is not working. What is wrong? 
  Thank you!


I guess you are getting size mismatch error at the following line.

e = z - h


In your example, z is a vector (2d tensor of shape Nx1) and h is a 2d tensor of shape NxP. So, you can't directly subtract h from z.

You can do the following to avoid size mismatch error.

e = z.expand(*h.size()) - h


Here, z.expand(*h.size()) will convert the tensor z from Nx1 to NxP shape by duplicating the column vector P times.
