bojone/vae

use about np.interp

wangherr opened this issue · 0 comments

# [vae](https://github.com/bojone/vae)/vae_vmf_keras.py

def sampling(mu):
    """vMF分布重参数操作
    """
    dims = K.int_shape(mu)[-1]
    # 预先计算一批w
    epsilon = 1e-7
    x = np.arange(-1 + epsilon, 1, epsilon)
    y = kappa * x + np.log(1 - x**2) * (dims - 3) / 2
    y = np.cumsum(np.exp(y - y.max()))
    y = y / y[-1]
    W = K.constant(np.interp(np.random.random(10**6), y, x))
    # 实时采样w
    idx = K.random_uniform(K.shape(mu[:, :1]), 0, 10**6, dtype='int32')
    w = K.gather(W, idx)
    # 实时采样z
    eps = K.random_normal(K.shape(mu))
    nu = eps - K.sum(eps * mu, axis=1, keepdims=True) * mu
    nu = K.l2_normalize(nu, axis=-1)
    return w * mu + (1 - w**2)**0.5 * nu

In numpy's docs (https://numpy.org/doc/stable/reference/generated/numpy.interp.html),
numpy.interp(x, xp, fp, left=None, right=None, period=None)

While in the code there is np.interp(np.random.random(10**6), y, x),
should it be np.interp(np.random.random(10**6), x, y)?