pymc-devs / pymc2

THIS IS THE **OLD** PYMC PROJECT (VERSION 2). PLEASE USE PYMC INSTEAD:
http://pymc-devs.github.com/pymc/
Other
879 stars 229 forks source link

sample_ppc() problem with pymc3.1 #159

Closed nkoudounas closed 7 years ago

nkoudounas commented 7 years ago

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

# Initialize random weights between each layer
init_1 = np.random.randn(X.shape[1], n_hidden).astype(floatX)
init_2 = np.random.randn(n_hidden, n_hidden).astype(floatX)
init_out = np.random.randn(n_hidden).astype(floatX)

with pm.Model() as neural_network:
    # Weights from input to hidden layer
    weights_in_1 = pm.Normal('w_in_1', 0, sd=1, 
                             shape=(X.shape[1], n_hidden), 
                             testval=init_1)

    # Weights from 1st to 2nd layer
    weights_1_2 = pm.Normal('w_1_2', 0, sd=1, 
                            shape=(n_hidden, n_hidden), 
                            testval=init_2)

    # Weights from hidden layer to output
    weights_2_out = pm.Normal('w_2_out', 0, sd=1, 
                              shape=(n_hidden,), 
                              testval=init_out)

    # Build neural-network using tanh activation function
    act_1 = pm.math.tanh(pm.math.dot(ann_input, 
                                     weights_in_1))
    act_2 = pm.math.tanh(pm.math.dot(act_1, 
                                     weights_1_2))
    act_out = pm.math.sigmoid(pm.math.dot(act_2, 
                                          weights_2_out))

    # Binary classification -> Bernoulli likelihood
    out = pm.Bernoulli('out', 
                       act_out,
                       observed=ann_output,
                       total_size=Y_train.shape[0] # IMPORTANT for minibatches
                      )
return neural_network

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)

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()
junpenglao commented 7 years ago

Hi @aplamhden, could you please move this issue to https://github.com/pymc-devs/pymc3/?

nkoudounas commented 7 years ago

Done. Thank you

junpenglao commented 7 years ago

Thanks! Could you also close this please ;-)

nkoudounas commented 7 years ago

Maybe i am too lazy. Of course. 🥇