【问题标题】:How to build bayesian network from ANN using tensorflow?如何使用 tensorflow 从 ANN 构建贝叶斯网络?
【发布时间】:2018-07-11 14:52:50
【问题描述】:

我是机器学习的新手。我想建立贝叶斯神经网络。我以前有人工神经网络,我想用它来构建贝叶斯网络。我尝试这样做是因为我想比较 ANN 和 BN 预测结果的结果,所以我认为两个程序的结构必须相同,例如 epoch 和隐藏层的总和,除了模型结构或 ANN 的层结构和国阵。这是我的 ANN 代码:

import tensorflow as tf
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')


def neural_network_model(data):
    hidden_1_layer = {'weights': tf.Variable(tf.random_normal([784, n_nodes_hl1])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases': tf.Variable(tf.random_normal([n_classes])), }

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output


def train_neural_network(x):
    prediction = neural_network_model(x)
    # OLD VERSION:
    # cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
    # NEW:
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10
    with tf.Session() as sess:
        # OLD:
        # sess.run(tf.initialize_all_variables())
        # NEW:
        sess.run(tf.global_variables_initializer())

        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)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c

            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))


train_neural_network(x)

我已经阅读了关于贝叶斯网络的tutorial,但我还不够了解。那么,我可以修改上面的 ANN 代码来构建贝叶斯网络吗?

【问题讨论】:

    标签: neural-network bayesian-networks


    【解决方案1】:

    这有点晚了,但如果你还在做这项工作,你可以看看这篇论文https://papers.nips.cc/paper/1211-learning-bayesian-belief-networks-with-neural-network-estimators.pdf 以了解实现的想法,here 以了解相关参考的顺利介绍

    您可能必须实现自己的损失函数和优化器,您应该查看这些答案 herehere 开始

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 2017-11-12
      • 2020-03-25
      • 2011-01-09
      • 2012-07-14
      • 2013-05-11
      • 2013-07-19
      • 2019-11-05
      相关资源
      最近更新 更多