Python 在另一个函数中调用函数,但变量是从另一个函数声明/实例化/初始化/赋值的
问题Python 在另一个函数中调用函数,但变量是从另一个函数声明/实例化/初始化/赋值的,python,variables,recursion,global-variables,Python,Variables,Recursion,Global Variables,问题 def a(...): model = b(...) 我正在运行一个(…),但未定义模型 b(…)看起来像: def b(...): ... model=... ... return model 我的问题:我的问题在python中被称为什么?所以我可以解决它。类似全局/局部或嵌套函数、递归、静态、函数内部调用函数,或从另一个函数声明/实例化/初始化/赋值 下面是同样的问题,但我的真实代码,因为我有谷歌它,所以我可能需要帮助我的具体情况 我跑步的内容: start_parame
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
从model=get\u instance\u segmentation\u model(num\u classes)
听起来好像您没有返回
模型
并传递它
你是说:
model=set\u train\u validation\u函数(i、k、j)
列车功能(模型)
这意味着
def set\u train\u validation\u function(…):
需要返回模型
,然后您需要def train function(model):
请更新代码的缩进。Python对缩进非常敏感,Python程序员也是如此。
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 )
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