Skip to content

Commit

Permalink
Issue yell#8 Issue yell#6 Use absolute import instead relative
Browse files Browse the repository at this point in the history
  • Loading branch information
SrMouraSilva committed Mar 6, 2019
1 parent a6af347 commit b40cd3a
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 35 deletions.
14 changes: 6 additions & 8 deletions boltzmann_machines/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
__author__ = 'Yelysei Bondarenko'
__email__ = '[email protected]'

from .dbm import *
from . import dbm
from . import ebm
from . import layers

from . import base
from . import rbm
from . import utils
from boltzmann_machines.dbm import *
from boltzmann_machines.ebm import *
#from boltzmann_machines.layers import *

#from boltzmann_machines import base
#from boltzmann_machines import rbm
#from boltzmann_machines import utils
1 change: 1 addition & 0 deletions boltzmann_machines/base/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def is_param_name(name):
return not name.startswith('_') and not name.endswith('_')


def is_attribute_name(name):
return not name.startswith('_') and name.endswith('_')
6 changes: 3 additions & 3 deletions boltzmann_machines/base/base_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
from copy import deepcopy
from ..base.base import is_param_name, is_attribute_name
from .mixin import SeedMixin
from ..utils.utils import write_during_training
from boltzmann_machines.base.base import is_param_name, is_attribute_name
from boltzmann_machines.base.mixin import SeedMixin
from boltzmann_machines.utils.utils import write_during_training


class BaseModel(SeedMixin):
Expand Down
2 changes: 1 addition & 1 deletion boltzmann_machines/base/mixin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import tensorflow as tf

from ..utils import RNG
from boltzmann_machines.utils import RNG


class BaseMixin(object):
Expand Down
3 changes: 1 addition & 2 deletions boltzmann_machines/base/tf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import tensorflow as tf
from functools import wraps

from ..base import (BaseModel, DtypeMixin,
is_param_name)
from boltzmann_machines.base import (BaseModel, DtypeMixin, is_param_name)


def run_in_tf_session(check_initialized=True, update_seed=False):
Expand Down
8 changes: 4 additions & 4 deletions boltzmann_machines/dbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from tensorflow.core.framework import summary_pb2
from tensorflow.contrib.distributions import Bernoulli

from .base import run_in_tf_session
from .ebm import EnergyBasedModel
from .layers import BernoulliLayer
from .utils import (make_list_from, write_during_training,
from boltzmann_machines.base import run_in_tf_session
from boltzmann_machines.ebm import EnergyBasedModel
from boltzmann_machines.layers import BernoulliLayer
from boltzmann_machines.utils import (make_list_from, write_during_training,
batch_iter, epoch_iter,
log_sum_exp, log_diff_exp, log_mean_exp, log_std_exp)

Expand Down
2 changes: 1 addition & 1 deletion boltzmann_machines/ebm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base import TensorFlowModel
from boltzmann_machines.base import TensorFlowModel


class EnergyBasedModel(TensorFlowModel):
Expand Down
6 changes: 3 additions & 3 deletions boltzmann_machines/rbm/base_rbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import tensorflow as tf
from tensorflow.core.framework import summary_pb2

from ..ebm import EnergyBasedModel
from ..base import run_in_tf_session, is_attribute_name
from ..utils import (make_list_from, batch_iter, epoch_iter,
from boltzmann_machines.ebm import EnergyBasedModel
from boltzmann_machines.base import run_in_tf_session, is_attribute_name
from boltzmann_machines.utils import (make_list_from, batch_iter, epoch_iter,
write_during_training)
from ..utils.testing import assert_len, assert_shape

Expand Down
10 changes: 5 additions & 5 deletions boltzmann_machines/rbm/rbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import tensorflow as tf
from tensorflow.contrib.distributions import Multinomial

from .env import *
from .base_rbm import BaseRBM
from layers import BernoulliLayer, MultinomialLayer, GaussianLayer
from boltzmann_machines.rbm.env import *
from boltzmann_machines.rbm.base_rbm import BaseRBM
from boltzmann_machines.layers import BernoulliLayer, MultinomialLayer, GaussianLayer


class BernoulliRBM(BaseRBM):
Expand Down Expand Up @@ -125,6 +125,6 @@ def logit_mean(X):

if __name__ == '__main__':
# run corresponding tests
from utils.testing import run_tests
from tests import test_rbm as t
from boltzmann_machines.utils.testing import run_tests
from boltzmann_machines.rbm.tests import test_rbm as t
run_tests(__file__, t)
4 changes: 2 additions & 2 deletions boltzmann_machines/rbm/tests/test_rbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
assert_almost_equal,
assert_raises)

from rbm import BernoulliRBM, MultinomialRBM, GaussianRBM
from utils import RNG
from boltzmann_machines.rbm import BernoulliRBM, MultinomialRBM, GaussianRBM
from boltzmann_machines.utils import RNG


class TestRBM(object):
Expand Down
3 changes: 2 additions & 1 deletion boltzmann_machines/utils/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def shift(x, offset=(0, 0)):
y = nd.interpolation.shift(x, shift=offset, mode='nearest')
return y


def horizontal_mirror(x):
y = np.fliplr(x[:,:,...])
y = np.fliplr(x[:, :, ...])
return y
2 changes: 1 addition & 1 deletion boltzmann_machines/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import matplotlib.pyplot as plt

from .rng import RNG
from boltzmann_machines.utils.rng import RNG


def load_mnist(mode='train', path='.'):
Expand Down
2 changes: 2 additions & 0 deletions boltzmann_machines/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ def run_tests(script_path, test_module=None):
params.append('--with-doctest')
nose.run(argv=params)


def assert_shape(obj, name, desired_shape):
actual_shape = getattr(obj, name).shape
if actual_shape != desired_shape:
raise ValueError('`{0}` has invalid shape {1} != {2}'.\
format(name, actual_shape, desired_shape))


def assert_len(obj, name, desired_len):
actual_len = len(getattr(obj, name))
if actual_len != desired_len:
Expand Down
20 changes: 16 additions & 4 deletions boltzmann_machines/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np

from tqdm import tqdm, tqdm_notebook


def _is_in_ipython():
try: __IPYTHON__; return True
except NameError: return False
Expand All @@ -10,6 +12,7 @@ def _is_in_ipython():
def write_during_training(s):
tqdm.write(s)


def batch_iter(X, batch_size=10, verbose=False, desc='epoch'):
"""Divide input data into batches, with optional
progress bar.
Expand Down Expand Up @@ -41,16 +44,19 @@ def batch_iter(X, batch_size=10, verbose=False, desc='epoch'):
for i in gen:
yield X[i*batch_size:(i + 1)*batch_size]


def epoch_iter(start_epoch, max_epoch, verbose=False):
gen = range(start_epoch + 1, max_epoch + 1)
if verbose:
gen = progress_bar(gen, leave=True, ncols=84, desc='training')
for epoch in gen:
yield epoch


def make_list_from(x):
return list(x) if hasattr(x, '__iter__') else [x]


def one_hot(y, n_classes=None):
"""Convert `y` to one-hot encoding.
Expand All @@ -67,6 +73,7 @@ def one_hot(y, n_classes=None):
n_classes = n_classes or np.max(y) + 1
return np.eye(n_classes)[y]


def one_hot_decision_function(y):
"""
Examples
Expand All @@ -76,15 +83,16 @@ def one_hot_decision_function(y):
... [0.2, 0.2, 0.6],
... [0.3, 0.4, 0.3]]
>>> one_hot_decision_function(y)
array([[ 0., 0., 1.],
[ 1., 0., 0.],
[ 0., 0., 1.],
[ 0., 1., 0.]])
array([[0., 0., 1.],
[1., 0., 0.],
[0., 0., 1.],
[0., 1., 0.]])
"""
z = np.zeros_like(y)
z[np.arange(len(z)), np.argmax(y, axis=1)] = 1
return z


def unhot(y, n_classes=None):
"""
Map `y` from one-hot encoding to {0, ..., `n_classes` - 1}.
Expand All @@ -105,6 +113,7 @@ def unhot(y, n_classes=None):
_, n_classes = y.shape
return y.dot(np.arange(n_classes))


def log_sum_exp(x):
"""Compute log(sum(exp(x))) in a numerically stable way.
Expand All @@ -124,6 +133,7 @@ def log_sum_exp(x):
a = max(x)
return a + np.log(sum(np.exp(x - a)))


def log_mean_exp(x):
"""Compute log(mean(exp(x))) in a numerically stable way.
Expand All @@ -135,6 +145,7 @@ def log_mean_exp(x):
"""
return log_sum_exp(x) - np.log(len(x))


def log_diff_exp(x):
"""Compute log(diff(exp(x))) in a numerically stable way.
Expand All @@ -149,6 +160,7 @@ def log_diff_exp(x):
a = max(x)
return a + np.log(np.diff(np.exp(x - a)))


def log_std_exp(x, log_mean_exp_x=None):
"""Compute log(std(exp(x))) in a numerically stable way.
Expand Down

0 comments on commit b40cd3a

Please sign in to comment.