Beispiel #1
0
def test_rff_feature_map():
    x = 3.
    u = 2.
    omega = 2.
    phi = rff_feature_map_single(x, omega, u)
    phi_manual = np.cos(omega * x + u) * np.sqrt(2.)
    assert_close(phi, phi_manual)
def test_rff_feature_map():
    x = 3.
    u = 2.
    omega = 2.
    phi = rff_feature_map_single(x, omega, u)
    phi_manual = np.cos(omega * x + u) * np.sqrt(2.)
    assert_close(phi, phi_manual)
    def log_pdf(self, x):
        if self.theta is None:
            raise RuntimeError("Model not fitted yet.")
        assert_array_shape(x, ndim=1, dims={0: self.D})

        phi = rff_feature_map_single(x, self.omega, self.u)
        return np.dot(phi, self.theta)
Beispiel #4
0
 def log_pdf(self, x):
     if self.theta is None:
         raise RuntimeError("Model not fitted yet.")
     assert_array_shape(x, ndim=1, dims={0: self.D})
     
     phi = rff_feature_map_single(x, self.omega, self.u)
     return np.dot(phi, self.theta)
def test_rff_feature_map_single_equals_feature_map():
    N = 10
    D = 20
    m = 3
    X = np.random.randn(N, D)
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    
    phis = rff_feature_map(X, omega, u)
    
    for i, x in enumerate(X):
        phi = rff_feature_map_single(x, omega, u)
        assert_allclose(phis[i], phi)
Beispiel #6
0
def test_rff_feature_map_single_equals_feature_map():
    N = 10
    D = 20
    m = 3
    X = np.random.randn(N, D)
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)

    phis = rff_feature_map(X, omega, u)

    for i, x in enumerate(X):
        phi = rff_feature_map_single(x, omega, u)
        assert_allclose(phis[i], phi)
def test_rff_feature_map_comp_theano_result_equals_manual():
    if not theano_available:
        raise SkipTest("Theano not available.")
    
    D = 2
    x = np.random.randn(D)
    m = 10
    sigma = 1.
    omega, u = rff_sample_basis(D, m, sigma)
    
    phi_manual = rff_feature_map_single(x, omega, u)
    for i in range(m):
        # phi_manual is a monte carlo average, so have to normalise by np.sqrt(m) here
        phi = rff_feature_map_comp_theano(x, omega[:, i], u[i]) / np.sqrt(m)
        assert_close(phi, phi_manual[i])
Beispiel #8
0
def test_rff_feature_map_comp_theano_result_equals_manual():
    if not theano_available:
        raise SkipTest("Theano not available.")

    D = 2
    x = np.random.randn(D)
    m = 10
    sigma = 1.
    omega, u = rff_sample_basis(D, m, sigma)

    phi_manual = rff_feature_map_single(x, omega, u)
    for i in range(m):
        # phi_manual is a monte carlo average, so have to normalise by np.sqrt(m) here
        phi = rff_feature_map_comp_theano(x, omega[:, i], u[i]) / np.sqrt(m)
        assert_close(phi, phi_manual[i])
Beispiel #9
0
def update_b_single(x, b, n, omega, u):
    D = omega.shape[0]
    m = omega.shape[1]

    projections_sum = np.zeros(m)
    phi = rff_feature_map_single(x, omega, u)
    for d in range(D):
        # second derivative of feature map
        phi2 = -phi * (omega[d, :]**2)
        projections_sum -= phi2

    # Knuth's running average
    n += 1
    delta = projections_sum - b
    b += delta / n

    return b
Beispiel #10
0
def update_b_single(x, b, n, omega, u):
    D = omega.shape[0]
    m = omega.shape[1]
    
    projections_sum = np.zeros(m)
    phi = rff_feature_map_single(x, omega, u)
    for d in range(D):
        # second derivative of feature map
        phi2 = -phi * (omega[d, :] ** 2)
        projections_sum -= phi2
    
    # Knuth's running average
    n += 1
    delta = projections_sum - b
    b += delta / n
    
    return b