Tensorflow Session

必须先构建(定义)graph,之后才能运行(启动)graph。

tf.Graph

两种用于运行计算图(graph)的会话(session)

tf.Session()

tf.InteractivesSession()

tf.Session()

fetch 取回

feed 注入

用途

  1. 临时替代graph中任意操作的输入tensor
  2. 对graph中任何op提交补丁(插入一个tensor)

使用方式

  1. feed_dict提供数据,作为sess.run( )调用的参数。使用tf.placeholder( ),为某些操作的输入创建占位符。
  2. 如果没有正确提供tf.placeholder( ),feed操作将产生错误。
  3. feed注入的值不能是tf的tensor对象,应该是Python常量、字符串、列表、numpy ndarrays,TensorHandles。

tf.InteractivesSession()

当python编辑环境是shell、IPython等交互式环境时,用方法tensor.eval( ),operation.run( ) 代替sess.run( )

with

上下文管理器在对象内实现了两个方法:enter() 和__exit__()
enter()方法会在with的代码块执行之前执行,exit()会在代码块执行结束后执行。
exit()方法内会自带当前对象的清理方法。

猜你喜欢

转载自blog.csdn.net/qq_21980099/article/details/85247862