我正在尝试使用pymc3与贝叶斯建立神经网络。它由1个输入层,2个隐藏层和1个输出层组成。在每个隐藏层中,它都有5个节点。
当我尝试拥有的代码时,它返回一条错误消息,我不确定该如何解决。
这是代码
import os, sys
import pymc3 as pm
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import theano.tensor as tt
Input data
X = [1,2,3,4,....,2880]
Y = [2,3,4,5,....,2881]
Train/Test set
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.2)
ann_input = theano.shared(np.array(X_train))
ann_output = theano.shared(np.array(Y_train))
Initialize random weights between each layer
init_1 = np.random.randn(X.shape[1], n_hidden).astype(float)
init_2 = np.random.randn(n_hidden, n_hidden).astype(float)
init_out = np.random.randn(n_hidden).astype(float)
with pm.Model() as neural_network:
n_hidden = 5. ## nodes
# 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))
# Softmax
p = tt.nnet.softmax(act_out)
out = pm.Categorical('out', p=p, observed=ann_output)
我遇到了以下错误消息:
输入尺寸不匹配。
(input[0].shape[1] = 2880, input[1].shape[1] = 1)
这是整个错误消息:
File "/BNN/nn.py", line 65, in construct_nn out = pm.Categorical('out', p=p, observed=ann_output) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/pymc3/distributions/distribution.py", line 46, in __new__ return model.Var(name, dist, data, total_size) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/pymc3/model.py", line 856, in Var total_size=total_size, model=self) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/pymc3/model.py", line 1389, in __init__ self.logp_elemwiset = distribution.logp(data) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/pymc3/distributions/discrete.py", line 1006, in logp tt.all(p_ >= 0, axis=-1), tt.all(p <= 1, axis=-1)) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/pymc3/distributions/dist_math.py", line 50, in bound return tt.switch(alltrue(conditions), logp, -np.inf) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/theano/gof/op.py", line 674, in __call__ required = thunk() File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/theano/gof/op.py", line 862, in rval thunk() File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/theano/gof/cc.py", line 1739, in __call__ reraise(exc_type, exc_value, exc_trace) File "/Users/miniconda3/envs/bnn/lib/python3.6/site-packages/six.py", line 693, in reraise raise value ValueError: Input dimension mis-match. (input[0].shape[1] = 2880, input[1].shape[1] = 1)```