Thank you for the helpful implementation, but I think line:
kernel *= 1 / (std * math.sqrt(2 * math.pi)) * \
          torch.exp(-((mgrid - mean) / (2 * std)) ** 2)

should change to
kernel *= 1 / (std * math.sqrt(2 * math.pi)) * \
          torch.exp((-((mgrid - mean) / std) ** 2) / 2)

since: