AttributeError: ‘xxx‘ object has no attribute ‘__bases__‘ 问题解决

问题描述:
这是我的代码,本体为一个pytorch模型,希望通过查看父类继承确定是否为网络模型。

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 3, 1)
        self.conv2 = nn.Conv2d(32, 64, 3, 1)
        self.dropout1 = nn.Dropout2d(0.25)
        self.dropout2 = nn.Dropout2d(0.5)
        self.fc1 = nn.Linear(9216, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output
        
net = Net()
print(net.__bases__)

结果报错:该类没有__bases__属性(?这个属性不是每个类都有的吗?当时头顶两个问号)

Traceback (most recent call last):
  File "C:/Users/Ming/Desktop/user_project/Test2.py", line 7, in <module>
    print(net.__bases__)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1131, in __getattr__
    type(self).__name__, name))
AttributeError: 'Net' object has no attribute '__bases__'

后来仔细研究发现了,取__bases__属性,首先要唤出该类定义,也就是需要添加__class__,具体如下:

net = Net()
print(net.__class__.__bases__)

得出结果:

(<class 'torch.nn.modules.module.Module'>,)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值