Beispiel #1
0
 def probability_ratio(
     self, tag: str, x: ep.Tensor, y: ep.Tensor, step: int
 ) -> None:
     x_ = x.float32().mean(axis=0).item()
     y_ = y.float32().mean(axis=0).item()
     if y_ == 0:
         return
     self.writer.add_scalar(tag, x_ / y_, step)
Beispiel #2
0
def test_logical_and_nonboolean(t: Tensor, f: Callable[[Tensor, Tensor],
                                                       Tensor]) -> None:
    t = t.float32()
    f(t > 1, t > 1)
    with pytest.raises(ValueError):
        f(t, t > 1)
    with pytest.raises(ValueError):
        f(t > 1, t)
    with pytest.raises(ValueError):
        f(t, t)
Beispiel #3
0
def apply_decision_rule(
    decision_rule: str,
    beta: float,
    best_advs: ep.Tensor,
    best_advs_norms: ep.Tensor,
    x_k: ep.Tensor,
    x_0: ep.Tensor,
    found_advs: ep.Tensor,
):
    if decision_rule == "EN":
        norms = beta * flatten(x_k - x_0).abs().sum(
            axis=-1) + flatten(x_k - x_0).square().sum(axis=-1)
    elif decision_rule == "L1":
        norms = flatten(x_k - x_0).abs().sum(axis=-1)
    else:
        raise ValueError("invalid decision rule")

    new_best = (norms < best_advs_norms).float32() * found_advs.float32()
    new_best = atleast_kd(new_best, best_advs.ndim)
    best_advs = new_best * x_k + (1 - new_best) * best_advs
    best_advs_norms = ep.minimum(norms, best_advs_norms)

    return best_advs, best_advs_norms
Beispiel #4
0
 def probability(self, tag: str, x: ep.Tensor, step: int) -> None:
     self.writer.add_scalar(tag, x.float32().mean(axis=0).item(), step)