输入feature为(32,1024)的张量,会产生报错RuntimeError: Output 0 of SplitBackward0 is a view and is being modified inplace. This view is the output of a function that returns multiple views. Such functions do not allow the output views to be modified inplace. You should replace the inplace operation by an out-of-place one.,最终报错的语句是result = torch.relu_(input)
时间: 2024-04-11 20:29:01 浏览: 86
这个错误通常发生在原地(inplace)操作上,特别是在使用`torch.relu_()`函数时。这个错误的原因是,`torch.relu_()`函数返回一个与输入张量共享存储的视图,并且在原地修改了这个视图。然而,由于该视图是由多个视图返回的函数的输出之一,因此不允许原地修改。
要解决这个问题,你可以将`torch.relu_()`函数替换为`torch.relu()`函数,该函数会返回一个新的张量,而不是修改原始张量。修改后的代码如下:
```python
x = self.conv1(self.af(feature))
x = self.af(x)
x = self.conv2(x)
residual = self.conv_res(feature)
return torch.relu(x + residual)
```
通过使用`torch.relu()`函数,你可以避免原地修改导致的错误,并获得正确的结果。
相关问题
仍然是这个报错RuntimeError: Output 0 of SplitBackward0 is a view and is being modified inplace. This view is the output of a function that returns multiple views. Such functions do not allow the output views to be modified inplace. You should replace the inplace operation by an out-of-place one.
这个错误通常发生在原地(inplace)操作上,特别是在使用`torch.relu_()`函数时。这个错误的原因是,`torch.relu_()`函数返回一个与输入张量共享存储的视图,并且在原地修改了这个视图。然而,由于该视图是由多个视图返回的函数的输出之一,因此不允许原地修改。
要解决这个问题,你可以将`torch.relu_()`函数替换为`torch.relu()`函数,该函数会返回一个新的张量,而不是修改原始张量。修改后的代码如下:
```python
x = self.conv1(self.af(feature))
x = self.af(x)
x = self.conv2(x)
residual = self.conv_res(feature)
return torch.relu(x + residual)
```
通过使用`torch.relu()`函数,你可以避免原地修改导致的错误,并获得正确的结果。
RuntimeError: Output 0 of SplitBackward0 is a view and is being modified inplace. This view is the output of a function that returns multiple views. Such functions do not allow the output views to be modified inplace. You should replace the inplace operation by an out-of-place one.
这个错误通常是因为在一个函数返回多个视图的情况下,试图就地修改其中一个视图而引发的。这样的函数不允许就地修改输出视图,你应该使用非就地操作来替换它。例如,你可以使用类似 `torch.clone()` 或 `torch.tensor.clone()` 的方法创建一个新的张量,并对其进行修改,而不是直接修改原始视图。这样可以避免出现这个错误。