TypeError: 'Net' object is not callable python报错

水深无声 2022-01-31 04:35 399阅读 0赞

原程序如下:

  1. import torch
  2. import torch.nn as nn
  3. class Net():
  4. def __init__(self,inc,k):
  5. super().__init__()
  6. self.net = nn.Sequential(
  7. nn.Conv2d(inc,inc,k,2,1)
  8. )
  9. def forward(self,x):
  10. return self.net(x)
  11. class UpsampleLayer(torch.nn.Module):
  12. def __init__(self):
  13. super(UpsampleLayer, self).__init__()
  14. self.pixelShuffle = torch.nn.PixelShuffle(2)
  15. def forward(self, x):
  16. return self.pixelShuffle(x)
  17. if __name__ == '__main__':
  18. x = torch.Tensor(1,4,100,100)
  19. net1 = Net(4,3)
  20. y = net1(x)
  21. up = UpsampleLayer()
  22. y1 = up(y)
  23. print(y1)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5OTM4NjY2_size_16_color_FFFFFF_t_70

按照网上常见的说法是命名冲突问题,但是我只是一个如此简单的demo,又没有占用关键字,怎么可能是命名问题。于是在第26行把 y = net1(x) 改为 y = net1.forward(x) ,告诉这个网络用其内的什么函数,就好了。。。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5OTM4NjY2_size_16_color_FFFFFF_t_70 1

又如

  1. import torch
  2. import torch.nn as nn
  3. class Net():
  4. def __init__(self,inc,k):
  5. super().__init__()
  6. self.net = nn.Sequential(
  7. nn.Conv2d(inc,inc,k,2,1)
  8. )
  9. def forward(self,x):
  10. return self.net(x)
  11. class UpsampleLayer(torch.nn.Module):
  12. def __init__(self):
  13. super().__init__()
  14. self.pixelShuffle = torch.nn.PixelShuffle(2)
  15. def forward(self, x):
  16. return self.pixelShuffle(x)
  17. class MainNet():
  18. def __init__(self):
  19. super().__init__()
  20. self.net1 = Net(4, 3)
  21. self.up = UpsampleLayer()
  22. def forward(self,x):
  23. y = self.net1.forward(x)
  24. return self.up(y)
  25. if __name__ == '__main__':
  26. x = torch.Tensor(1,4,100,100)
  27. mnet = MainNet()
  28. print(mnet.forward(x))

发表评论

表情:
评论列表 (有 0 条评论,399人围观)

还没有评论,来说两句吧...

相关阅读