2017-05-04 73 views
0

我想將keras 2.0和pymc3組合起來構建一個神經網絡。這是代碼的從Thomas Weicki's Bayesian deep learning II 這是一個修改是我的代碼:組合Keras 2.0和pymc3的問題

import numpy as np 
import pymc3 as pm 
import theano 
import theano.tensor as T 
from keras.layers import Input, Dense 
from keras import backend as K 
from sklearn import datasets 
from sklearn.preprocessing import scale 
from sklearn.cross_validation import train_test_split 
from sklearn.datasets import make_moons 
from scipy.stats import mode 

X, Y = make_moons(noise=0.2, random_state=0, n_samples=1000) 
X = scale(X) 

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.6) 

ann_input = theano.shared(X_train.astype(np.float32)) 
ann_output = theano.shared(Y_train.astype(np.float32)) 

print (X_train.shape) 
print (Y_train.shape) 


class GaussWeights(object): 
    def __init__(self): 
     self.count = 0 

    def __call__(self, shape, name='w',dtype=None): 
     return pm.Normal(
     name, mu=0, sd=.1, 
     testval=K.random_normal(shape,dtype=dtype), 
     shape=shape) 

n_hidden = 16 

def build_ann(x, y, init): 
    b = (T.ones_like(x[:])) 
    rows = b.shape.eval()[0] 
    cols = b.shape.eval()[1] 
    with pm.Model() as m: 
     i = Input(tensor=x, shape=(rows,cols)) 
     layer1 = Dense(16,kernel_initializer=init, activation='tanh')(i) 
     layer2 = Dense(1, kernel_initializer=init, activation='sigmoid')(layer1) 
     layer2 = layer2.reshape((rows,)) 
     out = pm.Bernoulli('out', layer2, observed=y) 
    return m, out 

#m,out = build_ann(ann_input, ann_output) 
m,out = build_ann(ann_input, ann_output, GaussWeights()) 

with m: 
    #Run ADVI which returns posterior means, standard deviations, and the evidence lower bound (ELBO) 
    ann_input.set_value(X_train.astype(np.float32)) 
    ann_output.set_value(Y_train.astype(np.float32)) 
    v_params = pm.variational.advi(n=50000) 
    trace = pm.variational.sample_vp(v_params, draws=5000) 

# Replace shared variables with testing set 
ann_input.set_value(X_test.astype(np.float32)) 
ann_output.set_value(Y_test.astype(np.float32)) 

with m: 

    ppc = pm.sample_ppc(trace, samples=500) 

    # Use probability of > 0.5 to assume prediction of class 1 

    pred = ppc['out'].mean(axis=0) > 0.5 
    pred_mode = mode(ppc['out'], axis=0).mode[0, :] 

    print (pred.shape) 

print('Accuracy = {}%'.format((Y_test == pred).mean() * 100)) 

,但我得到了下面的錯誤,我不知道如何解決:

回溯(最近最後調用):

File "keras_deep_learning.py", line 50, in <module> 
    m,out = build_ann(ann_input, ann_output, GaussWeights()) 
    File "keras_deep_learning.py", line 43, in build_ann 
    layer1 = Dense(16,kernel_initializer=init, activation='tanh')(i) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/keras/engine/topology.py", line 558, in __call__ 
    self.build(input_shapes[0]) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/keras/layers/core.py", line 827, in build 
    constraint=self.kernel_constraint) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 88, in wrapper 
    return func(*args, **kwargs) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/keras/engine/topology.py", line 391, in add_weight 
    weight = K.variable(initializer(shape), dtype=dtype, name=name) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/keras/backend/theano_backend.py", line 143, in variable 
    value = value.eval() 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/gof/graph.py", line 516, in eval 
    self._fn_cache[inputs] = theano.function(inputs, self) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/compile/function.py", line 326, in function 
    output_keys=output_keys) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/compile/pfunc.py", line 486, in pfunc 
    output_keys=output_keys) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/compile/function_module.py", line 1794, in orig_function 
    output_keys=output_keys).create(
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/compile/function_module.py", line 1446, in __init__ 
    accept_inplace) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/compile/function_module.py", line 177, in std_fgraph 
    update_mapping=update_mapping) 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/gof/fg.py", line 180, in __init__ 
    self.__import_r__(output, reason="init") 
    File "/home/gbenga/.local/lib/python3.5/site-packages/theano/gof/fg.py", line 361, in __import_r__ 
    raise MissingInputError("Undeclared input", variable=variable) 
theano.gof.fg.MissingInputError: Undeclared input 

回答