我正在尝试将keras 2.0与pymc3结合起来构建一个神经网络。它是Thomas Weicki's Bayesian deep learning II代码的修改 这是我的代码:
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
答案 0 :(得分:0)
不幸的是,使用Keras 2.0,您无法再使用符号初始值设定项来表示权重。尝试降级到Keras 1.2它会起作用。
请参阅以下问题以供参考: