在另一个函数中调用一个函数,但是变量是 declared/instantiated/ 初始化/从另一个函数分配的
Call a function inside another function but a variable are declared/instantiated/ initialized/ assigned from another function
问题
def a(...):
model = b(...)
我运行宁一个(...)但模型未定义。
b(...) 看起来像:
def b(...):
...
model=...
...
return model
我的问题:我的问题在python中叫什么?所以我可以解决它。像 global/local 或嵌套函数、递归、静态、在函数内部调用函数,或 declaring/instantiating/ 从另一个函数初始化/赋值?
下面是同一个问题,但使用的是我的真实代码,因为我有 google 它,所以我的具体案例可能需要帮助。
我运行:
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 set_train_validation_function(i, k, j):
在我def start_parameter_searching(lrList, wdList, momentumList):
在 def set_train_validation_function(i, k, j):
里面我有 model = get_instance_segmentation_model(num_classes)
并且模型没有定义。
get_instance_segmentation_model(num_classes)
可能不再是 called/declared/instatiated。该函数也在另一个函数中。
所有内容都放在一个伪代码文件中
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):
问题
def a(...):
model = b(...)
我运行宁一个(...)但模型未定义。
b(...) 看起来像:
def b(...):
...
model=...
...
return model
我的问题:我的问题在python中叫什么?所以我可以解决它。像 global/local 或嵌套函数、递归、静态、在函数内部调用函数,或 declaring/instantiating/ 从另一个函数初始化/赋值?
下面是同一个问题,但使用的是我的真实代码,因为我有 google 它,所以我的具体案例可能需要帮助。
我运行:
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 set_train_validation_function(i, k, j):
在我def start_parameter_searching(lrList, wdList, momentumList):
在 def set_train_validation_function(i, k, j):
里面我有 model = get_instance_segmentation_model(num_classes)
并且模型没有定义。
get_instance_segmentation_model(num_classes)
可能不再是 called/declared/instatiated。该函数也在另一个函数中。
所有内容都放在一个伪代码文件中
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):