Ejemplo n.º 1
0
def test_get_metric_fn():
    """Tests probflow.utils.metrics.get_metric_fn"""

    metric_fn = metrics.get_metric_fn("f1")

    # Predictive dist
    probs = tf.constant([0, 1, 1, 1, 1, 0], dtype=tf.float32)
    pred_dist = Bernoulli(probs=probs)
    y_true = np.array([1, 0, 1, 1, 0, 0]).astype("float32")

    # Compare metric
    ppv = 2 / 4
    tpr = 2 / 3
    f1 = 2 * (ppv * tpr) / (ppv + tpr)
    assert is_close(metric_fn(y_true, pred_dist.mean()), f1)

    # Should be able to pass a callable
    metric_fn = metrics.get_metric_fn(lambda x, y: 3)
    assert metric_fn(y_true, pred_dist.mean()) == 3

    # Should raise a type error if passed anything else
    with pytest.raises(TypeError):
        metrics.get_metric_fn(3)
    with pytest.raises(TypeError):
        metrics.get_metric_fn([1, 2, 3])
    with pytest.raises(TypeError):
        metrics.get_metric_fn({"apples": 1, "oranges": 2})

    # And value error if invalid string
    with pytest.raises(ValueError):
        metrics.get_metric_fn("asdf")
Ejemplo n.º 2
0
def test_accuracy():
    """Tests probflow.utils.metrics.accuracy"""

    # Predictive dist
    probs = tf.constant([1, 1, 1, 1, 1, 1], dtype=tf.float32)
    pred_dist = Bernoulli(probs=probs)
    y_true = np.array([1, 0, 1, 1, 0, 1]).astype("float32")

    # Compare metric
    assert is_close(metrics.accuracy(y_true, pred_dist.mean()), 2.0 / 3.0)
Ejemplo n.º 3
0
def test_precision():
    """Tests probflow.utils.metrics.precision"""

    # Predictive dist
    probs = tf.constant([1, 1, 1, 1, 1, 0], dtype=tf.float32)
    pred_dist = Bernoulli(probs=probs)
    y_true = np.array([1, 0, 1, 1, 0, 0]).astype("float32")

    # Compare metric
    assert is_close(metrics.precision(y_true, pred_dist.mean()), 3.0 / 5.0)
Ejemplo n.º 4
0
def test_true_positive_rate():
    """Tests probflow.utils.metrics.true_positive_rate"""

    # Predictive dist
    probs = tf.constant([1, 1, 1, 1, 1, 0], dtype=tf.float32)
    pred_dist = Bernoulli(probs=probs)
    y_true = np.array([1, 0, 1, 1, 0, 1]).astype("float32")

    # Compare metric
    assert is_close(metrics.true_positive_rate(y_true, pred_dist.mean()), 0.75)
Ejemplo n.º 5
0
def test_f1_score():
    """Tests probflow.utils.metrics.f1_score"""

    # Predictive dist
    probs = tf.constant([0, 1, 1, 1, 1, 0], dtype=tf.float32)
    pred_dist = Bernoulli(probs=probs)
    y_true = np.array([1, 0, 1, 1, 0, 0]).astype("float32")

    # Compare metric
    ppv = 2 / 4
    tpr = 2 / 3
    f1 = 2 * (ppv * tpr) / (ppv + tpr)
    assert is_close(metrics.f1_score(y_true, pred_dist.mean()), f1)
Ejemplo n.º 6
0
 def __call__(self, x):
     return Bernoulli(x @ self.weight() + self.bias())
Ejemplo n.º 7
0
 def __call__(self, x):
     x = to_tensor(x)
     return Bernoulli(x @ self.weight() + self.bias())
Ejemplo n.º 8
0
def test_Bernoulli():
    """Tests Bernoulli distribution"""

    # Create the distribution
    dist = Bernoulli(0)

    # Check default params
    assert dist.logits == 0
    assert dist.probs is None

    # Call should return backend obj
    assert isinstance(dist(), tfd.Bernoulli)

    # Test methods
    assert is_close(dist.prob(0).numpy(), 0.5)
    assert is_close(dist.prob(1).numpy(), 0.5)
    assert is_close(dist.log_prob(0).numpy(), np.log(0.5))
    assert is_close(dist.log_prob(1).numpy(), np.log(0.5))
    assert dist.mean().numpy() == 0.5

    # Test sampling
    samples = dist.sample()
    assert isinstance(samples, tf.Tensor)
    assert samples.ndim == 0
    samples = dist.sample(10)
    assert isinstance(samples, tf.Tensor)
    assert samples.ndim == 1
    assert samples.shape[0] == 10

    # Should be able to set params
    dist = Bernoulli(probs=0.8)
    assert dist.probs == 0.8
    assert dist.logits is None
    assert is_close(dist.prob(0).numpy(), 0.2)
    assert is_close(dist.prob(1).numpy(), 0.8)

    # But only with Tensor-like objs
    with pytest.raises(TypeError):
        dist = Bernoulli("lalala")
    with pytest.raises(TypeError):
        dist = Bernoulli()
Ejemplo n.º 9
0
 def __call__(self, x):
     return Bernoulli(x @ self.w())
Ejemplo n.º 10
0
 def __call__(self, x):
     return Bernoulli(torch.tensor(x) @ self.w())
Ejemplo n.º 11
0
def test_Bernoulli():
    """Tests Bernoulli distribution"""

    # Create the distribution
    dist = Bernoulli(0)

    # Check default params
    assert dist.logits == 0
    assert dist.probs is None

    # Call should return backend obj
    assert isinstance(dist(), tod.bernoulli.Bernoulli)

    # Test methods
    zero = torch.zeros([1])
    one = torch.ones([1])
    assert is_close(dist.prob(zero).numpy(), 0.5)
    assert is_close(dist.prob(one).numpy(), 0.5)
    assert is_close(dist.log_prob(zero).numpy(), np.log(0.5))
    assert is_close(dist.log_prob(one).numpy(), np.log(0.5))
    assert dist.mean().numpy() == 0.5

    # Test sampling
    samples = dist.sample()
    assert isinstance(samples, torch.Tensor)
    assert samples.ndim == 0
    samples = dist.sample(10)
    assert isinstance(samples, torch.Tensor)
    assert samples.ndim == 1
    assert samples.shape[0] == 10

    # Need to pass tensor-like probs
    with pytest.raises(TypeError):
        dist = Bernoulli("lalala")

    # Should be able to set params
    dist = Bernoulli(probs=0.8)
    assert dist.probs == 0.8
    assert dist.logits is None
    assert is_close(dist.prob(zero).numpy(), 0.2)
    assert is_close(dist.prob(one).numpy(), 0.8)