ecursionError: maximum recursion depth exceeded while calling a Python object
在 Python 中,`RecursionError: maximum recursion depth exceeded` 错误通常发生在一个函数递归调用自身太多次,超出了 Python 默认的递归深度限制时。Python 默认的递归深度限制是比较低的,以避免无限递归导致的栈溢出。
在你的代码中,这个错误可能发生在 `_get_fc_input_size` 方法里。这个方法在构造函数中被调用,并且它试图创建一个 `AlexNet` 实例来计算全连接层的输入尺寸。但是,由于 `_get_fc_input_size` 方法本身在 `AlexNet` 类定义中,这将导致无限递归调用:
def _get_fc_input_size(self, shape):
model = AlexNet() # 这里创建了一个新的 AlexNet 实例,它又将调用 _get_fc_input_size 方法
model.eval()
with torch.no_grad():
x = torch.randn(shape)
x = model.conv(x)
return x.view(x.size(0), -1).size(1)
要解决这个问题,你应该避免在 `_get_fc_input_size` 方法中创建新的 `AlexNet` 实例。相反,你应该使用当前实例的 `conv` 属性来计算输出尺寸。以下是修正后的代码:
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
# ... [省略其他部分] ...
# 计算全连接层输入大小
self.fc_input_size = self._get_fc_input_size()
self.fc = nn.Sequential(
# ... [省略其他部分] ...
)
def forward(self, img):
feature = self.conv(img)
feature = feature.view(img.size(0), -1) # 展平特征图
output = self.fc(feature)
return output
def _get_fc_input_size(self):
# 使用当前实例的 conv 属性来计算输出尺寸
x = torch.randn(1, 1, 28, 28) # 假设输入图像尺寸是 (1, 28, 28)
x = self.conv(x)
# 计算展平后的特征数量
return x.view(-1).size(0)
# 创建模型
model = AlexNet().to(device)
# ... [省略其他部分] ...
在这个修正中,`_get_fc_input_size` 方法不再创建新的 `AlexNet` 实例,而是直接使用 `self.conv` 来获取卷积层的输出,然后计算这个输出在展平后的大小。这样,递归调用的问题就解决了。