Tensorflow重用时进行推断?

时间:2018-08-02 22:30:59

标签: python tensorflow deep-learning

在完成训练和推理时,tensorflow是否需要设置重用== True? 我有这样的网络:

def __build_net(self,placeholder,reuse=False):
         with tf.variable_scope('siamse',reuse=reuse):
             layer = tf.layers.dense(placeholder,3000,activation=tf.nn.leaky_relu)
             layer = tf.layers.batch_normalization(layer)

             embedding= tf.layers.dense(layer,300,activation = tf.nn.leaky_relu)
             print('Siamse Net has built',flush=True)
         return embedding

然后我创建两个共享相同参数的网络:

self.embedding1=self.__build_net(self.centers_placeholder)
self.embedding2=self.__build_net(self.neighbors_placeholder,reuse=True)

我使用此网络生成某种数据的嵌入。

我的问题是:进行这样的推理(生成嵌入)时,是否需要将重用设置为True:

       with tf.Session() as sess:
        self.saver.restore(sess,self.store_path+self.model_type+'_model_'+str(self.model_num)+'_'+str(self.center_size)+'_'+str(self.neighbor_size)+'.ckpt')
        embedding = self.__build_net(self.centers_placeholder,reuse=True)
        embeddings = sess.run(embedding,feed_dict = {self.centers_placeholder : data})

或者像这样:

    with tf.Session() as sess:
        self.saver.restore(sess,self.store_path+self.model_type+'_model_'+str(self.model_num)+'_'+str(self.center_size)+'_'+str(self.neighbor_size)+'.ckpt')
        embedding = self.__build_net(self.centers_placeholder,reuse=False)
        embeddings = sess.run(embedding,feed_dict = {self.centers_placeholder : data})

然后,在设置变量作用域时,是否需要为每个图层命名?

谢谢!

1 个答案:

答案 0 :(得分:1)

否。...reuse表示是否需要使用先前定义的变量。

说,您已经创建了一个名为“ foo / v”的变量:

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
    print(v.name)   ---> foo/v:0

运行以下命令将会给出:

with tf.variable_scope("foo"):
    v1 = tf.get_variable("v", [1])   ---> gives error as name 'foo/v' exists
    print(v1.name) 
with tf.variable_scope("foo", reuse=False):
    v1 = tf.get_variable("v", [1])   ---> gives error as name 'foo/v' exists
    print(v1.name) 
with tf.variable_scope("foo", reuse=True):
    v1 = tf.get_variable("v", [1])
    print(v1.name)   ---> foo/v:0
with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
    v1 = tf.get_variable("v", [1])
    print(v1.name)   ---> foo/v:0

但是,如果您从一开始就运行以下命令:

with tf.variable_scope("foo", reuse=True):
    v1 = tf.get_variable("v", [1])
    print(v1.name)   ---> gives error as 'foo/v' does not exist (thus cannot be reused).

因此,我更喜欢一直设置reuse=tf.AUTO_REUSE

有关详细说明,请阅读TensorFlow官方指南中的How Does Variable Scope Work?

顺便说一下,tf.layers.batch_normalization有一个training选项,需要在推断过程中设置False。请参阅说明here