首先导入库:
#coding=utf-8
#如果出现编码上的问题,可能需要先写上面这行,保存一下文件,再写下面的部分
import tensorflow as tf
创建新变量的一个实例:
#对应的参数(name_or_scope, default_name, values=None, ...,reuse=None,...,auxiliary_name_scope=True)
#其中,default_name: name_or_scope若为None,则用它,否则它将无用
with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v = tf.get_variable("v", [1]) #获取具有此名称"v"的现有变量(如果已有)或创建一个新变量(还没有该名称的变量); 参数(name, shape, dtype, initializer, ...)
print(v.name) #output: foo/bar/v:0
s = tf.get_variable_scope() #返回当前变量scopereuse=None
print(s.name) #output: foo/bar
assert v.name == "foo/bar/v:0"
若name_or_scope=None, 且default_name='fcy':(default_name: name_or_scope若为None,则用它,否则它将无用)
#若name_or_scope=None, 且default_name='fcy':
with tf.variable_scope(name_or_scope=None, default_name='fcy'):
with tf.variable_scope("bar"):
v = tf.get_variable("v", [1])
print(v.name) #output: fcy/bar/v:0
s = tf.get_variable_scope() #返回当前变量scopereuse=None
print(s.name) #output: fcy/bar
assert v.name == "fcy/bar/v:0"

本文介绍了TensorFlow中变量管理的关键概念,包括`tf.variable_scope`, `tf.get_variable`, `tf.get_variable_scope`以及`arg_scope`的使用。通过示例展示了如何创建、重用和安全地管理变量,以及`arg_scope`在控制变量共享方面的作用。"
8806931,1403176,Apache Tez:优化Hadoop处理的DAG框架,"['Hadoop生态', '大数据处理', 'MapReduce优化', 'DAG框架', 'Apache组件']
最低0.47元/天 解锁文章
476

被折叠的 条评论
为什么被折叠?



