我在Tensorflow 0.10下运行我的代码,但在将版本更新为0.12.1后,所有变量都抛出FailedPreconditionError: Attempting to use uninitialized value W error
。除了我使用tf.global_variables_initializer()
而不是tf.initialize_all_variables()
之外,我的代码保持不变。我尝试在tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
下添加import tensorflow as tf
,但它无效。所以我打开这个问题因为我不知道错误在哪里。
我的代码如下。省略了一些更具可读性的细节。
并且错误每次都不同。所有tf.Variable
个已定义的变量都会出错。
class model(object):
def __init__(self, paras):
self.D = tf.constant(D, dtype = tf.float32)
self.Q = tf.constant(Q, dtype = tf.float32)
if self.USE_FEATURE:
self.CF = tf.Variable((np.random.rand(self.rank, d_F) - 0.5) / self.rank, \
dtype = tf.float32, name = 'CF') #error here
self.W = tf.Variable((np.random.rand(self.rank, sample_num) - 0.5) / self.rank / 200, \
dtype = tf.float32, name = 'W') #error here
self.C = tf.Variable((np.random.rand(context_num, self.rank) - 0.5) / self.rank, \
dtype = tf.float32, name = 'C') #error here
ED = tf.transpose(self.Q) * (1.0 / (1.0 + tf.exp(- tf.matmul(self.C, self.W))))
recons = self.D - ED
W_grad = tf.matmul(tf.transpose(self.C), recons)
self.W_grad = tf.Variable(W_grad, dtype = tf.float32) #error here
self._build_update_W_grad()
if not self.USE_FEATURE:
self._build_alter_W()
else:
self._build_W_with_F()
self._build_alter_C()
def _run(self, sess):
tf.initialize_all_variables().run() #where the error throws
for i in xrange(self.max_iter):
if (i + 1) % self.prun_step == 0:
self.mu = self.mu * self.prun_rate
if (i + 1) % 2 == 1:
for j in xrange(self.inner_maxiter):
if not self.USE_FEATURE:
self.up_W_grad.run()
self.up_W.run()
else:
self.up_W_grad.run()
for k in xrange(self.sgd_batch):
self.up_W_CF.run()
#raise NotImplementedError
else:
for j in xrange(self.inner_maxiter):
self.up_C.run()
W = self.W.eval()
C = self.C.eval()
print 'end training. save W and C'
return W, C
def _build_alter_W(self):
#codes
self.up_W = tf.group(updata_W)
def _build_W_with_F(self):
#codes
self.up_W_CF = tf.group(updata_W, updata_CF)
#raise NotImplementedError
def _build_alter_C(self):
#codes
self.up_C = tf.group(updata_C)
def _build_update_W_grad(self):
#codes
self.up_W_grad = tf.group(update_W_grad)
#main program
train_epoch = model(paras)
with tf.Session(config = config) as sess:
W, C = train_epoch._run(sess)
有人可以帮忙吗?该程序在0.10中运行良好,但在我更新到0.12.1后崩溃了。我只更改了tf.initialize_all_variables().run()
到tf.global_variables_initializer().run()
。