使用Edward时,我的TensorFlow图表异常大

时间:2017-06-18 21:14:43

标签: tensorflow bayesian

我在这里有代码,我已经从website进行了修改。基本上我写的是:

#import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
#from tensorflow.examples.tutorials.mnist import input_data
from edward.models import Categorical, Normal
import edward as ed
#ed.set_seed(39)

import pandas as pd
import csv



# Use the TensorFlow method to download and/or load the data.
with open ("data_final.csv", "r") as csvfile:
    reader1 = csv.reader(csvfile)
    data1 = np.array(list(reader1)).astype(np.float)



#mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


N = data1.shape[0] -1   # number of images in a minibatch.
D = 4   # number of features.
K = 4    # number of classes.

# Create a placeholder to hold the data (in minibatches) in a TensorFlow graph.
x = tf.placeholder(tf.float32, [N, D])
# Normal(0,1) priors for the variables. Note that the syntax assumes TensorFlow 1.1.
w = Normal(loc=tf.zeros([D, K]), scale=tf.ones([D, K]))
b = Normal(loc=tf.zeros(K), scale=tf.ones(K))
# Categorical likelihood for classication.
y =tf.matmul(x,w)+b

# Contruct the q(w) and q(b). in this case we assume Normal distributions.
qw = Normal(loc=tf.Variable(tf.random_normal([D, K])),
              scale=tf.nn.softplus(tf.Variable(tf.random_normal([D, K])))) 
qb = Normal(loc=tf.Variable(tf.random_normal([K])),
              scale=tf.nn.softplus(tf.Variable(tf.random_normal([K]))))

# We use a placeholder for the labels in anticipation of the traning data.
y_ph = tf.placeholder(tf.float32, [N, K])
# Define the VI inference technique, ie. minimise the KL divergence between q and p.
inference = ed.KLqp({w: qw, b: qb}, data={y:y_ph})

# Initialse the infernce variables
inference.initialize(n_iter=5000, n_print=100, scale={y: 1})

# We will use an interactive session.
sess = tf.InteractiveSession()
# Initialise all the vairables in the session.
tf.global_variables_initializer().run()

我使用链接here的数据来运行代码。运行代码不到一秒钟后我收到一个错误(所以我很难相信这实际发生了)说:

  

ValueError:GraphDef不能大于2GB。

我认为还有其他主题与我的错误相同,但那些人已经实例化了100万个参数。我订购了20个参数,所以不确定为什么我会收到此错误。

1 个答案:

答案 0 :(得分:1)

在我的情况下,仍然存在变量(可能是图表),这些变量并非从之前的Edward运行中收集的垃圾。垃圾收集/重置控制台解决了问题。