问题描述:
这个函数功能对应mindspore中的哪些函数,萌新在迁移的过程中遇到了这个函数 在api映射中没有发现这个函数的迁移,想知道解决问题的相关知识和方法。图中的三个函数在库中都没有找到迁移,就是F后面的函数。
解答:
1. torch.nn.functional.interpolate()和upsamle()可以考虑下使用 MindSpore 的 nn.ResizeBilinear 接口:
比较与torch.nn.Upsample的功能差异 — MindSpore master documentation
2. F.binary_cross_entropy_with_logits可以使用这段代码,后续会提交到开源仓。
def binary_cross_entropy_with_logits(input, target, weight=None, reduction='mean', pos_weight=None):
max_val = ops.maximum(-input, 0)
if pos_weight is not None:
log_weight = ((pos_weight - 1) * target) + 1
loss = (1 - target) * input
loss_1 = ops.log(ops.exp(-max_val) + ops.exp(-input - max_val)) + max_val
loss += log_weight * loss_1
else:
loss = (1 - target) * input
loss += max_val
loss += ops.log(ops.exp(-max_val) + ops.exp(-input - max_val))
if weight is not None:
output = loss * weight
else:
output = loss
if reduction == "mean":
return ops.reduce_mean(output)
elif reduction == "sum":
return ops.reduce_sum(output)
else:
return output