%matplotlib inline
import theano
floatX = theano.config.floatX
import pymc3 as pm
import theano.tensor as T
import sklearn
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from warnings import filterwarnings
filterwarnings('ignore')
sns.set_style('white')
from sklearn import datasets
from sklearn.preprocessing import scale
from sklearn.cross_validation import train_test_split
from sklearn.datasets import make_moons
X, Y = make_moons(noise=0.2, random_state=0, n_samples=1000)
X = scale(X)
X = X.astype(floatX)
Y = Y.astype(floatX)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
with neural_network:
inference_no_s = pm.ADVI()
approx_no_s = pm.fit(n=30000, method=inference_no_s)
trace = approx_no_s.sample(draws=5000)
###NOTE: The original ipynb is using: trace = approx.sample_vp(draws=5000), but approx is 'MeanField' object that has no attribute 'sample_vp', so i change it to : trace = approx_no_s.sample(draws=5000)###
in ()
1 ann_input.set_value(X_test)
2 ann_output.set_value(Y_test)
----> 3 ppc = pm.sample_ppc(trace, model=neural_network, samples=500, progressbar=False)
4
C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\sampling.py in sample_ppc(trace, samples, model, vars, size, random_seed, progressbar)
526 for var in vars:
527 ppc[var.name].append(var.distribution.random(point=param,
--> 528 size=size))
529
530 return {k: np.asarray(v) for k, v in ppc.items()}
C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\discrete.py in random(self, point, size, repeat)
152
153 def random(self, point=None, size=None, repeat=None):
--> 154 p = draw_values([self.p], point=point)
155 return generate_samples(stats.bernoulli.rvs, p,
156 dist_shape=self.shape,
C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\distribution.py in draw_values(params, point)
183 if not isinstance(node, (tt.sharedvar.TensorSharedVariable,
184 tt.TensorConstant)):
--> 185 givens[name] = (node, draw_value(node, point=point))
186 values = [None for _ in params]
187 for i, param in enumerate(params):
C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\distribution.py in draw_value(param, point, givens)
251 except:
252 shape = param.shape
--> 253 if len(shape) == 0 and len(value) == 1:
254 value = value[0]
255 return value
TypeError: object of type 'TensorVariable' has no len()
When i changed from pymc3 to pymc3.1 i have a problem with sample_ppc(). The error was not only with ADVI but with Metropolis and NUTS too. I am providing the same example as the original note book https://github.com/pymc-devs/pymc3/blob/master/docs/source/notebooks/bayesian_neural_network_opvi-advi.ipynb
%matplotlib inline import theano floatX = theano.config.floatX import pymc3 as pm import theano.tensor as T import sklearn import numpy as np import matplotlib.pyplot as plt import seaborn as sns from warnings import filterwarnings filterwarnings('ignore') sns.set_style('white') from sklearn import datasets from sklearn.preprocessing import scale from sklearn.cross_validation import train_test_split from sklearn.datasets import make_moons
X, Y = make_moons(noise=0.2, random_state=0, n_samples=1000) X = scale(X) X = X.astype(floatX) Y = Y.astype(floatX) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
fig, ax = plt.subplots() ax.scatter(X[Y==0, 0], X[Y==0, 1], label='Class 0') ax.scatter(X[Y==1, 0], X[Y==1, 1], color='r', label='Class 1') sns.despine(); ax.legend() ax.set(xlabel='X', ylabel='Y', title='Toy binary classification data set');
def construct_nn(ann_input, ann_output): n_hidden = 5
ann_input = theano.shared(X_train) ann_output = theano.shared(Y_train) neural_network = construct_nn(ann_input, ann_output)
%%time
with neural_network: inference_no_s = pm.ADVI() approx_no_s = pm.fit(n=30000, method=inference_no_s)
trace = approx_no_s.sample(draws=5000)
###NOTE: The original ipynb is using: trace = approx.sample_vp(draws=5000), but approx is 'MeanField' object that has no attribute 'sample_vp', so i change it to : trace = approx_no_s.sample(draws=5000)###
ann_input.set_value(X_test) ann_output.set_value(Y_test) ppc = pm.sample_ppc(trace, model=neural_network, samples=500, progressbar=False)
pred = ppc['out'].mean(axis=0) > 0.5
And the error i got was:
TypeError Traceback (most recent call last)