贝叶斯网络中的尺寸必须相等

时间:2018-02-02 08:58:51

标签: python machine-learning neural-network deep-learning bayesian-networks

我有贝叶斯网络代码来训练mnist数据集,如下所示:

import edward as ed
import tensorflow as tf
from edward.models import Normal
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

x_ = tf.placeholder('float', [None, 784])
y_ = tf.placeholder('float', shape=(batch_size))

# def neural_network_model(data):
w_h1 = Normal(loc=tf.zeros([784, n_nodes_hl1]), scale=tf.ones([784, n_nodes_hl1]))
w_h2 = Normal(loc=tf.zeros([n_nodes_hl1, n_nodes_hl2]), scale=tf.ones([n_nodes_hl1, n_nodes_hl2]))
w_h3 = Normal(loc=tf.zeros([n_nodes_hl2, n_nodes_hl3]), scale=tf.ones([n_nodes_hl2, n_nodes_hl3]))
w_o = Normal(loc=tf.zeros([n_nodes_hl3, n_classes]), scale=tf.ones([n_nodes_hl3, n_classes]))

b_h1 = Normal(loc=tf.zeros([n_nodes_hl1]), scale=tf.ones([n_nodes_hl1]))
b_h2 = Normal(loc=tf.zeros([n_nodes_hl2]), scale=tf.ones([n_nodes_hl2]))
b_h3 = Normal(loc=tf.zeros([n_nodes_hl3]), scale=tf.ones([n_nodes_hl3]))
b_o = Normal(loc=tf.zeros([n_classes]), scale=tf.ones([n_classes]))

y_pre = Normal(tf.matmul(x_, w_o) + b_o, scale=1.0)

qw_h1 = Normal(loc=tf.Variable(tf.random_normal([784, n_nodes_hl1])),
               scale=tf.Variable(tf.random_normal([784, n_nodes_hl1])))
qw_h2 = Normal(loc=tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
               scale=tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])))
qw_h3 = Normal(loc=tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
               scale=tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])))
qw_o = Normal(loc=tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
              scale=tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])))

qb_h1 = Normal(loc=tf.Variable(tf.random_normal([n_nodes_hl1])), scale=tf.Variable(tf.random_normal([n_nodes_hl1])))
qb_h2 = Normal(loc=tf.Variable(tf.random_normal([n_nodes_hl2])), scale=tf.Variable(tf.random_normal([n_nodes_hl2])))
qb_h3 = Normal(loc=tf.Variable(tf.random_normal([n_nodes_hl3])), scale=tf.Variable(tf.random_normal([n_nodes_hl3])))
qb_o = Normal(loc=tf.Variable(tf.random_normal([n_classes])), scale=tf.Variable(tf.random_normal([n_classes])))

y = Normal(tf.matmul(x_, qw_o) + qb_o, scale=1.0)

inference = ed.KLqp({w_h1: qw_h1, b_h1: qb_h1,
                     w_h2: qw_h2, b_h2: qb_h2,
                     w_h3: qw_h3, b_h3: qb_h3,
                     w_o: qw_o, b_o: qb_o, }, data={y_pre: y_})
inference.initialize()

sess = tf.Session()
sess.run(tf.global_variables_initializer())
hm_epochs = 10
with sess:
    for epoch in range(hm_epochs):
        epoch_loss = 0
        for _ in range(int(mnist.train.num_examples / batch_size)):
            epoch_x, epoch_y = mnist.train.next_batch(batch_size)
            inference.update(feed_dict={x_: epoch_x, y_: epoch_y})

我通过组合来自pythonprogramming.net tutorial的神经网络和来自this link的贝叶斯网络来实现它。但是,有错误:

  

ValueError:尺寸必须相等,但“MatMul”为784和500   (op:'MatMul')输入形状:[?,784],[500,10]。

那么,上面的错误是什么意思?怎么解决?

0 个答案:

没有答案