tf.reset_default_graph()

tf.reset_default_graph函数用于清除默认图形堆栈并重置全局默认图形。 

注意:默认图形是当前线程的一个属性。该tf.reset_default_graph函数只适用于当前线程。当一个tf.Session或者tf.InteractiveSession激活时调用这个函数会导致未定义的行为。调用此函数后使用任何以前创建的tf.Operation或tf.Tensor对象将导致未定义的行为。

 

复制代码
with tf.variable_scope('Space_a'):
    a = tf.constant([1,2,3])
with tf.variable_scope('Space_b'):
    b = tf.constant([7,8,9])
with tf.variable_scope('Space_c'):
    c= a + b
d = a+b
with tf.Session() as sess:
    print(a)
    print(b)
    print(c)
    print(d)
    print(sess.run(c))
    print(sess.run(d))
复制代码

 

 

Tensor("Space_a/Const:0", shape=(3,), dtype=int32)
Tensor("Space_b/Const:0", shape=(3,), dtype=int32)
Tensor("Space_c/add:0", shape=(3,), dtype=int32)
Tensor("add:0", shape=(3,), dtype=int32)
[ 8 10 12]
[ 8 10 12]

 

 

如何清除每次运行时,tensorflow中不断增加的节点并重置整个defualt graph?

 

复制代码
tf.reset_default_graph()#清空dedault graph以及nodes
with tf.variable_scope('Space_a'):
    a = tf.constant([1,2,3])
with tf.variable_scope('Space_b'):
    b = tf.constant([7,8,9])
with tf.variable_scope('Space_c'):
    c= a + b
d = a+b
with tf.Session() as sess:
    print(a)
    print(b)
    print(c)
    print(d)
    print(sess.run(c))
    print(sess.run(d))
复制代码

 

 

Tensor("Space_a/Const:0", shape=(3,), dtype=int32)
Tensor("Space_b/Const:0", shape=(3,), dtype=int32)
Tensor("Space_c/add:0", shape=(3,), dtype=int32)
Tensor("add:0", shape=(3,), dtype=int32)
[ 8 10 12]
[ 8 10 12]

posted on   cltt  阅读(1163)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示