graph

tensorflow中graph包含一些操作对象,这些对象就是计算节点。而tensor表示的是不同操作间的数据节点。

tensorflow会创建默认的图,可以通过tf.get_default_graph()来访问:

代码:

import tensorflow as tf
import numpy as np

c=tf.constant(value=1)
#print(assert c.graph is tf.get_default_graph())
print(c.graph)
print(tf.get_default_graph())

结果:

 

如何使用自定义的graph,使用Graph.as_default()的上下文管理器:

代码:

import tensorflow as tf
import numpy as np

c=tf.constant(value=1)
#print(assert c.graph is tf.get_default_graph())
print(c.graph)
print(tf.get_default_graph())

g=tf.Graph()
print("g:",g)
with g.as_default():
  d=tf.constant(value=2)
  print(d.graph)
  #print(g)

g2=tf.Graph()
print("g2:",g2)
g2.as_default()
e=tf.constant(value=15)
print(e.graph)

结果:

分析:第一个总共有一个graph,就是c默认创建的;第二种情况:先创建了图g,然后在g下声明变量,因此g覆盖了d声明时默认的graph。第三种情况,就是有两个图。

 

tf.train.write_graph(g1.as_graph_def(),'.','graph.pb',False)  保存模型,但是它只是保存了模型的结构,并不保存训练完毕的参数值

tf.train.import_meta_graph函数给出model.ckpt-n.meta的路径后会加载图结构,并返回saver对象

tf.train.saver()保存模型,因为它只是保存了网络中的参数值,并不保存模型的结构。

tf.train.get_checkpoint_state(),可以用来检查是否有保存的checkpoint,

 

转载于:https://www.cnblogs.com/smartwhite/p/8989413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值