【问题标题】:Pytorch trying to make a NN received an invalid combination of argumentsPytorch 试图让 NN 收到无效的参数组合
【发布时间】:2018-08-10 15:33:57
【问题描述】:

我正在尝试使用 pytroch 构建我的第一个 NN,但遇到了问题。

TypeError: new() 收到了无效的参数组合 - 得到了 (float, int, int, int),但预期是以下之一: * (torch.device 设备) * (torch.Storage 存储) *(张量其他) *(整数大小的元组,torch.device 设备) *(对象数据,torch.device 设备)

现在我知道这是在说什么,因为我没有将正确的类型传递给方法或 init。但我不知道我应该通过什么,因为它看起来对我来说是正确的。

def main():
#Get the time and data
now = datetime.datetime.now()
hourGlassToStack = 2 #Hourglasses to stack
numModules= 2        #Residual Modules for each hourglass
numFeats = 256      #Number of features in each hourglass
numRegModules = 2   #Depth regression modules

print("Creating Model")
model = HourglassNet3D(hourGlassToStack, numModules, numFeats,numRegModules).cuda()
print("Model Created")

这是创建模型的主要方法。 然后它调用这个方法。

class HourglassNet3D(nn.Module):

  def __init__(self, nStack, nModules, nFeats, nRegModules):
    super(HourglassNet3D, self).__init__()
    self.nStack = nStack
    self.nModules = nModules
    self.nFeats = nFeats
    self.nRegModules = nRegModules
    self.conv1_ = nn.Conv2d(3, 64, bias = True, kernel_size = 7, stride = 2, padding = 3)
   self.bn1 = nn.BatchNorm2d(64)
    self.relu = nn.ReLU(inplace = True)
    self.r1 = Residual(64, 128)
    self.maxpool = nn.MaxPool2d(kernel_size = 2, stride = 2)
    self.r4 = Residual(128, 128)
    self.r5 = Residual(128, self.nFeats)

    _hourglass, _Residual, _lin_, _tmpOut, _ll_, _tmpOut_, _reg_ = [], [], [], [], [], [], []
    for i in range(self.nStack):
      _hourglass.append(Hourglass(4, self.nModules, self.nFeats))
      for j in range(self.nModules):
        _Residual.append(Residual(self.nFeats, self.nFeats))
      lin = nn.Sequential(nn.Conv2d(self.nFeats, self.nFeats, bias = True, kernel_size = 1, stride = 1), 
                      nn.BatchNorm2d(self.nFeats), self.relu)
      _lin_.append(lin)
      _tmpOut.append(nn.Conv2d(self.nFeats, 16, bias = True, kernel_size = 1, stride = 1))
  _ll_.append(nn.Conv2d(self.nFeats, self.nFeats, bias = True, kernel_size = 1, stride = 1))
  _tmpOut_.append(nn.Conv2d(16, self.nFeats, bias = True, kernel_size = 1, stride = 1))

for i in range(4):
  for j in range(self.nRegModules):
    _reg_.append(Residual(self.nFeats, self.nFeats))

self.hourglass = nn.ModuleList(_hourglass)
self.Residual = nn.ModuleList(_Residual)
self.lin_ = nn.ModuleList(_lin_)
self.tmpOut = nn.ModuleList(_tmpOut)
self.ll_ = nn.ModuleList(_ll_)
self.tmpOut_ = nn.ModuleList(_tmpOut_)
self.reg_ = nn.ModuleList(_reg_)

self.reg = nn.Linear(4 * 4 * self.nFeats,16 )

然后这个调用这个

class Residual(nn.Module):
#set the number ofinput and output for each layer
def __init__(self, numIn, numOut):
   super(Residual, self).__init__()
   self.numIn = numIn
   self.numOut = numOut
   self.bn = nn.BatchNorm2d(self.numIn)
   self.relu = nn.ReLU(inplace = True)
   self.conv1 = nn.Conv2d(self.numIn, self.numOut / 2, bias = True, kernel_size = 1)
   self.bn1 = nn.BatchNorm2d(self.numOut / 2)
   self.conv2 = nn.Conv2d(self.numOut / 2, self.numOut / 2, bias = True, kernel_size = 3, stride = 1, padding = 1)
   self.bn2 = nn.BatchNorm2d(self.numOut / 2)
   self.conv3 = nn.Conv2d(self.numOut / 2, self.numOut, bias = True, kernel_size = 1)

   if self.numIn != self.numOut:
       self.conv4 = nn.Conv2d(self.numIn, self.numOut, bias = True, kernel_size = 1) 

所有这一切对我来说都很好,但我不知道如果我做错了我应该如何通过这个。 感谢您的帮助

【问题讨论】:

  • 你能告诉我们这个错误具体出现在哪一行吗?此外,您应该始终定义一个forward(self, x) 方法,该方法指定您如何通过网络传播;我不确定 PyTorch 的内部结构(是否需要创建网络),但它也有助于像 SO 社区这样的旁观者更好地分析您的网络。
  • 我有它们,但不想在这里放一大堆多余的代码
  • 好吧,只是想知道你有没有什么东西。

标签: python-3.x pytorch


【解决方案1】:

您可能需要注意在 Residual 类中传递给卷积层的内容。 Per default, Python 3 will convert any division operation into a float variable.

尝试将变量转换回整数,看看是否有帮助。 Residual 的固定代码:

class Residual(nn.Module):
#set the number ofinput and output for each layer

    def __init__(self, numIn, numOut):
       super(Residual, self).__init__()
       self.numIn = numIn
       self.numOut = numOut
       self.bn = nn.BatchNorm2d(self.numIn)
       self.relu = nn.ReLU(inplace = True)
       self.conv1 = nn.Conv2d(self.numIn, int(self.numOut / 2), bias = True, kernel_size = 1)
       self.bn1 = nn.BatchNorm2d(int(self.numOut / 2))
       self.conv2 = nn.Conv2d(int(self.numOut / 2), int(self.numOut / 2), bias = True, kernel_size = 3, stride = 1, padding = 1)
       self.bn2 = nn.BatchNorm2d(int(self.numOut / 2))
       self.conv3 = nn.Conv2d(int(self.numOut / 2), self.numOut, bias = True, kernel_size = 1)

       if self.numIn != self.numOut:
           self.conv4 = nn.Conv2d(self.numIn, self.numOut, bias = True, kernel_size = 1) 

【讨论】:

  • 你的老板我知道我错了什么,Residual Conv2d 需要它在 int 中,但由于 python 它将它转换为 float?]
  • 没错。由于您要指定特征的数量,因此您必须传递一个整数值。此外,这解释了上面的(某种神秘的)错误消息:在列出的“接受的类型”中,您最接近“整数元组”,但由于您还传递了一个浮点值,PyTorch 无法解释它正确。
  • 我觉得有道理。我真的很感激。
猜你喜欢
  • 2019-07-26
  • 2020-08-27
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
相关资源
最近更新 更多