import inferpy as inf
import numpy as np
import tensorflow as tf
@inf.probmodel
def pca(k,d):
w = inf.Normal(loc=np.zeros([k,d]), scale=1, name="w") # shape = [k,d]
with inf.datamodel():
z = inf.Normal(np.ones([k]),1, name="z") # shape = [N,k]
x = inf.Normal(z @ w , 1, name="x") # shape = [N,d]
# generate training data
N = 1000
sess = tf.Session()
x_train = np.concatenate([inf.Normal([0.0,0.0], scale=1.).sample(int(N/2)), inf.Normal([10.0,10.0], scale=1.).sample(int(N/2))])
@inf.probmodel
def qmodel(k,d):
qw_loc = inf.Parameter(tf.ones([k,d]), name="qw_loc")
qw_scale = tf.math.softplus(inf.Parameter(tf.ones([k, d]), name="qw_scale"))
qw = inf.Normal(qw_loc, qw_scale, name="w")
with inf.datamodel():
qz_loc = inf.Parameter(tf.ones([k]), name="qz_loc")
qz_scale = tf.math.softplus(inf.Parameter(tf.ones([k]), name="qz_scale"))
qz = inf.Normal(qz_loc, qz_scale, name="z")
# set the inference algorithm
VI = inf.inference.VI(qmodel(k=1,d=2), epochs=1000)
# create an instance of the model
m = pca(k=1,d=2)
# run the inference
m.fit({"x": x_train}, VI)
I get the error:
Traceback (most recent call last):
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 527, in _apply_op_helper
preferred_dtype=default_dtype)
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1224, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1018, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype float64 for Tensor with dtype float32: 'Tensor("Neg:0", shape=(), dtype=float32)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-8b91a39c8a1e>", line 49, in <module>
m.fit({"x": x_train}, VI)
File "[...]/inferpy/util/runtime.py", line 86, in wrapper
return f(*args, **kwargs)
File "[...]/inferpy/models/prob_model.py", line 199, in fit
inference_method.compile(self, plate_size)
File "[...]/inferpy/inference/variational/vi.py", line 76, in compile
self.train_tensor = self._generate_train_tensor(plate_size=self.plate_size)
File "[...]/inferpy/inference/variational/vi.py", line 133, in _generate_train_tensor
loss_tensor = self.loss_fn(pvars, qvars, **kwargs)
File "[...]/inferpy/inference/variational/loss_functions/elbo.py", line 26, in ELBO
ELBO = energy + entropy
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 884, in binary_op_wrapper
return func(x, y, name=name)
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 387, in add
"Add", x=x, y=y, name=name)
File "/Users/rcabanas/venv/InferPy/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 563, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Add' Op has type float32 that does not match type float64 of argument 'x'.
When running the following code:
I get the error: