From 42bdbe00d3a36f04ee9f7b28aa4e825760b597c5 Mon Sep 17 00:00:00 2001 From: Juanje Date: Wed, 30 Sep 2020 12:47:56 +0200 Subject: [PATCH 01/10] New branch for the issue 212 --- examples/edward/ed_vae_mnist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/edward/ed_vae_mnist.py b/examples/edward/ed_vae_mnist.py index 7609a7f..ea7f02a 100644 --- a/examples/edward/ed_vae_mnist.py +++ b/examples/edward/ed_vae_mnist.py @@ -2,8 +2,8 @@ import matplotlib.pyplot as plt import numpy as np import inferpy as inf -import pyro -import torch +# import pyro +# import torch import tensorflow_probability.python.edward2 as ed # number of components From fb14f1547458d74f5b4b073eb1f72e64cedc2124 Mon Sep 17 00:00:00 2001 From: Juanje Date: Wed, 30 Sep 2020 20:59:58 +0200 Subject: [PATCH 02/10] Updating random_variable.py --- inferpy/models/random_variable.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/inferpy/models/random_variable.py b/inferpy/models/random_variable.py index e72b1ff..ff9daa6 100644 --- a/inferpy/models/random_variable.py +++ b/inferpy/models/random_variable.py @@ -16,11 +16,18 @@ from enum import IntEnum import tensorflow as tf from tensorflow_probability import edward2 as ed -from tensorflow_probability.python.edward2.interceptor import interceptable +# For tfp version 0.7.0 +# from tensorflow_probability.edward2.interception import interceptable +from tensorflow_probability.python.edward2 import interceptable import tensorflow_probability as tfp -from tensorflow_probability.python.edward2 import generated_random_variables -from tensorflow.python.client import session as tf_session +# For tfp version 0.7.0 +# from tensorflow_probability.python.edward2 import generated_random_variables +from tensorflow_probability.python.experimental.edward2 import generated_random_variables +# For tfp version 0.7.0 +# from tensorflow.python.client import session as tf_session + + import warnings from . import sanitize_input_arg From 4a64af9fbff3c0fb0549c5b9b5fee57f27082237 Mon Sep 17 00:00:00 2001 From: Juanje Date: Fri, 9 Oct 2020 11:16:39 +0200 Subject: [PATCH 03/10] Update --- examples/probzoo/nlpca_mnist.py | 2 +- examples/probzoo/vae_mnist.py | 4 ++-- inferpy/models/random_variable.py | 2 +- req.txt | 35 +++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 req.txt diff --git a/examples/probzoo/nlpca_mnist.py b/examples/probzoo/nlpca_mnist.py index 30f18c2..ee9d8b2 100644 --- a/examples/probzoo/nlpca_mnist.py +++ b/examples/probzoo/nlpca_mnist.py @@ -1,4 +1,4 @@ -# required pacakges +# required packages import inferpy as inf import numpy as np import tensorflow as tf diff --git a/examples/probzoo/vae_mnist.py b/examples/probzoo/vae_mnist.py index cb8f2d1..c31d2bf 100644 --- a/examples/probzoo/vae_mnist.py +++ b/examples/probzoo/vae_mnist.py @@ -2,8 +2,8 @@ import matplotlib.pyplot as plt import numpy as np import inferpy as inf -import pyro -import torch +# import pyro +# import torch import tensorflow_probability.python.edward2 as ed # number of components diff --git a/inferpy/models/random_variable.py b/inferpy/models/random_variable.py index ff9daa6..1595b3d 100644 --- a/inferpy/models/random_variable.py +++ b/inferpy/models/random_variable.py @@ -26,7 +26,7 @@ from tensorflow_probability.python.experimental.edward2 import generated_random_variables # For tfp version 0.7.0 # from tensorflow.python.client import session as tf_session - +from tensorflow.python.client import session as tf_session import warnings diff --git a/req.txt b/req.txt new file mode 100644 index 0000000..701cb75 --- /dev/null +++ b/req.txt @@ -0,0 +1,35 @@ +absl-py==0.10.0 +astor==0.8.1 +certifi==2020.6.20 +cloudpickle==1.1.1 +cycler==0.10.0 +decorator==4.4.2 +gast==0.2.2 +google-pasta==0.2.0 +grpcio==1.32.0 +h5py==2.10.0 +importlib-metadata==2.0.0 +Keras==2.3.1 +Keras-Applications==1.0.8 +Keras-Preprocessing==1.1.2 +kiwisolver==1.2.0 +Markdown==3.2.2 +matplotlib==3.3.2 +networkx==2.5 +numpy==1.19.2 +opt-einsum==3.3.0 +Pillow==7.2.0 +protobuf==3.13.0 +pyparsing==2.4.7 +python-dateutil==2.8.1 +PyYAML==5.3.1 +scipy==1.5.2 +six==1.15.0 +tensorboard==1.15.0 +tensorflow==1.15.2 +tensorflow-estimator==1.15.1 +tensorflow-probability==0.8.0 +termcolor==1.1.0 +Werkzeug==1.0.1 +wrapt==1.12.1 +zipp==3.2.0 From df833ac36360750dd304dede4a6f06fc5245bf77 Mon Sep 17 00:00:00 2001 From: Juanje Date: Fri, 16 Oct 2020 13:44:42 +0200 Subject: [PATCH 04/10] Update --- inferpy/contextmanager/randvar_registry.py | 1 + 1 file changed, 1 insertion(+) diff --git a/inferpy/contextmanager/randvar_registry.py b/inferpy/contextmanager/randvar_registry.py index fe94288..ebc7b96 100644 --- a/inferpy/contextmanager/randvar_registry.py +++ b/inferpy/contextmanager/randvar_registry.py @@ -111,6 +111,7 @@ def update_graph(rv_name=None): elements_set.add(rv_name) # now create the dependencies graph _properties['graph'] = tf_graph.get_graph(elements_set) + print("DEBUG") @contextmanager From 570c8b03772c7c47a1be39c6188f6e748a0f7d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Caba=C3=B1as?= Date: Sun, 18 Oct 2020 20:02:20 +0200 Subject: [PATCH 05/10] fixed at sanitize --- inferpy/contextmanager/randvar_registry.py | 1 - inferpy/models/__init__.py | 8 +++++++- inferpy/models/random_variable.py | 5 ++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/inferpy/contextmanager/randvar_registry.py b/inferpy/contextmanager/randvar_registry.py index ebc7b96..fe94288 100644 --- a/inferpy/contextmanager/randvar_registry.py +++ b/inferpy/contextmanager/randvar_registry.py @@ -111,7 +111,6 @@ def update_graph(rv_name=None): elements_set.add(rv_name) # now create the dependencies graph _properties['graph'] = tf_graph.get_graph(elements_set) - print("DEBUG") @contextmanager diff --git a/inferpy/models/__init__.py b/inferpy/models/__init__.py index eaa0709..d3bc04c 100644 --- a/inferpy/models/__init__.py +++ b/inferpy/models/__init__.py @@ -2,7 +2,7 @@ from tensorflow_probability.python import edward2 as ed -def sanitize_input_arg(arg): +def sanitize_input_arg(arg, varname=None): # This function sanitize the input arguments to create Random Variables. It does: # - convert Random Variables (from inferpy or edward2) to tensors, even if there # are in a list or nested list, in order to allow to use them as arguments for other Random Variables @@ -21,6 +21,12 @@ def sanitize_input_arg(arg): if hasattr(sanitized_arg, "dtype") and util.common.is_float(sanitized_arg.dtype) and sanitized_arg.dtype != util.floatx(): sanitized_arg = tf.cast(sanitized_arg, util.floatx()) + # In TFP>0.8 the argument tensors are not created until needed, but we need them + # for building the dependence graph. + if varname is not None and isinstance(sanitized_arg, tf.Tensor): + with tf.name_scope(varname): + sanitized_arg = tf.identity(sanitized_arg) + return sanitized_arg diff --git a/inferpy/models/random_variable.py b/inferpy/models/random_variable.py index 1595b3d..5ba55af 100644 --- a/inferpy/models/random_variable.py +++ b/inferpy/models/random_variable.py @@ -335,8 +335,8 @@ def func(*args, **kwargs): sample_shape = kwargs.pop('sample_shape', ()) # convert any Random Variable in a list or nested list in arguments to tensors, allowing to use RV's as arguments - sanitized_args = [sanitize_input_arg(arg) for arg in args] - sanitized_kwargs = {k: sanitize_input_arg(v) for k, v in kwargs.items()} + sanitized_args = [sanitize_input_arg(arg, rv_name) for arg in args] + sanitized_kwargs = {k: sanitize_input_arg(v, rv_name) for k, v in kwargs.items()} # If it is inside a data model, ommit the sample_shape in kwargs if exist and use size from data_model # NOTE: Needed here because we need to know the shape of the distribution, as well as its dtype @@ -345,7 +345,6 @@ def func(*args, **kwargs): if contextmanager.data_model.is_active(): # create graph once tensors are registered in graph contextmanager.randvar_registry.update_graph(rv_name) - # compute sample_shape now that we have computed the dependencies sample_shape = contextmanager.data_model.get_sample_shape(rv_name) From 4e2f881d83eeef119412a703badbff9941ced7e1 Mon Sep 17 00:00:00 2001 From: Juanje Date: Tue, 20 Oct 2020 16:34:01 +0200 Subject: [PATCH 06/10] Update --- inferpy/models/random_variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inferpy/models/random_variable.py b/inferpy/models/random_variable.py index 5ba55af..6cac38f 100644 --- a/inferpy/models/random_variable.py +++ b/inferpy/models/random_variable.py @@ -338,7 +338,7 @@ def func(*args, **kwargs): sanitized_args = [sanitize_input_arg(arg, rv_name) for arg in args] sanitized_kwargs = {k: sanitize_input_arg(v, rv_name) for k, v in kwargs.items()} - # If it is inside a data model, ommit the sample_shape in kwargs if exist and use size from data_model + # If it is inside a data model, omit the sample_shape in kwargs if exist and use size from data_model # NOTE: Needed here because we need to know the shape of the distribution, as well as its dtype # Not using sample shape yet. Used just to create the tensors, and compute the dependencies using the tf graph tfp_dist = distribution_cls(*sanitized_args, **sanitized_kwargs) From 053b3e30e42d8954363af046c3db8cbc3fe9a62d Mon Sep 17 00:00:00 2001 From: Juanje Date: Mon, 26 Oct 2020 22:12:06 +0100 Subject: [PATCH 07/10] Update --- examples/edward/ed_logregression.py | 4 +++- inferpy/util/interceptor.py | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/edward/ed_logregression.py b/examples/edward/ed_logregression.py index ca90ae0..50786df 100644 --- a/examples/edward/ed_logregression.py +++ b/examples/edward/ed_logregression.py @@ -1,7 +1,9 @@ ### Setting up from tensorflow_probability import edward2 as ed -import tensorflow as tf +# import tensorflow as tf +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() d = 2 N = 50000 diff --git a/inferpy/util/interceptor.py b/inferpy/util/interceptor.py index c5eed9f..ac7d114 100644 --- a/inferpy/util/interceptor.py +++ b/inferpy/util/interceptor.py @@ -1,9 +1,12 @@ -import tensorflow as tf +# import tensorflow as tf +import tensorflow.compat.v1 as tf from tensorflow_probability import edward2 as ed from contextlib import contextmanager from inferpy import util from inferpy.contextmanager import data_model +tf.disable_v2_behavior() + # Global variable to access when enable_interceptor is used. However, the vaiable will be None in the finally clause, # so a local variable needs to be used in the set_value function, and the real enable_variable should never be deleted From 91d34ded5eef61771387c71dc9edea3eb9fe2f9a Mon Sep 17 00:00:00 2001 From: Juanje Date: Wed, 28 Oct 2020 18:29:56 +0100 Subject: [PATCH 08/10] Update --- examples/edward/ed_logregression.py | 2 -- examples/edward/ed_vae_mnist.py | 4 +++- examples/probzoo/nlpca.py | 4 +++- examples/probzoo/nlpca_mnist.py | 4 +++- examples/probzoo/pca_mnist.py | 4 +++- examples/probzoo/vae.py | 4 +++- examples/probzoo/vae_mnist.py | 4 +++- examples/probzoo/vae_mnist_csvdata.py | 4 +++- examples/probzoo/vae_mnist_inf_bay_layers.py | 4 +++- inferpy/inference/variational/svi.py | 8 ++++++-- inferpy/inference/variational/vi.py | 5 ++++- inferpy/models/parameter.py | 5 ++++- inferpy/models/prob_model.py | 5 ++++- inferpy/util/session.py | 4 +++- inferpy/util/tf_graph.py | 4 +++- 15 files changed, 48 insertions(+), 17 deletions(-) diff --git a/examples/edward/ed_logregression.py b/examples/edward/ed_logregression.py index 50786df..ba261ad 100644 --- a/examples/edward/ed_logregression.py +++ b/examples/edward/ed_logregression.py @@ -9,8 +9,6 @@ N = 50000 - - #12 ### Model definition #### diff --git a/examples/edward/ed_vae_mnist.py b/examples/edward/ed_vae_mnist.py index ea7f02a..a59eca6 100644 --- a/examples/edward/ed_vae_mnist.py +++ b/examples/edward/ed_vae_mnist.py @@ -1,10 +1,12 @@ -import tensorflow as tf +# import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import inferpy as inf # import pyro # import torch import tensorflow_probability.python.edward2 as ed +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() # number of components k = 2 diff --git a/examples/probzoo/nlpca.py b/examples/probzoo/nlpca.py index 43b46c2..f035158 100644 --- a/examples/probzoo/nlpca.py +++ b/examples/probzoo/nlpca.py @@ -7,7 +7,9 @@ ########## import inferpy as inf -import tensorflow as tf +# import tensorflow as tf +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() # definition of a generic model diff --git a/examples/probzoo/nlpca_mnist.py b/examples/probzoo/nlpca_mnist.py index ee9d8b2..cc8ba09 100644 --- a/examples/probzoo/nlpca_mnist.py +++ b/examples/probzoo/nlpca_mnist.py @@ -1,10 +1,12 @@ # required packages import inferpy as inf import numpy as np -import tensorflow as tf +# import tensorflow as tf import matplotlib.pyplot as plt from inferpy.data import mnist +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() # number of components diff --git a/examples/probzoo/pca_mnist.py b/examples/probzoo/pca_mnist.py index b6dc12a..33a0603 100644 --- a/examples/probzoo/pca_mnist.py +++ b/examples/probzoo/pca_mnist.py @@ -1,10 +1,12 @@ # required pacakges import inferpy as inf import numpy as np -import tensorflow as tf +# import tensorflow as tf import matplotlib.pyplot as plt from inferpy.data import mnist +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() tf.reset_default_graph() tf.set_random_seed(1234) diff --git a/examples/probzoo/vae.py b/examples/probzoo/vae.py index 75981b0..e33f3b1 100644 --- a/examples/probzoo/vae.py +++ b/examples/probzoo/vae.py @@ -1,7 +1,9 @@ # required packages import numpy as np import inferpy as inf -import tensorflow as tf +# import tensorflow as tf +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() # number of observations diff --git a/examples/probzoo/vae_mnist.py b/examples/probzoo/vae_mnist.py index c31d2bf..ea3189e 100644 --- a/examples/probzoo/vae_mnist.py +++ b/examples/probzoo/vae_mnist.py @@ -1,10 +1,12 @@ -import tensorflow as tf +# import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import inferpy as inf # import pyro # import torch import tensorflow_probability.python.edward2 as ed +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() # number of components k = 2 diff --git a/examples/probzoo/vae_mnist_csvdata.py b/examples/probzoo/vae_mnist_csvdata.py index ba5f8ee..9f4b5d4 100644 --- a/examples/probzoo/vae_mnist_csvdata.py +++ b/examples/probzoo/vae_mnist_csvdata.py @@ -1,11 +1,13 @@ # required pacakges import inferpy as inf import numpy as np -import tensorflow as tf +# import tensorflow as tf import matplotlib.pyplot as plt from inferpy.data import mnist from inferpy.data.loaders import CsvLoader +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() diff --git a/examples/probzoo/vae_mnist_inf_bay_layers.py b/examples/probzoo/vae_mnist_inf_bay_layers.py index f9bfaad..1a9d810 100644 --- a/examples/probzoo/vae_mnist_inf_bay_layers.py +++ b/examples/probzoo/vae_mnist_inf_bay_layers.py @@ -1,7 +1,9 @@ -import tensorflow as tf +# import tensorflow as tf import inferpy as inf from inferpy.data import mnist import tensorflow_probability as tfp +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() N, M = 1000, 100 # data and batch size (x_train, _), _ = mnist.load_data(num_instances=N, diff --git a/inferpy/inference/variational/svi.py b/inferpy/inference/variational/svi.py index c7d2577..334f9d2 100644 --- a/inferpy/inference/variational/svi.py +++ b/inferpy/inference/variational/svi.py @@ -1,10 +1,12 @@ import numpy as np -import tensorflow as tf +# import tensorflow as tf from inferpy import util from inferpy import contextmanager from .vi import VI from inferpy.data.loaders import build_data_loader, DataLoader +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() class SVI(VI): @@ -74,7 +76,9 @@ def create_input_data_tensor(self, data_loader): raise ValueError("The size of the data must be equal or greater than the batch size") data_loader.shuffle_buffer_size = data_size - iterator = data_loader.to_tfdataset(self.batch_size).make_one_shot_iterator() + # iterator = data_loader.to_tfdataset(self.batch_size).make_one_shot_iterator() + + iterator = tf.compat.v1.data.make_one_shot_iterator(data_loader.to_tfdataset(self.batch_size)) # each time this tensor is evaluated in a session it contains new data input_data = data_loader.map_batch_fn(iterator.get_next()) diff --git a/inferpy/inference/variational/vi.py b/inferpy/inference/variational/vi.py index 8a50f56..4b08d68 100644 --- a/inferpy/inference/variational/vi.py +++ b/inferpy/inference/variational/vi.py @@ -1,5 +1,5 @@ import numpy as np -import tensorflow as tf +# import tensorflow as tf import inspect import itertools from tensorflow_probability.python import edward2 as ed @@ -14,6 +14,9 @@ from inferpy.data.loaders import build_sample_dict +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() + class VI(Inference): def __init__(self, qmodel, loss='ELBO', optimizer='AdamOptimizer', epochs=1000): diff --git a/inferpy/models/parameter.py b/inferpy/models/parameter.py index 3755681..d954cdf 100644 --- a/inferpy/models/parameter.py +++ b/inferpy/models/parameter.py @@ -12,13 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -import tensorflow as tf +# import tensorflow as tf from tensorflow.python.client import session as tf_session from . import sanitize_input_arg from inferpy import contextmanager from inferpy import util +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() + class Parameter: """ diff --git a/inferpy/models/prob_model.py b/inferpy/models/prob_model.py index 2cc4028..845f5a8 100644 --- a/inferpy/models/prob_model.py +++ b/inferpy/models/prob_model.py @@ -17,7 +17,7 @@ import functools from collections import OrderedDict from tensorflow_probability import edward2 as ed -import tensorflow as tf +# import tensorflow as tf import networkx as nx import warnings @@ -27,6 +27,9 @@ from .random_variable import RandomVariable from inferpy.data.loaders import build_data_loader +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() + def probmodel(builder): """ diff --git a/inferpy/util/session.py b/inferpy/util/session.py index 16b948a..26b2414 100644 --- a/inferpy/util/session.py +++ b/inferpy/util/session.py @@ -1,7 +1,9 @@ -import tensorflow as tf +# import tensorflow as tf +import tensorflow.compat.v1 as tf import warnings +tf.disable_v2_behavior() """ Module to manage global sessions and graphs executed in sessions diff --git a/inferpy/util/tf_graph.py b/inferpy/util/tf_graph.py index 85e782b..036751e 100644 --- a/inferpy/util/tf_graph.py +++ b/inferpy/util/tf_graph.py @@ -1,6 +1,8 @@ from collections import defaultdict import networkx as nx -import tensorflow as tf +# import tensorflow as tf +import tensorflow.compat.v1 as tf +tf.disable_v2_behavior() """ From 8079bb49a79b4ac0cf740cc7c61ecb284261d7fc Mon Sep 17 00:00:00 2001 From: Juanje Date: Thu, 5 Nov 2020 11:14:43 +0100 Subject: [PATCH 09/10] Update --- examples/probzoo/vae.py | 1 + examples/probzoo/vae_mnist_csvdata.py | 1 + examples/probzoo/vae_mnist_inf_bay_layers.py | 1 + 3 files changed, 3 insertions(+) diff --git a/examples/probzoo/vae.py b/examples/probzoo/vae.py index e33f3b1..753763f 100644 --- a/examples/probzoo/vae.py +++ b/examples/probzoo/vae.py @@ -2,6 +2,7 @@ import numpy as np import inferpy as inf # import tensorflow as tf + import tensorflow.compat.v1 as tf tf.disable_v2_behavior() diff --git a/examples/probzoo/vae_mnist_csvdata.py b/examples/probzoo/vae_mnist_csvdata.py index 9f4b5d4..f9a8fe5 100644 --- a/examples/probzoo/vae_mnist_csvdata.py +++ b/examples/probzoo/vae_mnist_csvdata.py @@ -6,6 +6,7 @@ import matplotlib.pyplot as plt from inferpy.data import mnist from inferpy.data.loaders import CsvLoader + import tensorflow.compat.v1 as tf tf.disable_v2_behavior() diff --git a/examples/probzoo/vae_mnist_inf_bay_layers.py b/examples/probzoo/vae_mnist_inf_bay_layers.py index 1a9d810..3cb6292 100644 --- a/examples/probzoo/vae_mnist_inf_bay_layers.py +++ b/examples/probzoo/vae_mnist_inf_bay_layers.py @@ -2,6 +2,7 @@ import inferpy as inf from inferpy.data import mnist import tensorflow_probability as tfp + import tensorflow.compat.v1 as tf tf.disable_v2_behavior() From 863bd7b21083fe05051a54ccce8114f5fc847730 Mon Sep 17 00:00:00 2001 From: Juanje Date: Fri, 4 Dec 2020 15:35:14 +0100 Subject: [PATCH 10/10] update --- examples/edward/ed_logregression.py | 6 ++--- examples/edward/ed_vae_mnist.py | 25 ++++++++++++++------ examples/probzoo/nlpca.py | 12 ++++++---- examples/probzoo/nlpca_mnist.py | 12 ++++++---- examples/probzoo/pca_mnist.py | 16 ++++++++----- examples/probzoo/vae.py | 20 ++++++++++------ examples/probzoo/vae_mnist.py | 13 ++++++---- examples/probzoo/vae_mnist_csvdata.py | 20 ++++++++++------ examples/probzoo/vae_mnist_inf_bay_layers.py | 4 +--- inferpy/inference/variational/vi.py | 4 +++- 10 files changed, 83 insertions(+), 49 deletions(-) diff --git a/examples/edward/ed_logregression.py b/examples/edward/ed_logregression.py index ba261ad..606d17a 100644 --- a/examples/edward/ed_logregression.py +++ b/examples/edward/ed_logregression.py @@ -1,9 +1,7 @@ ### Setting up from tensorflow_probability import edward2 as ed -# import tensorflow as tf -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() +import tensorflow as tf d = 2 N = 50000 @@ -67,6 +65,8 @@ def interceptor(f, *args, **kwargs): + + #### Inference qw, qw0 = qmodel(d, N) diff --git a/examples/edward/ed_vae_mnist.py b/examples/edward/ed_vae_mnist.py index a59eca6..543a14d 100644 --- a/examples/edward/ed_vae_mnist.py +++ b/examples/edward/ed_vae_mnist.py @@ -1,12 +1,12 @@ -# import tensorflow as tf +import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import inferpy as inf # import pyro # import torch import tensorflow_probability.python.edward2 as ed -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() +from tensorflow.python.framework import ops + # number of components k = 2 @@ -26,8 +26,8 @@ num_epochs = 1000 learning_rate = 0.01 -tf.reset_default_graph() -tf.set_random_seed(1234) +ops.reset_default_graph() +tf.random.set_seed(1234) #29 from inferpy.data import mnist @@ -88,12 +88,23 @@ def qmodel(k, d0, x): ############## Edward ############## -batch = tf.data.Dataset.from_tensor_slices(x_train)\ +"""batch = tf.data.Dataset.from_tensor_slices(x_train)\ .shuffle(M)\ .batch(M)\ .repeat()\ - .make_one_shot_iterator().get_next() + .make_one_shot_iterator().get_next()""" + + +@tf.function +def generate_batch(x_train): + return tf.data.Dataset.from_tensor_slices(x_train)\ + .shuffle(M)\ + .batch(M)\ + .repeat()\ + .__iter__().__next__() + +batch = generate_batch(x_train) qz = qmodel(k, d0, batch) with ed.interception(ed.make_value_setter(z=qz, x=batch)): diff --git a/examples/probzoo/nlpca.py b/examples/probzoo/nlpca.py index f035158..7f1b376 100644 --- a/examples/probzoo/nlpca.py +++ b/examples/probzoo/nlpca.py @@ -7,9 +7,7 @@ ########## import inferpy as inf -# import tensorflow as tf -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() +import tensorflow as tf # definition of a generic model @@ -36,8 +34,12 @@ def nlpca(k, d0, dx, decoder): def decoder(z,d0,dx): - h0 = tf.layers.dense(z, d0, tf.nn.relu) - return tf.layers.dense(h0, 2 * dx) + """h0 = tf.layers.dense(z, d0, tf.nn.relu) + return tf.layers.dense(h0, 2 * dx)""" + model = tf.keras.Sequential([tf.keras.layers.Dense(d0, tf.nn.relu)]) + h0 = model(z) + model2 = tf.keras.Sequential([tf.keras.layers.Dense(2 * dx)]) + return model2(h0) # Q-model approximating P diff --git a/examples/probzoo/nlpca_mnist.py b/examples/probzoo/nlpca_mnist.py index cc8ba09..928b2b1 100644 --- a/examples/probzoo/nlpca_mnist.py +++ b/examples/probzoo/nlpca_mnist.py @@ -1,12 +1,10 @@ # required packages import inferpy as inf import numpy as np -# import tensorflow as tf +import tensorflow as tf import matplotlib.pyplot as plt from inferpy.data import mnist -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() # number of components @@ -35,8 +33,12 @@ def nlpca(k, d0, dx, decoder): def decoder(z, d0, dx): # k -> d0 -> 2*dx - h0 = tf.layers.dense(z, d0, tf.nn.relu) - return tf.layers.dense(h0, 2 * dx) + """h0 = tf.layers.dense(z, d0, tf.nn.relu) + return tf.layers.dense(h0, 2 * dx)""" + model = tf.keras.Sequential([tf.keras.layers.Dense(d0, tf.nn.relu)]) + h0 = model(z) + model2 = tf.keras.Sequential([tf.keras.layers.Dense(2 * dx)]) + return model2(h0) @inf.probmodel diff --git a/examples/probzoo/pca_mnist.py b/examples/probzoo/pca_mnist.py index 33a0603..6c299fc 100644 --- a/examples/probzoo/pca_mnist.py +++ b/examples/probzoo/pca_mnist.py @@ -1,15 +1,18 @@ # required pacakges import inferpy as inf import numpy as np -# import tensorflow as tf +import tensorflow as tf import matplotlib.pyplot as plt +from tensorflow.python.framework import ops from inferpy.data import mnist -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() -tf.reset_default_graph() -tf.set_random_seed(1234) + +"""tf.reset_default_graph() +tf.set_random_seed(1234)""" + +ops.reset_default_graph() +tf.random.set_seed(1234) # number of components k = 2 @@ -62,7 +65,8 @@ def qmodel(k,d): # load the data (x_train, y_train), _ = mnist.load_data(num_instances=N, digits=DIG) -optimizer = tf.train.AdamOptimizer(learning_rate) +#optimizer = tf.train.AdamOptimizer(learning_rate) +optimizer = tf.optimizers.Adam() VI = inf.inference.VI(q, optimizer=optimizer, epochs=2000) m.fit({"x": x_train}, VI) diff --git a/examples/probzoo/vae.py b/examples/probzoo/vae.py index 753763f..beadc7e 100644 --- a/examples/probzoo/vae.py +++ b/examples/probzoo/vae.py @@ -1,10 +1,8 @@ # required packages import numpy as np import inferpy as inf -# import tensorflow as tf +import tensorflow as tf -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() # number of observations @@ -43,14 +41,22 @@ def vae(k, d0, dx, decoder): def decoder(z, d0, dx): # k -> d0 -> 2*dx - h0 = tf.layers.dense(z, d0, tf.nn.relu) - return tf.layers.dense(h0, 2 * dx) + """h0 = tf.layers.dense(z, d0, tf.nn.relu) + return tf.layers.dense(h0, 2 * dx)""" + model = tf.keras.Sequential([tf.keras.layers.Dense(d0, tf.nn.relu)]) + h0 = model(z) + model2 = tf.keras.Sequential([tf.keras.layers.Dense(2 * dx)]) + return model2(h0) # Q-model approximating P def encoder(x, d0, k): # dx -> d0 -> 2*k - h0 = tf.layers.dense(x, d0, tf.nn.relu) - return tf.layers.dense(h0, 2 * k) + """h0 = tf.layers.dense(x, d0, tf.nn.relu) + return tf.layers.dense(h0, 2 * k)""" + model = tf.keras.Sequential([tf.keras.layers.Dense(d0, tf.nn.relu)]) + h0 = model(x) + model2 = tf.keras.Sequential([tf.keras.layers.Dense(2 * k)]) + return model2(h0) @inf.probmodel diff --git a/examples/probzoo/vae_mnist.py b/examples/probzoo/vae_mnist.py index ea3189e..cffe92e 100644 --- a/examples/probzoo/vae_mnist.py +++ b/examples/probzoo/vae_mnist.py @@ -1,12 +1,12 @@ -# import tensorflow as tf +import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import inferpy as inf # import pyro # import torch import tensorflow_probability.python.edward2 as ed -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() +from tensorflow.python.framework import ops + # number of components k = 2 @@ -26,8 +26,11 @@ num_epochs = 1000 learning_rate = 0.01 -tf.reset_default_graph() -tf.set_random_seed(1234) +"""tf.reset_default_graph() +tf.set_random_seed(1234)""" +ops.reset_default_graph() +tf.random.set_seed(1234) + #29 from inferpy.data import mnist diff --git a/examples/probzoo/vae_mnist_csvdata.py b/examples/probzoo/vae_mnist_csvdata.py index f9a8fe5..43a3021 100644 --- a/examples/probzoo/vae_mnist_csvdata.py +++ b/examples/probzoo/vae_mnist_csvdata.py @@ -1,14 +1,12 @@ # required pacakges import inferpy as inf import numpy as np -# import tensorflow as tf +import tensorflow as tf import matplotlib.pyplot as plt from inferpy.data import mnist from inferpy.data.loaders import CsvLoader -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() @@ -38,14 +36,22 @@ def vae(k, d0, dx, decoder): def decoder(z, d0, dx): # k -> d0 -> 2*dx - h0 = tf.layers.dense(z, d0, tf.nn.relu) - return tf.layers.dense(h0, 2 * dx) + """h0 = tf.layers.dense(z, d0, tf.nn.relu) + return tf.layers.dense(h0, 2 * dx)""" + model = tf.keras.Sequential([tf.keras.layers.Dense(d0, tf.nn.relu)]) + h0 = model(z) + model2 = tf.keras.Sequential([tf.keras.layers.Dense(2 * dx)]) + return model2(h0) # Q-model approximating P def encoder(x, d0, k): # dx -> d0 -> 2*k - h0 = tf.layers.dense(x, d0, tf.nn.relu) - return tf.layers.dense(h0, 2 * k) + """h0 = tf.layers.dense(x, d0, tf.nn.relu) + return tf.layers.dense(h0, 2 * k)""" + model = tf.keras.Sequential([tf.keras.layers.Dense(d0, tf.nn.relu)]) + h0 = model(x) + model2 = tf.keras.Sequential([tf.keras.layers.Dense(2 * k)]) + return model2(h0) @inf.probmodel diff --git a/examples/probzoo/vae_mnist_inf_bay_layers.py b/examples/probzoo/vae_mnist_inf_bay_layers.py index 3cb6292..805043b 100644 --- a/examples/probzoo/vae_mnist_inf_bay_layers.py +++ b/examples/probzoo/vae_mnist_inf_bay_layers.py @@ -1,10 +1,8 @@ -# import tensorflow as tf +import tensorflow as tf import inferpy as inf from inferpy.data import mnist import tensorflow_probability as tfp -import tensorflow.compat.v1 as tf -tf.disable_v2_behavior() N, M = 1000, 100 # data and batch size (x_train, _), _ = mnist.load_data(num_instances=N, diff --git a/inferpy/inference/variational/vi.py b/inferpy/inference/variational/vi.py index 4b08d68..6150a13 100644 --- a/inferpy/inference/variational/vi.py +++ b/inferpy/inference/variational/vi.py @@ -164,7 +164,9 @@ def _generate_train_tensor(self, extra_loss_tensor, **kwargs): if extra_loss_tensor is not None: loss_tensor += extra_loss_tensor # use the optimizer to create the train tensor - train = self.optimizer.minimize(loss_tensor) + # train = self.optimizer.minimize(loss_tensor) + tvars = tf.trainable_variables() + train = self.optimizer.minimize(loss_tensor, var_list=tvars) # save the expanded variables and parameters self.expanded_variables = {