From b40cd3aeeb3c11c1aeeb68638a77545d2051b8dd Mon Sep 17 00:00:00 2001 From: SrMouraSilva Date: Wed, 6 Mar 2019 13:00:18 -0300 Subject: [PATCH] Issue #8 Issue #6 Use absolute import instead relative --- boltzmann_machines/__init__.py | 14 ++++++-------- boltzmann_machines/base/base.py | 1 + boltzmann_machines/base/base_model.py | 6 +++--- boltzmann_machines/base/mixin.py | 2 +- boltzmann_machines/base/tf_model.py | 3 +-- boltzmann_machines/dbm.py | 8 ++++---- boltzmann_machines/ebm.py | 2 +- boltzmann_machines/rbm/base_rbm.py | 6 +++--- boltzmann_machines/rbm/rbm.py | 10 +++++----- boltzmann_machines/rbm/tests/test_rbm.py | 4 ++-- boltzmann_machines/utils/augmentation.py | 3 ++- boltzmann_machines/utils/dataset.py | 2 +- boltzmann_machines/utils/testing.py | 2 ++ boltzmann_machines/utils/utils.py | 20 ++++++++++++++++---- 14 files changed, 48 insertions(+), 35 deletions(-) diff --git a/boltzmann_machines/__init__.py b/boltzmann_machines/__init__.py index 2f14059..4c27516 100644 --- a/boltzmann_machines/__init__.py +++ b/boltzmann_machines/__init__.py @@ -1,12 +1,10 @@ __author__ = 'Yelysei Bondarenko' __email__ = 'yell.bondarenko@gmail.com' -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 diff --git a/boltzmann_machines/base/base.py b/boltzmann_machines/base/base.py index 7196678..3c8a298 100644 --- a/boltzmann_machines/base/base.py +++ b/boltzmann_machines/base/base.py @@ -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('_') diff --git a/boltzmann_machines/base/base_model.py b/boltzmann_machines/base/base_model.py index 641b50e..6ec3ce2 100644 --- a/boltzmann_machines/base/base_model.py +++ b/boltzmann_machines/base/base_model.py @@ -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): diff --git a/boltzmann_machines/base/mixin.py b/boltzmann_machines/base/mixin.py index 8f1fcfd..c0865a3 100644 --- a/boltzmann_machines/base/mixin.py +++ b/boltzmann_machines/base/mixin.py @@ -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): diff --git a/boltzmann_machines/base/tf_model.py b/boltzmann_machines/base/tf_model.py index 37cdbfb..d33ad67 100644 --- a/boltzmann_machines/base/tf_model.py +++ b/boltzmann_machines/base/tf_model.py @@ -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): diff --git a/boltzmann_machines/dbm.py b/boltzmann_machines/dbm.py index 4b11654..b67e140 100644 --- a/boltzmann_machines/dbm.py +++ b/boltzmann_machines/dbm.py @@ -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) diff --git a/boltzmann_machines/ebm.py b/boltzmann_machines/ebm.py index 73d4549..d091dce 100644 --- a/boltzmann_machines/ebm.py +++ b/boltzmann_machines/ebm.py @@ -1,4 +1,4 @@ -from .base import TensorFlowModel +from boltzmann_machines.base import TensorFlowModel class EnergyBasedModel(TensorFlowModel): diff --git a/boltzmann_machines/rbm/base_rbm.py b/boltzmann_machines/rbm/base_rbm.py index 718c421..f5a859a 100644 --- a/boltzmann_machines/rbm/base_rbm.py +++ b/boltzmann_machines/rbm/base_rbm.py @@ -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 diff --git a/boltzmann_machines/rbm/rbm.py b/boltzmann_machines/rbm/rbm.py index a90e3f6..2a6c676 100644 --- a/boltzmann_machines/rbm/rbm.py +++ b/boltzmann_machines/rbm/rbm.py @@ -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): @@ -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) diff --git a/boltzmann_machines/rbm/tests/test_rbm.py b/boltzmann_machines/rbm/tests/test_rbm.py index a88c982..13e31a0 100644 --- a/boltzmann_machines/rbm/tests/test_rbm.py +++ b/boltzmann_machines/rbm/tests/test_rbm.py @@ -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): diff --git a/boltzmann_machines/utils/augmentation.py b/boltzmann_machines/utils/augmentation.py index f6cd1d0..82a4d2f 100644 --- a/boltzmann_machines/utils/augmentation.py +++ b/boltzmann_machines/utils/augmentation.py @@ -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 diff --git a/boltzmann_machines/utils/dataset.py b/boltzmann_machines/utils/dataset.py index 437bdb1..660cc15 100755 --- a/boltzmann_machines/utils/dataset.py +++ b/boltzmann_machines/utils/dataset.py @@ -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='.'): diff --git a/boltzmann_machines/utils/testing.py b/boltzmann_machines/utils/testing.py index eadde70..4176c5e 100755 --- a/boltzmann_machines/utils/testing.py +++ b/boltzmann_machines/utils/testing.py @@ -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: diff --git a/boltzmann_machines/utils/utils.py b/boltzmann_machines/utils/utils.py index bda9490..e452380 100644 --- a/boltzmann_machines/utils/utils.py +++ b/boltzmann_machines/utils/utils.py @@ -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 @@ -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. @@ -41,6 +44,7 @@ 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: @@ -48,9 +52,11 @@ def epoch_iter(start_epoch, max_epoch, verbose=False): 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. @@ -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 @@ -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}. @@ -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. @@ -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. @@ -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. @@ -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.