tensorflow - 使用 tensorflow 实现 RBM

标签 tensorflow

我正在尝试使用tensorflow实现RBM,代码如下:

rbm.py

""" An rbm implementation for TensorFlow, based closely on the one in Theano """
import tensorflow as tf
import math
def sample_prob(probs):
    return tf.nn.relu(
        tf.sign(
            probs - tf.random_uniform(probs.get_shape())))
class RBM(object):
    def __init__(self, name, input_size, output_size):
        with tf.name_scope("rbm_" + name):
            self.weights = tf.Variable(
                tf.truncated_normal([input_size, output_size],
                    stddev=1.0 / math.sqrt(float(input_size))), name="weights")
            self.v_bias = tf.Variable(tf.zeros([input_size]), name="v_bias")
            self.h_bias = tf.Variable(tf.zeros([output_size]), name="h_bias")

    def propup(self, visible):
        return tf.nn.sigmoid(tf.matmul(visible, self.weights) + self.h_bias)

    def propdown(self, hidden):
        return tf.nn.sigmoid(tf.matmul(hidden, tf.transpose(self.weights)) + self.v_bias)

    def sample_h_given_v(self, v_sample):
        return sample_prob(self.propup(v_sample))

    def sample_v_given_h(self, h_sample):
        return sample_prob(self.propdown(h_sample))

    def gibbs_hvh(self, h0_sample):
        v_sample = self.sample_v_given_h(h0_sample)
        h_sample = self.sample_h_given_v(v_sample)
        return [v_sample, h_sample]

    def gibbs_vhv(self, v0_sample):
        h_sample = self.sample_h_given_v(v0_sample)
        v_sample = self.sample_v_given_h(h_sample)
        return  [h_sample, v_sample]

    def cd1(self, visibles, learning_rate=0.1):
        h_start = self.propup(visibles)
        v_end = self.propdown(h_start)
        h_end = self.propup(v_end)
        w_positive_grad = tf.matmul(tf.transpose(visibles), h_start)
        w_negative_grad = tf.matmul(tf.transpose(v_end), h_end)
        update_w = self.weights.assign_add(learning_rate * (w_positive_grad - w_negative_grad))
        update_vb = self.v_bias.assign_add(learning_rate * tf.reduce_mean(visibles - v_end, 0))
        update_hb = self.h_bias.assign_add(learning_rate * tf.reduce_mean(h_start - h_end, 0))
        return [update_w, update_vb, update_hb]

    def reconstruction_error(self, dataset):
        err = tf.stop_gradient(dataset - self.gibbs_vhv(dataset)[1])
        return tf.reduce_sum(err * err)
<小时/>

rbm_MNIST_test.py

import tensorflow as tf
import numpy as np
import rbm
import input_data

def build_model(X, w1, b1, wo, bo):
    h1 = tf.nn.sigmoid(tf.matmul(X, w1)+b1)
    model = tf.nn.sigmoid(tf.matmul(h1, wo)+bo)
    return model

def init_weight(shape):
    return tf.Variable(tf.random_normal(shape, mean=0.0, stddev=0.01))

def init_bias(dim):
    return tf.Variable(tf.zeros([dim]))

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])

rbm_layer = rbm.RBM("mnist", 784, 500)

for i in range(10):
    print "RBM CD: ", i
    rbm_layer.cd1(trX)

rbm_w, rbm_vb, rbm_hb = rbm_layer.cd1(trX)


wo = init_weight([500,10])
bo = init_bias(10)
py_x = build_model(X, rbm_w, rbm_hb, wo, bo)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
predict_op = tf.argmax(py_x, 1)

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)

for i in range(10):
    for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
        sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
    print i, np.mean(np.argmax(teY, axis=1) ==
                     sess.run(predict_op, feed_dict={X: teX, Y: teY}))
<小时/>

但是出现了错误:

File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1626, in as_graph_def raise ValueError("GraphDef cannot be larger than 2GB.") ValueError: GraphDef cannot be larger than 2GB.

有人可以帮我解决这个问题吗?

最佳答案

TensorFlow 在 GraphDef 上确实有 2GB 的限制protos,它源于 Protocol Buffer 实现的限制。如果图中有很大的常量张量,您很快就会达到该限制。特别是,如果您多次使用相同 numpy 数组,TensorFlow 会向您的图中添加多个常量张量。

就您而言,mnist.train.images返回者 input_data.read_data_sets是一个 numpy float 组,形状为 (55000, 784) ,所以大约是164 MB 。您将该 numpy 数组传递给 rbm_layer.cd1 ,并在该函数内,每次使用 visibles 时,一个 TensorFlow Const节点是从 numpy 数组创建的。您使用visibiles在 3 个地点,因此每次调用 cd1图形大小增加了大约 492 MB ,这样你就很容易超过限制了。解决方案是创建一个 TensorFlow 常量一次并将该常量传递给 cd1功能如下:

trX_constant = tf.constant(trX)
for i in range(10):
    print "RBM CD: ", i
    rbm_layer.cd1(trX_constant)

顺便说一句,我不确定您在上述循环中的意图是什么。请注意 cd1函数只需添加 assign_add节点到图,并且实际上不执行分配。如果您确实希望在训练时发生这些分配,您应该考虑通过控制依赖项将这些分配链接到最终的 train_op节点。

关于tensorflow - 使用 tensorflow 实现 RBM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760981/

相关文章:

tensorflow - alexnet 分布式 tensorflow 性能

python - 我如何使用 tf.reshape()?

machine-learning - 如何在分割网中反向支撑

python - "LookupError: Function ` __class__ ` does not exist."使用 tf.function 时

tensorflow - 如何在Tensorflow中通过自定义概率分布进行采样?

python - 如何在图像分割中将 tf.Dataset 与 TIFF 文件一起使用?

tensorflow - 如何在 tensorflow 中将 TextVectorization 保存到磁盘?

python - 我如何使用 tensorflow 2 进行均衡学习率?

python - tensorflow/models.. 在 Windows 中的位置

python - Keras后端json定义为tensorflow,但是Keras仍然找不到tensorflow