错误不一致:TypeError:super(type,obj):obj必须是类型的实例或子类型

时间:2019-05-31 07:36:15

标签: python pytorch

我有一个包含以下两个类的python代码。

dout.writeBytes(str);

我使用相同的代码实例化了两个类。 import torch import torch.nn as nn import torch.nn.functional as F class QNet_baseline(nn.Module): """ A MLP with 2 hidden layer observation_dim (int): number of observation features action_dim (int): Dimension of each action seed (int): Random seed """ def __init__(self, observation_dim, action_dim, seed): super(QNet_baseline, self).__init__() self.seed = torch.manual_seed(seed) self.fc1 = nn.Linear(observation_dim, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64, action_dim) def forward(self, observations): """ Forward propagation of neural network """ x = F.relu(self.fc1(observations)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x class QNet_3hidden(nn.Module): """ A MLP with 3 hidden layer observation_dim (int): number of observation features action_dim (int): Dimension of each action seed (int): Random seed """ def __init__(self, observation_dim, action_dim, seed): super(QNet_3hidden, self).__init__() self.seed = torch.manual_seed(seed) self.fc1 = nn.Linear(observation_dim, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64, 64) self.fc4 = nn.Linear(64, action_dim) def forward(self, observations): """ Forward propagation of neural network """ x = F.relu(self.fc1(observations)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return x 可以正常工作,但是我遇到了QNet_baseline的以下错误。为什么QNet_3hidden可以工作,但是QNet_baseline却有错误?我在这里想念什么?谢谢!

QNet_3hidden

此外,以下是两个类的实例化方式:

/home/workspace/QNetworks.py in __init__(self, observation_dim, action_dim, seed)
     44 
     45     def __init__(self, observation_dim, action_dim, seed):
---> 46         super(QNet_3hidden, self).__init__()
     47         self.seed = torch.manual_seed(seed)
     48         self.fc1 = nn.Linear(observation_dim, 128)

TypeError: super(type, obj): obj must be an instance or subtype of type

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,完全重新启动内核有帮助。 正如此Comment by keitakurita中的建议:

<块引用>

您是否在 Jupyter notebook 中运行代码并且没有重新启动内核?如果是这样,则您的内核可能引用了错误的类。

我怀疑这可能是我重写类后遇到错误的原因。

这也可以解释为什么这是一个难以重现的错误。以下是类似问题的列表,以帮助跟踪相同的问题: