Skip to content

Commit

Permalink
Remove legacy variational code, update to 0.3.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
thjashin committed Mar 8, 2018
1 parent 5fe0618 commit 8146247
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 362 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ please clone the repository and run
pip install .
```
in the main directory. This will install ZhuSuan and its dependencies
automatically. ZhuSuan also requires Tensorflow version 1.0 or later. Because
automatically. ZhuSuan also requires **Tensorflow 1.4.0 or later**. Because
users should choose whether to install the cpu or gpu version of Tensorflow,
we do not include it in the dependencies. See
[Installing Tensorflow](https://www.tensorflow.org/install/).
Expand Down Expand Up @@ -80,6 +80,8 @@ pip install ".[examples]"
[HMC](examples/topic_models/lntm_mcem.py)
* Probabilistic Matrix Factorization on MovieLens 1M:
[HMC](examples/probabilistic_matrix_factorization/logistic_pmf_hmc.py)
* Sparse Variational Gaussian Process:
[SGVB](examples/gaussian_process/svgp.py)

## Citing ZhuSuan

Expand Down
3 changes: 1 addition & 2 deletions examples/gaussian_process/svgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# -*- coding: utf-8 -*-

"""
Stochastic sparse variational Gaussian process (SVGP) with normal approximation
for posterior.
Stochastic variational inference for sparse Gaussian process (SVGP).
For the formulation you can refer to, e.g., Section 2.1 of the following paper:
Expand Down
13 changes: 9 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
# To use a consistent encoding
from codecs import open
from os import path
import re

here = path.abspath(path.dirname(__file__))

# Get the version string
with open(path.join(here, 'zhusuan', '__init__.py'), encoding='utf-8') as f:
version = re.search(r'__version__ = \'(.*?)\'', f.read()).group(1)

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
readme = f.read()

setup(
name='zhusuan',

# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.3.0',
version=version,

description='A Library for Generative Models',
long_description=long_description,
description='A Library for Bayesian Deep Learning',
long_description=readme,

# The project's main homepage.
url='https://github.com/thu-ml/zhusuan',
Expand Down
20 changes: 10 additions & 10 deletions tests/distributions/test_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from contextlib import contextmanager
import tensorflow as tf
import numpy as np
from scipy import stats, misc, special
from scipy import stats
from scipy.special import logsumexp, factorial, gammaln

from tests.distributions import utils
from zhusuan.distributions.multivariate import *
Expand Down Expand Up @@ -230,11 +231,11 @@ def _test_value(logits, n_experiments, given, normalize_logits):

maybe_normalized_logits = logits
if normalize_logits:
maybe_normalized_logits -= misc.logsumexp(
maybe_normalized_logits -= logsumexp(
logits, axis=-1, keepdims=True)
n_experiments = np.sum(given, axis=-1)
target_log_p = np.log(misc.factorial(n_experiments)) - \
np.sum(np.log(misc.factorial(given)), -1) + \
target_log_p = np.log(factorial(n_experiments)) - \
np.sum(np.log(factorial(given)), -1) + \
np.sum(given * maybe_normalized_logits, -1)
self.assertAllClose(log_p.eval(), target_log_p)
p = dist.prob(given)
Expand Down Expand Up @@ -334,7 +335,7 @@ def _test_value(logits, given, normalize_logits):

maybe_normalized_logits = logits
if normalize_logits:
maybe_normalized_logits -= misc.logsumexp(
maybe_normalized_logits -= logsumexp(
logits, axis=-1, keepdims=True)
target_log_p = np.sum(given * maybe_normalized_logits, -1)
self.assertAllClose(log_p.eval(), target_log_p)
Expand Down Expand Up @@ -414,7 +415,7 @@ def test_value(self):
with self.test_session(use_gpu=True):
def _test_value(logits, given):
logits = np.array(logits, np.float32)
normalized_logits = logits - misc.logsumexp(
normalized_logits = logits - logsumexp(
logits, axis=-1, keepdims=True)
given = np.array(given, np.int32)
cat = OnehotCategorical(logits)
Expand Down Expand Up @@ -510,8 +511,7 @@ def dirichlet_logpdf(x, alpha):
# scipy's implementation of dirichlet logpdf doesn't support
# batch of x, we use this modified version.
def _lnB(alpha):
return np.sum(special.gammaln(alpha)) - \
special.gammaln(np.sum(alpha))
return np.sum(gammaln(alpha)) - gammaln(np.sum(alpha))

lnB = _lnB(alpha)
return - lnB + np.sum(np.log(x) * (alpha - 1), -1)
Expand Down Expand Up @@ -655,7 +655,7 @@ def _test_value(given, temperature, logits):
n = logits.shape[-1]

t = temperature
target_log_p = special.gammaln(n) + (n - 1) * np.log(t) + \
target_log_p = gammaln(n) + (n - 1) * np.log(t) + \
(logits - t * given).sum(axis=-1) - \
n * np.log(np.exp(logits - t * given).sum(axis=-1))

Expand Down Expand Up @@ -816,7 +816,7 @@ def _test_value(given, temperature, logits):
n = logits.shape[-1]

t = temperature
target_log_p = special.gammaln(n) + (n - 1) * np.log(t) + \
target_log_p = gammaln(n) + (n - 1) * np.log(t) + \
(logits - (t + 1) * np.log(given)).sum(axis=-1) - \
n * np.log(np.exp(logits - t * np.log(given)).sum(axis=-1))

Expand Down
5 changes: 3 additions & 2 deletions tests/distributions/test_univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import tensorflow as tf
import numpy as np
from scipy import stats, misc
from scipy import stats
from scipy.special import logsumexp

from tests.distributions import utils
from zhusuan.distributions.univariate import *
Expand Down Expand Up @@ -537,7 +538,7 @@ def test_value(self):
with self.test_session(use_gpu=True):
def _test_value(logits, given):
logits = np.array(logits, np.float32)
normalized_logits = logits - misc.logsumexp(
normalized_logits = logits - logsumexp(
logits, axis=-1, keepdims=True)
given = np.array(given, np.int32)
cat = Categorical(logits)
Expand Down
6 changes: 3 additions & 3 deletions tests/distributions/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import tensorflow as tf
import numpy as np
from scipy import misc
from scipy.special import factorial

from zhusuan.distributions.utils import *
from zhusuan.distributions.utils import get_shape_list, get_shape_at, \
Expand All @@ -20,8 +20,8 @@ def test_log_combination(self):
def _test_func(n, ks):
tf_n = tf.convert_to_tensor(n, tf.float32)
tf_ks = tf.convert_to_tensor(ks, tf.float32)
true_value = np.log(misc.factorial(n)) - \
np.sum(np.log(misc.factorial(ks)), axis=-1)
true_value = np.log(factorial(n)) - \
np.sum(np.log(factorial(ks)), axis=-1)
test_value = log_combination(tf_n, tf_ks).eval()
self.assertAllClose(true_value, test_value)

Expand Down
15 changes: 0 additions & 15 deletions tests/legacy/test_variational_legacy.py

This file was deleted.

6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import division

import tensorflow as tf
from scipy import misc
from scipy.special import logsumexp
import numpy as np
import six

Expand Down Expand Up @@ -254,7 +254,7 @@ def test_log_sum_exp(self):
a = np.array([[[1., 3., 0.2], [0.7, 2., 1e-6]],
[[0., 1e6, 1.], [1., 1., 1.]]])
for keepdims in [True, False]:
true_values = misc.logsumexp(a, (0, 2), keepdims=keepdims)
true_values = logsumexp(a, (0, 2), keepdims=keepdims)
test_values = sess.run(log_sum_exp(
tf.constant(a), (0, 2), keepdims))
self.assertAllClose(test_values, true_values)
Expand All @@ -267,7 +267,7 @@ def test_log_mean_exp(self):
a = np.array([[[1., 3., 0.2], [0.7, 2., 1e-6]],
[[0., 1e6, 1.], [1., 1., 1.]]])
for keepdims in [True, False]:
true_values = misc.logsumexp(a, (0, 2), keepdims=keepdims) - \
true_values = logsumexp(a, (0, 2), keepdims=keepdims) - \
np.log(a.shape[0] * a.shape[2])
test_values = sess.run(log_mean_exp(
tf.constant(a), (0, 2), keepdims))
Expand Down
3 changes: 2 additions & 1 deletion zhusuan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
from .utils import *
from .legacy import *

__version__ = '0.3.0'

__version__ = '0.3.1'
2 changes: 0 additions & 2 deletions zhusuan/legacy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from .variational_legacy import *

0 comments on commit 8146247

Please sign in to comment.