def test_nll_loss2d_sum(self): inputs_ms = mindspore.Tensor(self.inputs_2d, mindspore.float32) target_ms = mindspore.Tensor(self.target_2d, mindspore.int32) res_ms = nll_loss_ms(inputs_ms, target_ms, reduction='sum') inputs_pt = torch.tensor(self.inputs_2d) target_pt = torch.tensor(self.target_2d) res_pt = nll_loss_pt(inputs_pt, target_pt, reduction='sum') assert np.allclose(res_ms.asnumpy(), res_pt, 1e-3, 1e-3)
def test_nll_loss_mean_ignore_index(self): inputs_ms = mindspore.Tensor(self.inputs, mindspore.float32) target_ms = mindspore.Tensor(self.target, mindspore.int32) res_ms = nll_loss_ms(inputs_ms, target_ms, ignore_index=0) inputs_pt = torch.tensor(self.inputs) target_pt = torch.tensor(self.target) res_pt = nll_loss_pt(inputs_pt, target_pt, ignore_index=0) assert np.allclose(res_ms.asnumpy(), res_pt.numpy(), 1e-3, 1e-3)
def test_nll_loss2d(self): inputs_ms = mindspore.Tensor(self.inputs_2d, mindspore.float32) target_ms = mindspore.Tensor(self.target_2d, mindspore.int32) res_ms = nll_loss_ms(inputs_ms, target_ms, reduction='none') inputs_pt = torch.tensor(self.inputs_2d) target_pt = torch.tensor(self.target_2d) res_pt = nll_loss_pt(inputs_pt, target_pt, reduction='none') print(res_ms.shape, res_pt.shape) assert np.allclose(res_ms.asnumpy(), res_pt, atol=1e-3)
def test_nll_loss_sum_with_weight(self): inputs_ms = mindspore.Tensor(self.inputs, mindspore.float32) target_ms = mindspore.Tensor(self.target, mindspore.int32) weight_ms = mindspore.Tensor(self.weight, mindspore.float32) res_ms = nll_loss_ms(inputs_ms, target_ms, reduction='none', weight=weight_ms) inputs_pt = torch.tensor(self.inputs) target_pt = torch.tensor(self.target) weight_pt = torch.tensor(self.weight) res_pt = nll_loss_pt(inputs_pt, target_pt, reduction='none', weight=weight_pt) assert np.allclose(res_ms.asnumpy(), res_pt.numpy(), 1e-3, 1e-3)