Pytorch 为什么战俘在向后传球时返回南?

Pytorch 为什么战俘在向后传球时返回南?,pytorch,nan,tensor,python-3.8,Pytorch,Nan,Tensor,Python 3.8,我尝试将以下表达式作为损失函数进行计算: 此函数描述一系列闭合曲面。我正在使用一个神经网络来尝试预测曲面参数e1、e2、ax、ay、az,这些参数使给定点集的最小二乘距离最小化。数量F(p)-1虽然不成比例,但给出了点相对于曲面的距离度量。这是我的代码: def eval_inout_loss(self, p, x): #x: BxNx3 tensor -> x, y, z #p: Bx5 tensor -> e1, e2, ax, ay, az ####

我尝试将以下表达式作为损失函数进行计算:

此函数描述一系列闭合曲面。我正在使用一个神经网络来尝试预测曲面参数
e1、e2、ax、ay、az
,这些参数使给定点集的最小二乘距离最小化。数量F(p)-1虽然不成比例,但给出了点相对于曲面的距离度量。这是我的代码:

def eval_inout_loss(self, p, x):
    #x: BxNx3 tensor -> x, y, z
    #p: Bx5 tensor -> e1, e2, ax, ay, az

    ###########################################################################################
    # INSIDE - OUTSIDE FUNCTION LOSS
    ###########################################################################################
    # Evaluating F(x,y,z) and calculating Σ (F(x,y,z) - 1)^2
    
    #x^(2/e2), y^(2/e2), z^(2/e1)
    xs = torch.pow(torch.abs(x[:,:,0]), 2/p[:,1].unsqueeze(-1))
    ys = torch.pow(torch.abs(x[:,:,1]), 2/p[:,1].unsqueeze(-1))
    zs = torch.pow(torch.abs(x[:,:,2]), 2/p[:,0].unsqueeze(-1))

    s = torch.pow(xs + ys, p[:,1].unsqueeze(-1))
    s = torch.pow(s, 1/p[:,0].unsqueeze(-1)) + zs
    s = torch.pow(s, p[:,0].unsqueeze(-1))
    s = torch.abs(s - 1)

    #multiplying by axayaz to prioritize smaller volumes    
    volparams = p[:,2:].prod(-1)
    s = volparams * s.sum(-1)
    l1 = s.mean()
我知道
e1,e2的低值将导致该表达式在数值上不稳定,因此我通过在网络输出处应用sigmoid函数,将网络输出限制在
[0.2,1.9]
范围内:

x = 1.7 * torch.nn.functional.sigmoid(self.mlp6(x)) + 0.2
一个明显的怀疑是试图将一个负数提升为分数指数,从而产生nan作为输出。然而,这里不可能是这样,因为所有的数字都是平方的,因此严格来说是非负的。为了进一步增强数值稳定性,我在一篇相关论文中读到,提高
F(p)^e1
是有用的,我使用上面显示的
s=torch.pow(s,p[:,0].unsqueze(-1))
行来实现这一点

将“自动加载异常检测工具”设置为true会导致以下错误:

  File "/home/..../Desktop/code/file.py", line 391, in eval_inout_loss
  s = torch.pow(xs + ys, p[:,1].unsqueeze(-1))
  (function print_stack)                                                                                                                                                          
 
Traceback (most recent call last):
  File "/home/...../Desktop/code/samplenet.py", line 477, in <module>
  loss.backward()
  File "/home/..../.local/lib/python3.8/site-packages/torch/tensor.py", line 185, in backward
  torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/home/vlassis/.local/lib/python3.8/site-packages/torch/autograd/__init__.py", line 125, in backward
  Variable._execution_engine.run_backward(
  RuntimeError: Function 'PowBackward1' returned nan values in its 0th output.

出现
nan
的其他可能原因是什么?

为什么不发布一个?虽然不太可能,但您可能将sigmoid函数应用程序放在了错误的位置。@user202729不幸的是,即使是它的最小版本,代码也相当长,需要人们安装几个库并在gpu上执行一些培训。你知道有没有网站可以上传,这样问题就不会太长了?
e1 min max mean tensor(nan, device='cuda:0', grad_fn=<MinBackward1>) tensor(nan, device='cuda:0', grad_fn=<MaxBackward1>) tensor(nan, device='cuda:0', grad_fn=<MeanBackward0>)
e2 min max mean tensor(nan, device='cuda:0', grad_fn=<MinBackward1>) tensor(nan, device='cuda:0', grad_fn=<MaxBackward1>) tensor(nan, device='cuda:0', grad_fn=<MeanBackward0>)
x min max mean tensor(-1.8769, device='cuda:0') tensor(1.6930, device='cuda:0') tensor(0.0006, device='cuda:0')
y min max mean tensor(-1.4985, device='cuda:0') tensor(1.5802, device='cuda:0') tensor(0.0108, device='cuda:0')
z min max mean tensor(-1.6468, device='cuda:0') tensor(1.8991, device='cuda:0') tensor(-0.0091, device='cuda:0')
xs min max tensor(nan, device='cuda:0', grad_fn=<MinBackward1>) tensor(nan, device='cuda:0', grad_fn=<MaxBackward1>)
ys min max tensor(nan, device='cuda:0', grad_fn=<MinBackward1>) tensor(nan, device='cuda:0', grad_fn=<MaxBackward1>)
zs min max tensor(nan, device='cuda:0', grad_fn=<MinBackward1>) tensor(nan, device='cuda:0', grad_fn=<MaxBackward1>)