1. 程式人生 > >######(較深的應用)TensorFlow學習(三):Graph和Session ######(較深的應用)

######(較深的應用)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

RIGHT

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.