您的位置:首页 > 其它

TensorFlow 官方API 中文版(二)

2016-07-19 17:59 281 查看

TensorFlow 官方API 中文版(二)

7/19/2016 6:55:43 AM

1.1.1 class tf.Graph(续)

tf.Graph.devide(device_name_or_function)

  返回一个明确默认设备的使用的上下文管理器

device_name_or_function 参数可以是一个设备名字符串,一个设备函数或者None:

  如果这个参数是一个设备名字符串,所有在此上下文构建的操作将被指派给是这个名称的设备,除非它被一个嵌套的 device() 上下文所覆盖

  如果这个参数是函数,它将被视为一个从 Operation 对象到设备名字符串的函数,并且每次一个新的Operation被创建时都会被调用。这个Operation将会被指派给这个带有返回名的设备

  如果是None,所有从封闭的上下文调用的device()将会被忽略

想了解关于设备名字符串的合法语法,请在DeviceNameUtils中查阅相关文档

示例

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.


注:设备范围可能会被op包装器或者其他库代码所覆盖。例如,一个变量指定操作 v.assign()必须被tf.Variable v所托管(colocated),并且不兼容的设备将被忽略。

参数

device_name_or_function:在上下文中使用的设备名或函数

返回值

一个为新创建的操作明确默认设备的上下文管理器

tf.Graph.name_scope(name)

返回为操作创建分层的上下文管理器

一张图维持一个命名空间栈,在当前上下文生命期,一个 with name_scope(…):声明将一个新的名称压栈。

name参数将解释如下

一个字符串(没有以’/’结尾)将创建一个新的命名空间,在此空间name会附加到所有在上下文里创建的操作的前缀。如果name之前被用过,可以通过调用self.unique_name(name)使它变为独特的name

一个从之前a with g.name_scope(…) as scope: 声明捕获的空间将被视为一个绝对命名空间,这使得它有可能代表现有的空间

一个None值或者空字符串将会重置当前命名空间到最高层(空的)命名空间

示例:

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"


空间本身的命名可以被with g.name_scope(…) as scope:所捕获,它在变量空间内存有空间的命名。这个结果可以在一个空间内命名代表执行操作的所有结果的操作(This value can be used to name an operation that represents the overall result of executing the ops in a scope)。例如:

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)


参数:

name: 一个空间的命名

返回:

一个设置(install)name为新的命名空间的上下文管理器

一张图实例支持任意多个被命名标志的“集合”,为了方便,当建立一个很大的图时。集合能存储一组相关的对象。例如,tf.Variable为所有在图建立期间创建的变量使用一个集合(名为tf.GraphKeys.VARIABLES)。调用者可以通过指定一个新的命名来定义额外的集合。

tf.Graph.add_to_collection(name,value)

将value值存入给定name的collection

注意collections不是sets,所以有可能将同一个值多次加入一个collection。

参数:

names: collection的关键字(key)。 GraphKeys类包含许多标准集合名称

value:加入collection的值

tf.Graph.add_to_collections(names,value)

将value存入给定的names的collections中

注意collections不是sets,所以有可能将同一个值多次加入一个collection。这个函数保证忽略names中的重复值,但不会检查names中任意一个collection的value里已存在的成员。(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可以是可迭代的,但是如果它是个字符串,它将被视为一个单集合名

参数:

names: 要加入的collections的关键字(keys)。GraphKeys类包含许多标准集合名称

value:加入collections的值

tf.Graph.get_collection(name,scope=None)

返回给定名称集合的值的列表

这个方法不同于get_collection_ref(),后者总是返回真正的集合(如果存在),因此每次被调用时,它总是返回一个新的列表。(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.)

参数

name:集合的关键字。例如 GraphKeys类含有许多类的标准命名

scope:(可选)如果提供了,结果过滤列表仅包含那些没有名称属性总不返回,名称(name)属性匹配使用re.match得到的条目。如果一个 空间(scope)被提供,那么choice或者re.match意味着一个没有特殊前缀标记过滤器的空间。

返回:

给定名称的集合中值的列表,或者空表(当没有值加入集合)。这个列表包含一些按顺序的值,在这个顺序下它们被收集。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息