调用另一个函数内部的函数,但变量是从另一个功能声明/实例化/初始化/分配的



问题

def a(...):
model = b(...)

我正在运行一个(…(,但模型尚未定义。

b(…(看起来像:

def b(...):
... 
model=...
...
return model

我的问题:在python中,我的问题叫什么?所以我可以解决它。比如全局/局部或嵌套函数、递归、静态、调用函数内部的函数,或者从另一个函数声明/实例化/初始化/赋值?

下面是同样的问题,但我的真实代码,因为我已经用谷歌搜索过了,所以我可能需要帮助我的具体案例。

我运行的内容:

start_parameter_searching(lrList, momentumList, wdList )

功能:

def start_parameter_searching(lrList, wdList, momentumList):
for i in lrList:
for k in momentumList:
for j in wdListt:
set_train_validation_function(i, k, j)
trainFunction()
lrList = [0.001, 0.01, 0.1]
wdList = [0.001, 0.01, 0.1]
momentumList = [0.001, 0.01, 0.1]

错误

NameError                                 Traceback (most recent call last)
<ipython-input-20-1d7a642788ca> in <module>()
----> 1 start_parameter_searching(lrList, momentumList, wdList)
1 frames
<ipython-input-17-cd25561c1705> in trainFunction()
10   for epoch in range(num_epochs):
11       # train for one epoch, printing every 10 iterations
---> 12       _, loss = train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
13       # update the learning rate
14       lr_scheduler.step()
NameError: name 'model' is not defined

问题

我在def start_parameter_searching(lrList, wdList, momentumList):中运行def set_train_validation_function(i, k, j):

def set_train_validation_function(i, k, j):内部,我有model = get_instance_segmentation_model(num_classes),并且模型没有定义。get_instance_segmentation_model(num_classes)可能没有被再次调用/声明/初始化。该函数也在另一个函数中。

所有东西都放在一个伪代码文件中

def set_train_validation_function(i, k, j):
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# our dataset has two classes only - background and person
num_classes = 2
# get the model using our helper function
model = get_instance_segmentation_model(num_classes)
# move model to the right device
model.to(device)
# construct an optimizer
params = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=i,
momentum=k, weight_decay=j)
# and a learning rate scheduler which decreases the learning rate by
# 10x every 3 epochs
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=3,
gamma=0.1)

def start_parameter_searching(lrList, wdList, momentumList):
for i in lrList:
for k in momentumList:
for j in wdListt:
set_train_validation_function(i, k, j)
trainFunction()
lrList = [0.001, 0.01, 0.1]
wdList = [0.001, 0.01, 0.1]
momentumList = [0.001, 0.01, 0.1]
#start training
start_parameter_searching(lrList, momentumList, wdList )

以及model = get_instance_segmentation_model(num_classes)的问题

def get_instance_segmentation_model(num_classes):
# load an instance segmentation model pre-trained on COCO
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
# get the number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
# now get the number of input features for the mask classifier
in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
hidden_layer = 256
# and replace the mask predictor with a new one
model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
hidden_layer,
num_classes)
return model

听起来你没有返回model并传递它。

你的意思是:

model = set_train_validation_function(i, k, j)
trainFunction(model)

这意味着def set_train_validation_function(...):需要return model,然后您需要def trainFunction(model):

相关内容

  • 没有找到相关文章

最新更新