######(较深的应用)TensorFlow学习(三):Graph和Session ######(较深的应用)

之前讲完变量常量等等基本量的操作,意味着最基本的东西都有了,然后接下来很重要的就是那些量和操作怎么组成更大的集合,怎么运行这个集合。这些就是计算图谱graph和Session的作用了。

IV.Graph

https://www.tensorflow.org/versions/r0.11/api_docs/python/framework.html#Graph
一个TensorFlow的运算,被表示为一个数据流的图。一幅图中包含一些操作(Operation)对象,这些对象是计算节点。前面说过的Tensor对象,则是表示在不同的操作(operation)间的数据节点
一个默认的图总是被注册,也就是说,你一旦开始你的任务,就已经有一个默认的图已经创建好了。而且可以通过调用tf.get_default_graph()来访问到。添加一个操作到默认的图里面,只要简单的调用一个定义了新操作的函数就行。比如下面的例子展示的:

c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()
  
  
  • 1
  • 2
  • 1
  • 2

另外一种典型的用法就是要使用到Graph.as_default() 的上下文管理器( context manager),它能够在这个上下文里面覆盖默认的图。如下例

g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  assert c.graph is g
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

上面的例子里面创创建了一个新的图g,然后把g设为默认,那么接下来的操作不是在默认的图中,而是在g中了。你也可以认为现在g这个图就是新的默认的图了。

重要函数:
tf.Graph.__init__()

作用:
创建一个新的,空的图(graph)

tf.Graph.as_default()

作用:
返回一个使得这个图成为默认图的上下文管理器(context manager)。如果你想在同一个任务里面创造多个图,那么你应该用这个方法。
为了方便,要是你不显式创建一个新的图,一个全局的默认的图会被提供,全部的操作都会被添加到这个默认的图里面。
使用with关键字调用这个函数用来指定在这个程序块内的操作应该被添加到这个新的图里面。
默认的图是当前线程的属性,如果你创建了一个新的线程而且想在这个新线程中使用默认的图,你必须显式在这个线程函数中添加一个with g.as_default():

例:

# 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

# 2. Constructing and making default:
with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  assert c.graph is g
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

tf.Graph.as_graph_def(from_version=None, add_shapes=False)

Returns a serialized GraphDef representation of this graph.

The serialized GraphDef can be imported into another Graph (using import_graph_def()) or used with the C++ Session API.

This method is thread-safe.

Args:

from_version: Optional. If this is set, returns a GraphDef containing only the nodes that were added to this graph since its version property had the given value.
add_shapes: If true, adds an “_output_shapes” list attr to each node with the inferred shapes of each of its outputs.
Returns:

A GraphDef protocol buffer.

Raises:

ValueError: If the graph_def would be too large.

tf.Graph.finalize()

作用:结束(Finalizes)这个图,使其只读(read-only).调用这个函数之后,就没有操作能够添加到这个图里面去了。这个方法是确保当这个图被多个线程共享的时候,没有操作能够添加进去。

tf.Graph.finalized

是一个属性,要是为True的话,那么表示这个图已经被结束

tf.Graph.control_dependencies(control_inputs)

Returns a context manager that specifies control dependencies.
Use with the with keyword to specify that all operations constructed within the context should have control dependencies on control_inputs. For example:
with g.control_dependencies([a, b, c]):
# d and e will only run after a, b, and c have executed.
d = …
e = …
Multiple calls to control_dependencies() can be nested, and in that case a new Operation will have control dependencies on the union of control_inputs from all active contexts.
with g.control_dependencies([a, b]):
# Ops constructed here run after a and b.
with g.control_dependencies([c, d]):
# Ops constructed here run after a, b, c, and d.
You can pass None to clear the control dependencies:
with g.control_dependencies([a, b]):
# Ops constructed here run after a and b.
with g.control_dependencies(None):
# Ops constructed here run normally, not waiting for either a or b.
with g.control_dependencies([c, d]):
# Ops constructed here run after c and d, also not waiting
# for either a or b.
N.B. The control dependencies context applies only to ops that are constructed within the context. Merely using an op or tensor in the context does not add a control dependency. The following example illustrates this point:

WRONG

def my_func(pred, tensor):
t = tf.matmul(tensor, tensor)
with tf.control_dependencies([pred]):
# The matmul op is created outside the context, so no control
# dependency will be added.
return t

def my_func(pred, tensor):
with tf.control_dependencies([pred]):
# The matmul op is created in the context, so a control dependency
# will be added.
return tf.matmul(tensor, tensor)
Args:
control_inputs: A list of Operation or Tensor objects which must be executed or computed before running the operations defined in the context. Can also be None to clear the control dependencies.
Returns:
A context manager that specifies control dependencies for all operations constructed within the context.
Raises:
TypeError: If control_inputs is not a list of Operation or Tensor objects.

tf.Graph.device(device_name_or_function)
作用:返回一个上下文管理器(context manager)指定默认运行的设备
device_name_or_function 这个参数 可以是设备名称字符串,设备函数,或者None:

If it is a device name string, all operations constructed in this context will be assigned to the device with that name, unless overridden by a nested device() context.
If it is a function, it will be treated as a function from Operation objects to device name strings, and invoked each time a new Operation is created. The Operation will be assigned to the device with the returned name.
If it is None, all device() invocations from the enclosing context will be ignored.
For information about the valid syntax of device name strings, see the documentation in DeviceNameUtils.

For example:

with g.device('/gpu:0'):
  # All operations constructed in this context will be placed
  # on GPU 0.
  with g.device(None):
    # All operations constructed in this context will have no
    # assigned device.

# Defines a function from `Operation` to device string.
def matmul_on_gpu(n):
  if n.type == "MatMul":
    return "/gpu:0"
  else:
    return "/cpu:0"

with g.device(matmul_on_gpu):
  # All operations of type "MatMul" constructed in this context
  # will be placed on GPU 0; all other operations will be placed
  # on CPU 0.
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

N.B. The device scope may be overridden by op wrappers or other library code. For example, a variable assignment op v.assign() must be colocated with the tf.Variable v, and incompatible device scopes will be ignored.

Args:

device_name_or_function: The device name or function to use in the context.
Returns:

A context manager that specifies the default device to use for newly created ops.

tf.Graph.name_scope(name)

Returns a context manager that creates hierarchical names for operations.

A graph maintains a stack of name scopes. A with name_scope(…): statement pushes a new name onto the stack for the lifetime of the context.

The name argument will be interpreted as follows:

A string (not ending with ‘/’) will create a new name scope, in which name is appended to the prefix of all operations created in the context. If name has been used before, it will be made unique by calling self.unique_name(name).
A scope previously captured from a with g.name_scope(…) as scope: statement will be treated as an “absolute” name scope, which makes it possible to re-enter existing scopes.
A value of None or the empty string will reset the current name scope to the top-level (empty) name scope.
For example:

with tf.Graph().as_default() as g:
c = tf.constant(5.0, name=”c”)
assert c.op.name == “c”
c_1 = tf.constant(6.0, name=”c”)
assert c_1.op.name == “c_1”

# Creates a scope called “nested”
with g.name_scope(“nested”) as scope:
nested_c = tf.constant(10.0, name=”c”)
assert nested_c.op.name == “nested/c”

# Creates a nested scope called "inner".
with g.name_scope("inner"):
  nested_inner_c = tf.constant(20.0, name="c")
  assert nested_inner_c.op.name == "nested/inner/c"

# Create a nested scope called "inner_1".
with g.name_scope("inner"):
  nested_inner_1_c = tf.constant(30.0, name="c")
  assert nested_inner_1_c.op.name == "nested/inner_1/c"

  # Treats `scope` as an absolute name scope, and
  # switches to the "nested/" scope.
  with g.name_scope(scope):
    nested_d = tf.constant(40.0, name="d")
    assert nested_d.op.name == "nested/d"

    with g.name_scope(""):
      e = tf.constant(50.0, name="e")
      assert e.op.name == "e"

The name of the scope itself can be captured by with g.name_scope(…) as scope:, which stores the name of the scope in the variable scope. This value can be used to name an operation that represents the overall result of executing the ops in a scope. For example:

inputs = tf.constant(…)
with g.name_scope(‘my_layer’) as scope:
weights = tf.Variable(…, name=”weights”)
biases = tf.Variable(…, name=”biases”)
affine = tf.matmul(inputs, weights) + biases
output = tf.nn.relu(affine, name=scope)
NOTE: This constructor validates the given name. Valid scope names match one of the following regular expressions:

[A-Za-z0-9.][A-Za-z0-9_.\-/]* (for scopes at the root)
[A-Za-z0-9_.\-/]* (for other scopes)
Args:

name: A name for the scope.
Returns:

A context manager that installs name as a new name scope.

Raises:

ValueError: If name is not a valid scope name. The rules are the
A Graph instance supports an arbitrary number of “collections” that are identified by name. For convenience when building a large graph, collections can store groups of related objects: for example, the tf.Variable uses a collection (named tf.GraphKeys.VARIABLES) for all variables that are created during the construction of a graph. The caller may define additional collections by specifying a new name.

tf.Graph.add_to_collection(name, value)

Stores value in the collection with the given name.

Note that collections are not sets, so it is possible to add a value to a collection several times.

Args:

name: The key for the collection. The GraphKeys class contains many standard names for collections.
value: The value to add to the collection.
tf.Graph.add_to_collections(names, value)

Stores value in the collections given by names.

Note that collections are not sets, so it is possible to add a value to a collection several times. This function makes sure that duplicates in names are ignored, but it will not check for pre-existing membership of value in any of the collections in names.

names can be any iterable, but if names is a string, it is treated as a single collection name.

Args:

names: The keys for the collections to add to. The GraphKeys class contains many standard names for collections.
value: The value to add to the collections.
tf.Graph.get_collection(name, scope=None)

Returns a list of values in the collection with the given name.

This is different from get_collection_ref() which always returns the actual collection list if it exists in that it returns a new list each time it is called.

Args:

name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
scope: (Optional.) If supplied, the resulting list is filtered to include only items whose name attribute matches using re.match. Items without a name attribute are never returned if a scope is supplied and the choice or re.match means that a scope without special tokens filters by prefix.
Returns:

The list of values in the collection with the given name, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.

tf.Graph.get_collection_ref(name)

Returns a list of values in the collection with the given name.

If the collection exists, this returns the list itself, which can be modified in place to change the collection. If the collection does not exist, it is created as an empty list and the list is returned.

This is different from get_collection() which always returns a copy of the collection list if it exists and never creates an empty collection.

Args:

name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
Returns:

The list of values in the collection with the given name, or an empty list if no value has been added to that collection.

tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True)

Returns the object referred to by obj, as an Operation or Tensor.

This function validates that obj represents an element of this graph, and gives an informative error message if it is not.

This function is the canonical way to get/validate an object of one of the allowed types from an external argument reference in the Session API.

This method may be called concurrently from multiple threads.

Args:

obj: A Tensor, an Operation, or the name of a tensor or operation. Can also be any object with an _as_graph_element() method that returns a value of one of these types.
allow_tensor: If true, obj may refer to a Tensor.
allow_operation: If true, obj may refer to an Operation.
Returns:

The Tensor or Operation in the Graph corresponding to obj.

Raises:

TypeError: If obj is not a type we support attempting to convert to types.
ValueError: If obj is of an appropriate type but invalid. For example, an invalid string.
KeyError: If obj is not an object in the graph.
tf.Graph.get_operation_by_name(name)

Returns the Operation with the given name.

This method may be called concurrently from multiple threads.

Args:

name: The name of the Operation to return.
Returns:

The Operation with the given name.

Raises:

TypeError: If name is not a string.
KeyError: If name does not correspond to an operation in this graph.
tf.Graph.get_tensor_by_name(name)

Returns the Tensor with the given name.

This method may be called concurrently from multiple threads.

Args:

name: The name of the Tensor to return.
Returns:

The Tensor with the given name.

Raises:

TypeError: If name is not a string.
KeyError: If name does not correspond to a tensor in this graph.
tf.Graph.get_operations()

Return the list of operations in the graph.

You can modify the operations in place, but modifications to the list such as inserts/delete have no effect on the list of operations known to the graph.

This method may be called concurrently from multiple threads.

Returns:

A list of Operations.

tf.Graph.seed

The graph-level random seed of this graph.

tf.Graph.unique_name(name, mark_as_used=True)

Return a unique operation name for name.

Note: You rarely need to call unique_name() directly. Most of the time you just need to create with g.name_scope() blocks to generate structured names.

unique_name is used to generate structured names, separated by “/”, to help identify operations when debugging a graph. Operation names are displayed in error messages reported by the TensorFlow runtime, and in various visualization tools such as TensorBoard.

If mark_as_used is set to True, which is the default, a new unique name is created and marked as in use. If it’s set to False, the unique name is returned without actually being marked as used. This is useful when the caller simply wants to know what the name to be created will be.

Args:

name: The name for an operation.
mark_as_used: Whether to mark this name as being used.
Returns:

A string to be passed to create_op() that will be used to name the operation being created.

tf.Graph.version

Returns a version number that increases as ops are added to the graph.

Note that this is unrelated to the GraphDef version.

tf.Graph.graph_def_versions

The GraphDef version information of this graph.

For details on the meaning of each version, see GraphDef.

Returns:

A VersionDef.

tf.Graph.create_op(op_type, inputs, dtypes, input_types=None, name=None, attrs=None, op_def=None, compute_shapes=True, compute_device=True)

Creates an Operation in this graph.

This is a low-level interface for creating an Operation. Most programs will not call this method directly, and instead use the Python op constructors, such as tf.constant(), which add ops to the default graph.

Args:

op_type: The Operation type to create. This corresponds to the OpDef.name field for the proto that defines the operation.
inputs: A list of Tensor objects that will be inputs to the Operation.
dtypes: A list of DType objects that will be the types of the tensors that the operation produces.
input_types: (Optional.) A list of DTypes that will be the types of the tensors that the operation consumes. By default, uses the base DType of each input in inputs. Operations that expect reference-typed inputs must specify input_types explicitly.
name: (Optional.) A string name for the operation. If not specified, a name is generated based on op_type.
attrs: (Optional.) A dictionary where the key is the attribute name (a string) and the value is the respective attr attribute of the NodeDef proto that will represent the operation (an AttrValue proto).
op_def: (Optional.) The OpDef proto that describes the op_type that the operation will have.
compute_shapes: (Optional.) If True, shape inference will be performed to compute the shapes of the outputs.
compute_device: (Optional.) If True, device functions will be executed to compute the device property of the Operation.
Raises:

TypeError: if any of the inputs is not a Tensor.
ValueError: if colocation conflicts with existing device assignment.
Returns:

An Operation object.

tf.Graph.gradient_override_map(op_type_map)

EXPERIMENTAL: A context manager for overriding gradient functions.

This context manager can be used to override the gradient function that will be used for ops within the scope of the context.

For example:

@tf.RegisterGradient(“CustomSquare”)
def _custom_square_grad(op, grad):
# …

with tf.Graph().as_default() as g:
c = tf.constant(5.0)
s_1 = tf.square(c) # Uses the default gradient for tf.square.
with g.gradient_override_map({“Square”: “CustomSquare”}):
s_2 = tf.square(s_2) # Uses _custom_square_grad to compute the
# gradient of s_2.
Args:

op_type_map: A dictionary mapping op type strings to alternative op type strings.
Returns:

A context manager that sets the alternative op type to be used for one or more ops created in that context.

Raises:

TypeError: If op_type_map is not a dictionary mapping strings to strings.
Other Methods

tf.Graph.colocate_with(op, ignore_existing=False)

Returns a context manager that specifies an op to colocate with.

Note: this function is not for public use, only for internal libraries.

For example:

a = tf.Variable([1.0])
with g.colocate_with(a):
b = tf.constant(1.0)
c = tf.add(a, b)
b and c will always be colocated with a, no matter where a is eventually placed.

Args:

op: The op to colocate all created ops with.
ignore_existing: If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack.
Raises:

ValueError: if op is None.
Yields:

A context manager that specifies the op with which to colocate newly created ops.

tf.Graph.Container(container_name)

Returns a context manager that specifies the resource container to use.

Stateful operations, such as variables and queues, can maintain their states on devices so that they can be shared by multiple processes. A resource container is a string name under which these stateful operations are tracked. These resources can be released or cleared with tf.Session.reset().

For example:

with g.container(‘experiment0’):
# All stateful Operations constructed in this context will be placed
# in resource container “experiment0”.
v1 = tf.Variable([1.0])
v2 = tf.Variable([2.0])
with g.container(“experiment1”):
# All stateful Operations constructed in this context will be
# placed in resource container “experiment1”.
v3 = tf.Variable([3.0])
q1 = tf.FIFOQueue(10, tf.float32)
# All stateful Operations constructed in this context will be
# be created in the “experiment0”.
v4 = tf.Variable([4.0])
q1 = tf.FIFOQueue(20, tf.float32)
with g.container(“”):
# All stateful Operations constructed in this context will be
# be placed in the default resource container.
v5 = tf.Variable([5.0])
q3 = tf.FIFOQueue(30, tf.float32)

Resets container “experiment0”, after which the state of v1, v2, v4, q1

will become undefined (such as uninitialized).

tf.Session.reset(target, [“experiment0”])
Args:

container_name: container name string.
Returns:

A context manager for defining resource containers for stateful ops, yields the container name.

tf.Graph.get_all_collection_keys()

Returns a list of collections used in this graph.

tf.Graph.is_feedable(tensor)

Returns True if and only if tensor is feedable.

tf.Graph.is_fetchable(tensor_or_op)

Returns True if and only if tensor_or_op is fetchable.

tf.Graph.prevent_feeding(tensor)

Marks the given tensor as unfeedable in this graph.

tf.Graph.prevent_fetching(op)

Marks the given op as unfetchable in this graph.

V.Session(tf.Session)

运行TensorFLow操作(operations)的类,一个Seesion包含了操作对象执行的环境.
一个例子:

# -*- coding: utf-8 -*- 

from __future__ import print_function,division
import tensorflow as tf

#build graph
a=tf.constant(2.)
b=tf.constant(5.)
c=a*b

#construct session
sess=tf.Session()

#Evaluate the tensor `c`
print(sess.run(c))

#close session
sess.close()
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

上面最后一句close()是因为session一般都拥有很多的资源,要是session不再可用的话,就要调用close()函数来释放这些资源。
简单一点的话,也可以用上下文管理器,比如参照下面的方式:

# Using the `close()` method.
sess = tf.Session()
sess.run(...)
sess.close()

# Using the context manager.
with tf.Session() as sess:
  sess.run(...)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

重要函数:
tf.Session.__init__(target=”, graph=None, config=None)

作用:
这是session的构造函数,创建一个新的tensorflow的session

参数:
target:(可选)连接的执行引擎,默认是使用in-process引擎,分布式TensorFLow有更多的例子。
graph: (可选)投放进的计算图(graph),要是没有指定的话,那么默认的图就会被投放到这个session。要是你在同一个进程里面用了很多的图,你将为各个图使用不用的session,但是每一个graph都能够在多个session中使用。在这种情况下,经常显式的传递graph参数到session的构造里面。
config: (可选) A ConfigProto protocol buffer with configuration options for the session.

tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=None)

作用:
运行操作估算(计算)tensor。
参数:
fetches: 一个单独的图的元素,或者一个图的元素的列表。或者一个字典,这个字典的值是刚刚所说的一个图的元素(元素列表)(见下面第四部分的例子)。图的元素可以是一个操作(那么fetch回来的值将是None);一个tensor(反回的值将是将是表示这个tensor值的numpy ndarray对象);一个sparsetensor(稀疏tensor);一个get_tensor_handle的操作;一个表示tensor或者操作名称的string
feed_dict: 一个字典,为之前“占位”的元素“喂”给值。(具体见第四部分的例子。)
The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:
If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.
If the key is a SparseTensor, the value should be a SparseTensorValue.
If the key is a nested tuple of Tensors or SparseTensors, the value should be a nested tuple with the same structure that maps to their corresponding values as above.
Each value in feed_dict must be convertible to a numpy array of the dtype of the corresponding key.
options: A [RunOptions] protocol buffer
run_metadata: A [RunMetadata] protocol buffer
返回值:
如果你的fetchs参数传入的图的一个元素,那么返回一个单独的值,要是是图的一个元素列表,那么返回就是一个列表,要是你传入的是一个字典,那么返回的也是一个字典,这个字典的键和你传入的字典的键是一样的。函数返回的值和你传进去的fetch参数的形状是一样的,只是里面的元素是相应的值而已了。

The optional options argument expects a [RunOptions] proto. The options allow controlling the behavior of this particular step (e.g. turning tracing on).

The optional run_metadata argument expects a [RunMetadata] proto. When appropriate, the non-Tensor output of this step will be collected there. For example, when users turn on tracing in options, the profiled info will be collected into this argument and passed back.

Raises:

RuntimeError: If this Session is in an invalid state (e.g. has been closed).
TypeError: If fetches or feed_dict keys are of an inappropriate type.
ValueError: If fetches or feed_dict keys are invalid or refer to a Tensor that doesn’t exist.

Example:

   a = tf.constant([10, 20])
   b = tf.constant([1.0, 2.0])
   # 'fetches' can be a singleton
   v = session.run(a)
   # v is the numpy array [10, 20]
   # 'fetches' can be a list.
   v = session.run([a, b])
   # v a Python list with 2 numpy arrays: the numpy array [10, 20] and the
   # 1-D array [1.0, 2.0]
   # 'fetches' can be arbitrary lists, tuples, namedtuple, dicts:
   MyData = collections.namedtuple('MyData', ['a', 'b'])
   v = session.run({'k1': MyData(a, b), 'k2': [b, a]})
   # v is a dict with
   # v['k1'] is a MyData namedtuple with 'a' the numpy array [10, 20] and
   # 'b' the numpy array [1.0, 2.0]
   # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array
   # [10, 20].
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

tf.Session.close()

作用:关闭这个session,调用这个函数会使得所有与这个session有关的资源被释放

tf.Session.graph

投放(launch)到这个session里面的图

tf.Session.as_default()
作用:返回一个上下文管理器
Returns a context manager that makes this object the default session.

Use with the with keyword to specify that calls to Operation.run() or Tensor.eval() should be executed in this session.

c = tf.constant(..)
sess = tf.Session()

with sess.as_default():
  assert tf.get_default_session() is sess
  print(c.eval())
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

To get the current default session, use tf.get_default_session().

N.B. The as_default context manager does not close the session when you exit the context, and you must close the session explicitly.

c = tf.constant(…)
sess = tf.Session()
with sess.as_default():
print(c.eval())

with sess.as_default():
print(c.eval())

sess.close()
Alternatively, you can use with tf.Session(): to create a session that is automatically closed on exiting the context, including when an uncaught exception is raised.

N.B. The default graph is a property of the current thread. If you create a new thread, and wish to use the default session in that thread, you must explicitly add a with sess.as_default(): in that thread’s function.

Returns:

A context manager using this session as the default session.

tf.Session.reset(target, containers=None, config=None)

Resets resource containers on target, and close all connected sessions.

A resource container is distributed across all workers in the same cluster as target. When a resource container on target is reset, resources associated with that container will be cleared. In particular, all Variables in the container will become undefined: they lose their values and shapes.

NOTE: (i) reset() is currently only implemented for distributed sessions. (ii) Any sessions on the master named by target will be closed.

If no resource containers are provided, all containers are reset.

Args:

target: The execution engine to connect to.
containers: A list of resource container name strings, or None if all of all the containers are to be reset.
config: (Optional.) Protocol buffer with configuration options.
Raises:

tf.errors.OpError: Or one of its subclasses if an error occurs while resetting containers.

Other Methods

tf.Session.enter()

tf.Session.exit(exec_type, exec_value, exec_tb)

class tf.InteractiveSession

A TensorFlow Session for use in interactive contexts, such as a shell.

The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods Tensor.eval() and Operation.run() will use that session to run ops.

This is convenient in interactive shells and IPython notebooks, as it avoids having to pass an explicit Session object to run ops.

For example:

sess = tf.InteractiveSession()
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'
print(c.eval())
sess.close()
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Note that a regular session installs itself as the default session when it is created in a with statement. The common usage in non-interactive programs is to follow that pattern:

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.Session():
# We can also use ‘c.eval()’ here.
print(c.eval())
tf.InteractiveSession.init(target=”, graph=None, config=None)

Creates a new interactive TensorFlow session.

If no graph argument is specified when constructing the session, the default graph will be launched in the session. If you are using more than one graph (created with tf.Graph() in the same process, you will have to use different sessions for each graph, but each graph can be used in multiple sessions. In this case, it is often clearer to pass the graph to be launched explicitly to the session constructor.

Args:

target: (Optional.) The execution engine to connect to. Defaults to using an in-process engine.
graph: (Optional.) The Graph to be launched (described above).
config: (Optional) ConfigProto proto used to configure the session.
tf.InteractiveSession.close()

Closes an InteractiveSession.

tf.get_default_session()

Returns the default session for the current thread.

The returned Session will be the innermost session on which a Session or Session.as_default() context has been entered.

NOTE: The default session is a property of the current thread. If you create a new thread, and wish to use the default session in that thread, you must explicitly add a with sess.as_default(): in that thread’s function.

Returns:

The default Session being used in the current thread.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值