what.utils.proj API documentation

# what.utils.proj

View Source

     1import numpy as np
     2
     3# Deep Fool: Project on the lp ball centered at 0 and of radius xi
     4def proj_lp(v, xi=50, p=2):
     5
     6    # SUPPORTS only p = 2 and p = Inf for now
     7    if p == 2:
     8        v = v * min(1, xi/np.linalg.norm(v.flatten('C')))
     9        # v = v / np.linalg.norm(v.flatten(1)) * xi
    10    elif p == np.inf:
    11        v = np.sign(v) * np.minimum(abs(v), xi)
    12    else:
    13        raise ValueError('Values of p different from 2 and Inf are currently not supported...')
    14
    15    return v

def proj_lp(v, xi=50, p=2): View Source

     5def proj_lp(v, xi=50, p=2):
     6
     7    # SUPPORTS only p = 2 and p = Inf for now
     8    if p == 2:
     9        v = v * min(1, xi/np.linalg.norm(v.flatten('C')))
    10        # v = v / np.linalg.norm(v.flatten(1)) * xi
    11    elif p == np.inf:
    12        v = np.sign(v) * np.minimum(abs(v), xi)
    13    else:
    14        raise ValueError('Values of p different from 2 and Inf are currently not supported...')
    15
    16    return v