如何在pytorch中实现NNI(神经网络智能)的平均标准误差(MSE)度量



我对pytorch有些陌生,因为我已经使用Keras好几年了。现在,我想运行基于DARTS的网络体系结构搜索(NAS(:Differentiable architecture search(请参阅https://nni.readthedocs.io/en/stable/NAS/DARTS.html)它基于pytorch。

所有可用的示例都使用精度作为度量,但我需要计算MSE。这是可用的例子之一:

DartsTrainer(model,
loss=criterion,
metrics=lambda output, target: accuracy(output, target, topk=(1,)),
optimizer=optim,
num_epochs=args.epochs,
dataset_train=dataset_train,
dataset_valid=dataset_valid,
batch_size=args.batch_size,
log_frequency=args.log_frequency,
unrolled=args.unrolled,
callbacks=[LRSchedulerCallback(lr_scheduler), ArchitectureCheckpoint("./checkpoints")]) 
# where the accuracy is defined in a separate function:
def accuracy(output, target, topk=(1,)):
# Computes the precision@k for the specified values of k
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
# one-hot case
if target.ndimension() > 1:
target = target.max(1)[1]
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = dict()
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res["acc{}".format(k)] = correct_k.mul_(1.0 / batch_size).item()
return res

正如我在pytorch中看到的,计算度量比在Keras中更复杂。有人能帮忙吗?

作为一个试验,我写了这个代码:

def accuracy_mse(output, target):
batch_size = target.size(0)

diff = torch.square(output.t()-target)/batch_size
diff = diff.sum()
res = dict()
res["acc_mse"] = diff
return res    

它似乎起作用了,但我不能100%确定。。。

最后我发现转置(.t(((是导致问题的原因,所以最后的代码是:

def accuracy_mse(output, target):

""" Computes the mse """
batch_size = target.size(0)

diff = torch.square(output-target)/batch_size
diff = diff.sum()
res = dict()
res["mse"] = diff
return res  

最新更新