ValueError: Expected parameter scale (Tensor of shape (2854529,)) of distribution Normal(loc: torch.

正态分布,尺度需要大于0 ,

解决办法 scale = F.softplus(scale)

Traceback (most recent call last):
  

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/aistudio/bnn_pyro_fso_middle_2_16__256.py", line 469, in
    loss = svi.step(datax, datay)
  File "/home/aistudio/external-libraries/pyro/infer/svi.py", line 145, in step
    loss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)
  File "/home/aistudio/external-libraries/pyro/infer/trace_elbo.py", line 140, in loss_and_grads
    for model_trace, guide_trace in self._get_traces(model, guide, args, kwargs):
  File "/home/aistudio/external-libraries/pyro/infer/elbo.py", line 237, in _get_traces
    yield self._get_trace(model, guide, args, kwargs)
  File "/home/aistudio/external-libraries/pyro/infer/trace_elbo.py", line 57, in _get_trace
    model_trace, guide_trace = get_importance_trace(
  File "/home/aistudio/external-libraries/pyro/infer/enum.py", line 60, in get_importance_trace
    guide_trace = poutine.trace(guide, graph_type=graph_type).get_trace(
  File "/home/aistudio/external-libraries/pyro/poutine/trace_messenger.py", line 216, in get_trace
    self(*args, **kwargs)
  File "/home/aistudio/external-libraries/pyro/poutine/trace_messenger.py", line 198, in __call__
    raise exc from e
  File "/home/aistudio/external-libraries/pyro/poutine/trace_messenger.py", line 191, in __call__
    ret = self.fn(*args, **kwargs)
  File "/home/aistudio/external-libraries/pyro/nn/module.py", line 520, in __call__
    result = super().__call__(*args, **kwargs)
  File "/home/aistudio/external-libraries/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/aistudio/external-libraries/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/aistudio/external-libraries/pyro/infer/autoguide/guides.py", line 770, in forward
    latent = self.sample_latent(*args, **kwargs)
  File "/home/aistudio/external-libraries/pyro/infer/autoguide/guides.py", line 723, in sample_latent
    pos_dist = self.get_posterior(*args, **kwargs)
  File "/home/aistudio/external-libraries/pyro/infer/autoguide/guides.py", line 967, in get_posterior
    return dist.Normal(self.loc, self.scale).to_event(1)
  File "/home/aistudio/external-libraries/pyro/distributions/distribution.py", line 26, in __call__
    return super().__call__(*args, **kwargs)
  File "/home/aistudio/external-libraries/torch/distributions/normal.py", line 57, in __init__
    super().__init__(batch_shape, validate_args=validate_args)
  File "/home/aistudio/external-libraries/torch/distributions/distribution.py", line 70, in __init__
    raise ValueError(
ValueError: Expected parameter scale (Tensor of shape (2854529,)) of distribution Normal(loc: torch.Size([2854529]), scale: torch.Size([2854529])) to satisfy the constraint GreaterThan(lower_bound=0.0), but found invalid values:
tensor([0.1000, 0.1000, 0.1000,  ..., 0.1000, 0.1000, 0.0000], device='cuda:0',
       grad_fn=)
           Trace Shapes:        
            Param Sites:        
  AutoDiagonalNormal.loc 2854529
AutoDiagonalNormal.scale 2854529

这个错误信息指出在Pyro中创建正态分布`dist.Normal`时,`scale`参数包含了非正数(在这个案例中是0),这是不允许的,因为正态分布的标准差必须是大于0的实数。

错误发生在`AutoDiagonalNormal`的`get_posterior`方法中,当尝试创建一个正态分布对象时,`scale`参数的值中有0,这违反了正态分布的标准差必须大于0的约束。`scale`参数是通过`SoftplusBackward0`函数计算得到的,这个函数通常是`F.softplus`的梯度函数,它应该保证输出是非负的。

要解决这个问题,你可以采取以下措施:

1. **确保`scale`参数始终大于零**:在创建正态分布之前,使用`torch.clamp`或其他方法确保`scale`参数的所有值都大于一个很小的正数(例如`1e-6`)。

```python
scale = torch.clamp(scale, min=1e-6)
```

2. **检查`scale`参数的计算**:如果`scale`是通过模型中的某些计算得到的,确保这些计算不会导致0或负数的结果。

3. **使用变换**:如果你需要对`scale`参数进行变换以确保其为正,可以使用`F.softplus`(它将输入转换为非负数)。

```python
scale = F.softplus(scale)
```

4. **调试和监控**:在模型训练过程中监控`scale`参数的值,以确保它们在整个训练过程中保持有效。

5. **检查梯度**:如果`scale`是由模型学习得到的,检查其梯度是否正常,以确保在优化过程中不会遇到梯度爆炸或消失的问题。

6. **使用约束**:在定义模型时,可以使用概率编程框架提供的约束来确保参数满足特定条件。

在你的具体情况中,你需要检查`scale`参数是如何计算的,并确保在任何时候它都不会包含0或负数。如果`scale`是由模型参数决定的,你可能需要在模型定义中添加一个非负的约束,或者在采样之前对`scale`进行变换。

此外,由于这个错误是在执行变分推断(VI)时发生的,你可能还需要检查你的模型和引导(guide)是否正确定义,以及是否有任何潜在的问题可能导致无效的参数值。如果问题仍然存在,你可能需要进一步调试模型,或者考虑使用不同的建模策略。
 

你可能感兴趣的:(pytorch,深度学习,人工智能)