有什么办法不继承父变量_cope?

时间:2018-02-27 17:46:47

标签: python tensorflow

假设我有以下代码:

import tensorflow as tf
with tf.variable_scope('Embedding'):
    embd = tf.get_variable('embedding_matrix', [100, 10], dtype = tf.float32)

我想在新范围内重用名称为embedding_matrix的张量:

with tf.variable_scope('not_related'):
    with tf.variable_scope('Embedding', reuse = True) as scope:
        # I want the name of 'call_embd' be 'Embedding/embedding_matrix
        # but not 'not_related/Embedding/embedding_matrix'
        call_embd = tf.get_variable('embedding_matrix')

任何方式让call_embd的名称为Embedding/embedding_matrix

1 个答案:

答案 0 :(得分:0)

根据您的澄清评论,您发现variable_scopename_scope混淆。

简而言之:

  • variable_scope用于避免代码的不同部分无意中重复使用相同的变量。
  • name_scope用于对操作进行逻辑分组。

所以在你的情况下,你会使用类似的东西:

with tf.name_scope('Encoding'):
  with tf.variable_scope('Embedding'):
    embd = tf.get_variable('embedding_matrix', [100, 10], dtype = tf.float32)
    # ... use this var
# ... later ...
with tf.name_scope('Decoding'):
  with tf.variable_scope('Embedding', reuse=True):
    embd = tf.get_variable('embedding_matrix')
    # ... reuse this var

name_scopevariable_scope之间的curl -v -O ftp://192.168.26.10/inbox/project/logs/video/super_user_2017-09-25_19-20-27.webm -Q 'DELE inbox/project/logs/video/super_user_2017-09-25_19-20-27.webm' < 220 NASFTPD Turbo station 1.3.5a Server (ProFTPD) > USER anonymous < 331 Anonymous login ok, send your complete email address as your password > PASS ftp@example.com < 230 Anonymous access granted, restrictions apply > PWD 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0< 257 "/" is the current directory * Entry path is '/' > DELE inbox/project/logs/video/super_user_2017-09-25_19-20-27.webm * ftp_perform ends with SECONDARY: 0 < 250 DELE command successful > CWD inbox < 250 CWD command successful > CWD project < 250 CWD command successful > CWD logs < 250 CWD command successful > CWD video < 250 CWD command successful > EPSV * Connect data stream passively < 229 Entering Extended Passive Mode (|||55808|) * Hostname was NOT found in DNS cache * Trying 192.168.26.10... * Connecting to 192.168.26.10 (192.168.26.10) port 55808 * Connected to 192.168.26.10 (192.168.26.10) port 21 (#0) > TYPE I < 200 Type set to I > SIZE super_user_2017-09-25_19-20-27.webm < 550 super_user_2017-09-25_19-20-27.webm: No such file or directory > RETR super_user_2017-09-25_19-20-27.webm < 550 super_user_2017-09-25_19-20-27.webm: No such file or directory * RETR response: 550 * Remembering we are in dir "inbox/project/logs/video/" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 * Connection #0 to host 192.168.26.10 left intact curl: (78) RETR response: 550 之间的区别更多({无意中令人困惑}。