Beispiel #1
0
def test_sparsemax_loss_constructor_aginst_numpy(dtype):
    """check sparsemax-loss construcor against numpy."""
    random = np.random.RandomState(1)

    z = random.uniform(low=-3, high=3, size=(test_obs, 10))
    q = np.zeros((test_obs, 10))
    q[np.arange(0, test_obs), random.randint(0, 10, size=test_obs)] = 1

    loss_object = SparsemaxLoss()
    tf_loss_op = loss_object(q, z)
    np_loss = np.mean(_np_sparsemax_loss(z, q).astype(dtype))

    test_utils.assert_allclose_according_to_type(np_loss, tf_loss_op)
    assert np_loss.shape == tf_loss_op.shape
Beispiel #2
0
    def test_sparsemax_loss_constructor_aginst_numpy(self, dtype=None):
        """check sparsemax-loss construcor against numpy."""
        random = np.random.RandomState(1)

        z = random.uniform(low=-3, high=3, size=(test_obs, 10))
        q = np.zeros((test_obs, 10))
        q[np.arange(0, test_obs), random.randint(0, 10, size=test_obs)] = 1

        loss_object = SparsemaxLoss()
        tf_loss_op = loss_object(q, z)
        tf_loss_out = self.evaluate(tf_loss_op)
        np_loss = np.mean(_np_sparsemax_loss(z, q).astype(dtype))

        self.assertAllCloseAccordingToType(np_loss, tf_loss_out)
        self.assertShapeEqual(np_loss, tf_loss_op)
Beispiel #3
0
 def test_sparsemax_loss_constructor_not_from_logits(self, dtype=None):
     """check sparsemax-loss construcor throws when from_logits=True."""
     self.assertRaises(ValueError, lambda: SparsemaxLoss(from_logits=False))
Beispiel #4
0
def test_sparsemax_loss_constructor_not_from_logits(dtype):
    """check sparsemax-loss construcor throws when from_logits=True."""
    with pytest.raises(ValueError):
        SparsemaxLoss(from_logits=False)