FailedPreconditionError: Error while reading resource variable Variable_16 from Container: ...

一、问题描述

在用tensorflow2.0版本时,用

with tf.compat.v1.Session() as sess:
    pre=sess.run(prediction,feed_dict={x:x_test[0:2],keep_prob:1})
    print(pre)

出现错误如下

FailedPreconditionError: Error while reading resource variable Variable_16 from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/Variable_16)
	 [[{{node Conv2D_4/ReadVariableOp}}]]

 解决方案是没有对变量初始化,改正如下

with tf.compat.v1.Session() as sess:
    sess.run(tf.compat.v1.global_variables_initializer())
    pre=sess.run(prediction,feed_dict={x:x_test[0:2],keep_prob:1})
    print(pre)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常表示你的 TensorFlow 图中的某些操作依赖于尚未初始化的变量或张量。这可能是因为你尝试在使用它之前对变量进行了操作,或者因为你需要在使用图之前显式初始化变量。 解决此问题的一种方法是在使用 TensorFlow 图之前显式初始化变量。你可以使用 tf.global_variables_initializer() 函数来初始化所有全局变量,例如: ``` import tensorflow as tf # 定义变量 x = tf.Variable(0, name='x') # 初始化变量 init = tf.global_variables_initializer() # 创建会话并运行初始化操作 with tf.Session() as sess: sess.run(init) # 在此处继续执行图操作 ``` 另一种常见的原因是在多个 TensorFlow 会话之间共享变量。在这种情况下,你可以使用 tf.train.Saver 类来保存和恢复变量。例如: ``` import tensorflow as tf # 定义变量 x = tf.Variable(0, name='x') # 创建 Saver 对象 saver = tf.train.Saver() # 创建第一个会话并初始化变量 with tf.Session() as sess1: sess1.run(tf.global_variables_initializer()) # 在 sess1 中对变量进行操作 x += 1 # 保存变量到磁盘 saver.save(sess1, './model.ckpt') # 创建第二个会话并恢复变量 with tf.Session() as sess2: # 从磁盘中加载变量 saver.restore(sess2, './model.ckpt') # 在 sess2 中继续对变量进行操作 x += 1 ``` 在这个例子中,我们首先定义了一个变量 x,然后创建了一个 Saver 对象。在第一个会话中,我们初始化变量并对其执行一些操作,然后使用 Saver 将变量保存到磁盘。在第二个会话中,我们使用 Saver 来恢复变量,然后继续对其执行操作。注意,我们没有调用 tf.global_variables_initializer(),因为变量已经被保存和恢复了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值