Я пытаюсь построить нейронную сеть с байесовским с использованием 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)```