예제 #1
0
def test_gaussian_log_sample():
    del_shared()
    random_state = np.random.RandomState(1999)
    mu = linear([X_sym], [X.shape[1]],
                proj_dim=100,
                name='mu',
                random_state=random_state)
    sigma = linear([X_sym], [X.shape[1]],
                   proj_dim=100,
                   name='sigma',
                   random_state=random_state)
    random_state = np.random.RandomState(1999)
    r1 = gaussian_log_sample([mu], [sigma],
                             name="samp1",
                             random_state=random_state)
    random_state = np.random.RandomState(1999)
    r2 = gaussian_log_sample([mu], [sigma],
                             name="samp2",
                             random_state=random_state)
    random_state = np.random.RandomState(42)
    r3 = gaussian_log_sample([mu], [sigma],
                             name="samp3",
                             random_state=random_state)
    sample_function = theano.function([X_sym], [r1, r2, r3],
                                      mode="FAST_COMPILE")
    s_r1, s_r2, s_r3 = sample_function(X[:100])

    assert_almost_equal(s_r1, s_r2)
    assert_raises(AssertionError, assert_almost_equal, s_r1, s_r3)

    ss_r1, ss_r2, ss_r3 = sample_function(X[:100])
    assert_raises(AssertionError, assert_almost_equal, s_r1, ss_r1)
예제 #2
0
def test_gaussian_log_sample():
    del_shared()
    random_state = np.random.RandomState(1999)
    mu = linear([X_sym], [X.shape[1]], proj_dim=100, name='mu',
                random_state=random_state)
    sigma = linear([X_sym], [X.shape[1]], proj_dim=100, name='sigma',
                   random_state=random_state)
    random_state = np.random.RandomState(1999)
    r1 = gaussian_log_sample([mu], [sigma], name="samp1",
                             random_state=random_state)
    random_state = np.random.RandomState(1999)
    r2 = gaussian_log_sample([mu], [sigma], name="samp2",
                             random_state=random_state)
    random_state = np.random.RandomState(42)
    r3 = gaussian_log_sample([mu], [sigma], name="samp3",
                             random_state=random_state)
    sample_function = theano.function([X_sym], [r1, r2, r3],
                                      mode="FAST_COMPILE")
    s_r1, s_r2, s_r3 = sample_function(X[:100])

    assert_almost_equal(s_r1, s_r2)
    assert_raises(AssertionError, assert_almost_equal, s_r1, s_r3)

    ss_r1, ss_r2, ss_r3 = sample_function(X[:100])
    assert_raises(AssertionError, assert_almost_equal, s_r1, ss_r1)
예제 #3
0
width = 28
height = 28
n_input = width * height

# encode path aka q
l1_enc = softplus([X_sym], [X.shape[1]], proj_dim=n_hid, name='l1_enc',
                  random_state=random_state)
l2_enc = softplus([l1_enc], [n_hid], proj_dim=n_hid, name='l2_enc',
                  random_state=random_state)
code_mu = linear([l2_enc], [n_hid], proj_dim=n_code, name='code_mu',
                 random_state=random_state)
code_log_sigma = linear([l2_enc], [n_hid], proj_dim=n_code,
                        name='code_log_sigma', random_state=random_state)
kl = gaussian_log_kl([code_mu], [code_log_sigma]).mean()
sample_state = np.random.RandomState(2177)
samp = gaussian_log_sample([code_mu], [code_log_sigma], name='samp',
                           random_state=sample_state)

# decode path aka p
l1_dec = softplus([samp], [n_code], proj_dim=n_hid, name='l1_dec',
                  random_state=random_state)
l2_dec = softplus([l1_dec], [n_hid], proj_dim=n_hid, name='l2_dec',
                  random_state=random_state)
out = sigmoid([l2_dec], [n_hid], proj_dim=X.shape[1], name='out',
              random_state=random_state)

nll = binary_crossentropy(out, X_sym).mean()
# See https://arxiv.org/pdf/1406.5298v2.pdf, eq 5
# log p(x | z) = -nll so swap sign
# want to minimize cost in optimization so multiply by -1
# cost = -1 * (-nll - kl)
cost = nll + kl
예제 #4
0
l2_enc = softplus([l1_enc], [n_hid],
                  proj_dim=n_hid,
                  name='l2_enc',
                  random_state=random_state)
code_mu = linear([l2_enc], [n_hid],
                 proj_dim=n_code,
                 name='code_mu',
                 random_state=random_state)
code_log_sigma = linear([l2_enc], [n_hid],
                        proj_dim=n_code,
                        name='code_log_sigma',
                        random_state=random_state)
kl = gaussian_log_kl([code_mu], [code_log_sigma]).mean()
sample_state = np.random.RandomState(2177)
samp = gaussian_log_sample([code_mu], [code_log_sigma],
                           name='samp',
                           random_state=sample_state)

# decode path aka p
l1_dec = softplus([samp], [n_code],
                  proj_dim=n_hid,
                  name='l1_dec',
                  random_state=random_state)
l2_dec = softplus([l1_dec], [n_hid],
                  proj_dim=n_hid,
                  name='l2_dec',
                  random_state=random_state)
out = sigmoid([l2_dec], [n_hid],
              proj_dim=X.shape[1],
              name='out',
              random_state=random_state)