def test_tf_static_superloss_categorical_ce(self): sl = SuperLoss(CrossEntropy(inputs=['y_pred', 'y'], outputs='ce')) sl.build(framework="tf", device=None) output = self.do_forward(sl, data=[self.tf_pred_cat, self.tf_true_cat], state=self.state) self.assertTrue(np.allclose(output.numpy(), -0.0026386082))
def test_tf_static_superloss_hinge(self): true = tf.constant([[-1, 1, 1, -1], [1, 1, 1, 1], [-1, -1, 1, -1], [1, -1, -1, -1]]) pred = tf.constant([[0.1, 0.9, 0.05, 0.05], [0.1, -0.2, 0.0, -0.7], [0.0, 0.15, 0.8, 0.05], [1.0, -1.0, -1.0, -1.0]]) sl = SuperLoss(Hinge(inputs=('x1', 'x2'), outputs='x')) sl.build('tf', None) output = self.do_forward(sl, data=[pred, true], state=self.state) self.assertTrue(np.allclose(output.numpy(), -0.072016776))
def test_torch_superloss_hinge(self): true = torch.tensor([[-1, 1, 1, -1], [1, 1, 1, 1], [-1, -1, 1, -1], [1, -1, -1, -1]]).to("cuda:0" if torch.cuda.is_available() else "cpu") pred = torch.tensor([[0.1, 0.9, 0.05, 0.05], [0.1, -0.2, 0.0, -0.7], [0.0, 0.15, 0.8, 0.05], [1.0, -1.0, -1.0, -1.0]]).to("cuda:0" if torch.cuda.is_available() else "cpu") sl = SuperLoss(Hinge(inputs=('x1', 'x2'), outputs='x')) sl.build('torch', "cuda:0" if torch.cuda.is_available() else "cpu") output = sl.forward(data=[pred, true], state=self.state) self.assertTrue(np.allclose(output.to("cpu").numpy(), -0.072016776))
def test_torch_superloss_binary_ce(self): sl = SuperLoss(CrossEntropy(inputs=['y_pred', 'y'], outputs='ce')) sl.build(framework="torch", device="cuda:0" if torch.cuda.is_available() else "cpu") output = sl.forward( data=[self.torch_pred_binary, self.torch_true_binary], state=self.state) self.assertTrue( np.allclose(output.detach().to("cpu").numpy(), -0.0026238672))
def test_tf_superloss_sparse_categorical_ce(self): sl = SuperLoss(CrossEntropy(inputs=['y_pred', 'y'], outputs='ce')) sl.build(framework="tf", device=None) output = sl.forward(data=[self.tf_pred_sparse, self.tf_true_sparse], state=self.state) self.assertTrue(np.allclose(output.numpy(), -0.024740249))