TensorFlow,如何重用变量作用域名称

TensorFlow,如何重用变量作用域名称,tensorflow,Tensorflow,我在这里定义了一个类 class基本网络(对象): 定义初始化(self,scope,task,name,is_train=False,img_shape=(80,80)): self.scope=scope self.is\u train=is\u train self.task\u name=任务名称 自我创建网络(范围,img\u形状=img\u形状) 定义创建网络(self,scope,img_shape=(80,80)): 使用tf.variable_范围(范围): 使用tf.vari

我在这里定义了一个类

class基本网络(对象):
定义初始化(self,scope,task,name,is_train=False,img_shape=(80,80)):
self.scope=scope
self.is\u train=is\u train
self.task\u name=任务名称
自我创建网络(范围,img\u形状=img\u形状)
定义创建网络(self,scope,img_shape=(80,80)):
使用tf.variable_范围(范围):
使用tf.variable\u范围(self.task\u名称):
使用tf.variable_scope('input_data'):
self.inputs=tf.placeholder(shape=[None,*img\u shape,cfg.HIST\u LEN],dtype=tf.float32)
使用tf.variable_作用域(“网络”):
使用tf.variable_scope('conv_1'):
self.conv_1=slim.conv2d(激活=tf.nn.relu,输入=self.inputs,num_输出=32,
内核大小=[8,8],步幅=4,填充='SAME',可训练=self.is\u train)
使用tf.variable_scope('conv_2'):
self.conv_2=slim.conv2d(激活=tf.nn.relu,输入=self.conv_1,输出=64,
内核大小=[4,4],步幅=2,填充='SAME',可训练=self.is\u train)
使用tf.variable_scope('conv_3'):
self.conv_3=slim.conv2d(激活=tf.nn.relu,输入=self.conv_2,输出=64,
内核大小=[3,3],步幅=1,填充='SAME',可训练=self.is\u train)
使用tf.variable_scope('f_c'):
self.fc=slim.完全连接(slim.flant(self.conv_3)),512,
激活(fn=tf.nn.elu,可训练=自我训练)
我想用不同的任务名称定义两个BasicNetwork实例。范围为“全局”。但是当我检查输出时,有

ipdb> for i in net_1.layres: print(i)
Tensor("global/simple/networks/conv_1/Conv/Relu:0", shape=(?, 20, 20, 32), dtype=float32, device=/device:GPU:2)
Tensor("global/simple/networks/conv_2/Conv/Relu:0", shape=(?, 10, 10, 64), dtype=float32, device=/device:GPU:2)
Tensor("global/simple/networks/conv_3/Conv/Relu:0", shape=(?, 10, 10, 64), dtype=float32, device=/device:GPU:2)
Tensor("global/simple/networks/f_c/fully_connected/Elu:0", shape=(?, 512), dtype=float32, device=/device:GPU:2)

ipdb> for i in net_2.layres: print(i)
Tensor("global_1/supreme/networks/conv_1/Conv/Relu:0", shape=(?, 20, 20, 32), dtype=float32, device=/device:GPU:2)
Tensor("global_1/supreme/networks/conv_2/Conv/Relu:0", shape=(?, 10, 10, 64), dtype=float32, device=/device:GPU:2)
Tensor("global_1/supreme/networks/conv_3/Conv/Relu:0", shape=(?, 10, 10, 64), dtype=float32, device=/device:GPU:2)
Tensor("global_1/supreme/networks/f_c/fully_connected/Elu:0", shape=(?, 512), dtype=float32, device=/device:GPU:2)

正如您在输出中看到的,已经创建了一个新的作用域
global\u 1
,但我想将其设置为
global
。我设置了
reuse=True
,但后来我发现当没有名为
global
的作用域时,
reuse=True
不能使用。我该怎么办

使用
reuse
True可以获得现有变量。现在要重用变量,图形中应该存在软管。如果存在同名的th变量,则可以将这些变量重新用于其他操作

class BasicNetwork(object):
def __init__(self, scope, task_name, reuse, is_train=False, img_shape=(80, 80)):
    self.scope = scope
    self.is_train = is_train
    self.reuse = reuse
    self.task_name = task_name
    self.__create_network(scope, reuse=self.reuse, img_shape=img_shape)

def __create_network(self, scope, reuse=None, img_shape=(80, 80)):
    with tf.variable_scope(scope, reuse=reuse):
    ...
        # delete this line with tf.variable_scope(self.task_name): 
        # or replace with; with tf.name_scope(self.task_name):               

trainnet = BasicNetwork('global', taskname, None)
# resue the created variables
valnet = BasicNetwork('global', taskname, True)

使用
reuse
True可以获得现有变量。现在要重用变量,图形中应该存在软管。如果存在同名的th变量,则可以将这些变量重新用于其他操作

class BasicNetwork(object):
def __init__(self, scope, task_name, reuse, is_train=False, img_shape=(80, 80)):
    self.scope = scope
    self.is_train = is_train
    self.reuse = reuse
    self.task_name = task_name
    self.__create_network(scope, reuse=self.reuse, img_shape=img_shape)

def __create_network(self, scope, reuse=None, img_shape=(80, 80)):
    with tf.variable_scope(scope, reuse=reuse):
    ...
        # delete this line with tf.variable_scope(self.task_name): 
        # or replace with; with tf.name_scope(self.task_name):               

trainnet = BasicNetwork('global', taskname, None)
# resue the created variables
valnet = BasicNetwork('global', taskname, True)

嗨,任务名是不同的,一个是“简单”,另一个是“至高无上”。这不会影响主要逻辑。很抱歉,发生了错误,
ValueError:Variable global/supreme/networks/conv_1/conv/weights不存在,或者不是用tf.get_Variable()创建的。您的意思是在VarScope中设置重用=无吗。代码为net\u 1=BasicACNetwork('global','simple',reuse=None);net_2=BasicACNetwork('global','supreme',reuse=True)
reuse
变量,它应该存在于图中;从erros MSG来看,变量似乎没有在图中创建。是的,这是因为没有创建
global/supreme…
。图中只有
global/simple…
。嗨,任务名称不同,一个是“simple”,另一个是“supreme”。这不会影响主逻辑。很抱歉,发生了错误,
ValueError:Variable global/supreme/networks/conv_1/conv/weights不存在,或者不是用tf.get_Variable()创建的。您的意思是在VarScope中设置重用=无吗。代码为net\u 1=BasicACNetwork('global','simple',reuse=None);net_2=BasicACNetwork('global','supreme',reuse=True)
reuse
变量,它应该存在于图中;从erros MSG来看,变量似乎没有在图中创建。是的,这是因为没有创建
global/supreme…
。图中只有
global/simple…
。我认为这个问题是在事实发生后在这里得到回答的:我认为这个问题是在事实发生后在这里得到回答的: