unused argument (row_names = TRUE) >
时间: 2024-05-25 16:16:08 浏览: 17
This error message occurs when a function is called with an argument that it does not recognize or is not expecting. In this case, the function is expecting only one argument, but the user has provided an additional argument called "row_names" which the function does not know how to handle.
To resolve this error, the user should check the documentation for the function to determine what arguments it expects and ensure that any additional arguments are removed or corrected. Alternatively, the user may need to use a different function that can handle the desired argument.
相关问题
def calc_gradient_penalty(self, netD, real_data, fake_data): alpha = torch.rand(1, 1) alpha = alpha.expand(real_data.size()) alpha = alpha.cuda() interpolates = alpha * real_data + ((1 - alpha) * fake_data) interpolates = interpolates.cuda() interpolates = Variable(interpolates, requires_grad=True) disc_interpolates, s = netD.forward(interpolates) s = torch.autograd.Variable(torch.tensor(0.0), requires_grad=True).cuda() gradients1 = autograd.grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=torch.ones(disc_interpolates.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True, allow_unused=True)[0] gradients2 = autograd.grad(outputs=s, inputs=interpolates, grad_outputs=torch.ones(s.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True, allow_unused=True)[0] gradient_penalty = (((gradients1.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA) + \ (((gradients2.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA) return gradient_penalty,上述代码中(((gradients2.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA)提示出现错误:RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.
这个错误提示是因为在计算梯度时,有些张量(tensors)没有被使用到,可能是因为你在计算梯度时定义了一些不必要的变量,或者是你在定义某些变量时出现了问题。根据错误提示建议你设置 allow_unused=True,这样可以忽略未使用的张量而不报错。你可以这样修改代码:
```
gradients2 = autograd.grad(outputs=s, inputs=interpolates,
grad_outputs=torch.ones(s.size()).cuda(),
create_graph=True, retain_graph=True,
only_inputs=True, allow_unused=True)[0]
```
改为:
```
gradients2 = autograd.grad(outputs=s, inputs=interpolates,
grad_outputs=torch.ones(s.size()).cuda(),
create_graph=True, retain_graph=True,
only_inputs=True, allow_unused=True)[0]
if gradients2 is None:
return None
```
这样就可以避免报错了。
设置allow_unused=true
对于你的第一个问题,`allow_unused=True`是一个在PyTorch中使用的参数。当你定义一个函数或者模型时,PyTorch会检查所有的输入参数是否都被使用了。如果有某个参数在模型的前向计算中没有被使用到,PyTorch会抛出一个警告。通过设置`allow_unused=True`,你告诉PyTorch忽略这个警告,即允许存在未使用的参数。
这个参数通常用于一些特殊情况,比如你有一个通用的模型,但是根据不同的任务只使用其中的一部分参数。在这种情况下,你可以将不需要使用的参数设置为`allow_unused=True`,以避免警告的出现。
需要注意的是,`allow_unused=True`只是用来控制是否抛出警告,对于模型的计算结果没有影响。因此,在大多数情况下,不需要设置这个参数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)