2016-05-07 19 views
11

テンソルフローで特定のモデルの変数スコープの名前を変更できますか?TensorFlowで保存されたモデルの変数スコープの名前を変更します。

は例えば、私はチュートリアルに基づいて、MNIST桁のためのロジスティック回帰モデルを作成しました:

with tf.variable_scope('my-first-scope'): 
    NUM_IMAGE_PIXELS = 784 
    NUM_CLASS_BINS = 10 
    x = tf.placeholder(tf.float32, shape=[None, NUM_IMAGE_PIXELS]) 
    y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASS_BINS]) 

    W = tf.Variable(tf.zeros([NUM_IMAGE_PIXELS,NUM_CLASS_BINS])) 
    b = tf.Variable(tf.zeros([NUM_CLASS_BINS])) 

    y = tf.nn.softmax(tf.matmul(x,W) + b) 
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 
    saver = tf.train.Saver([W, b]) 

... # some training happens 

saver.save(sess, 'my-model') 

は今、私は'my-first-scope'変数のスコープで保存されたモデルを再ロードし、新しい再びすべてを保存したいです新しい変数スコープ'my-second-scope'の下にあります。あなたの目標を達成するために、次のよう

答えて

7

あなたはtf.contrib.framework.load_variabletf.contrib.framework.list_variables使用してすることができます:

:kevemanの答えに基づいて、私はあなたがどんなTensorFlowチェックポイントの変数の名前を変更するために実行することができますPythonスクリプトを作成し
with tf.Graph().as_default(), tf.Session().as_default() as sess: 
    with tf.variable_scope('my-first-scope'): 
    NUM_IMAGE_PIXELS = 784 
    NUM_CLASS_BINS = 10 
    x = tf.placeholder(tf.float32, shape=[None, NUM_IMAGE_PIXELS]) 
    y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASS_BINS]) 

    W = tf.Variable(tf.zeros([NUM_IMAGE_PIXELS,NUM_CLASS_BINS])) 
    b = tf.Variable(tf.zeros([NUM_CLASS_BINS])) 

    y = tf.nn.softmax(tf.matmul(x,W) + b) 
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 
    saver = tf.train.Saver([W, b]) 
    sess.run(tf.global_variables_initializer()) 
    saver.save(sess, 'my-model') 

vars = tf.contrib.framework.list_variables('.') 
with tf.Graph().as_default(), tf.Session().as_default() as sess: 

    new_vars = [] 
    for name, shape in vars: 
    v = tf.contrib.framework.load_variable('.', name) 
    new_vars.append(tf.Variable(v, name=name.replace('my-first-scope', 'my-second-scope'))) 

    saver = tf.train.Saver(new_vars) 
    sess.run(tf.global_variables_initializer()) 
    saver.save(sess, 'my-new-model') 
+0

これは、グラフを定義しておく必要があるチェックポイントを復元するために、以前のスコープ名を使用してグラフとすべてを構築したことが必要です。 チェックポイントファイルがある場合は、その中のスコープ名を置き換えることはできますか? – npit

18

https://gist.github.com/batzner/7c24802dd9c5e15870b4b56e22135c96

変数名の部分文字列を置き換えて、すべての名前に接頭辞を付けることができます。ここではオプションの引数

--replace_from=substr --replace_to=substr --add_prefix=abc --dry_run 

python tensorflow_rename_variables.py --checkpoint_dir=path/to/dir 

でスクリプトを呼び出すことは、スクリプトの中核機能である:

def rename(checkpoint_dir, replace_from, replace_to, add_prefix, dry_run=False): 
    checkpoint = tf.train.get_checkpoint_state(checkpoint_dir) 
    with tf.Session() as sess: 
     for var_name, _ in tf.contrib.framework.list_variables(checkpoint_dir): 
      # Load the variable 
      var = tf.contrib.framework.load_variable(checkpoint_dir, var_name) 

      # Set the new name 
      new_name = var_name 
      if None not in [replace_from, replace_to]: 
       new_name = new_name.replace(replace_from, replace_to) 
      if add_prefix: 
       new_name = add_prefix + new_name 

      if dry_run: 
       print('%s would be renamed to %s.' % (var_name, new_name)) 
      else: 
       print('Renaming %s to %s.' % (var_name, new_name)) 
       # Rename the variable 
       var = tf.Variable(var, name=new_name) 

     if not dry_run: 
      # Save the variables 
      saver = tf.train.Saver() 
      sess.run(tf.global_variables_initializer()) 
      saver.save(sess, checkpoint.model_checkpoint_path) 

例:

python tensorflow_rename_variables.py --checkpoint_dir=path/to/dir --replace_from=scope1 --replace_to=scope1/model --add_prefix=abc/ 

が変数scope1/Variable1の名前を変更しますabc/scope1/model/Variable1へ。

+0

スクリプトでこのエラーが発生しました:ValueError:指定されたディレクトリの 'checkpoint'ファイルまたはチェックポイントを見つけることができませんでした。./fi – ryuzakinho

+1

@ryuzakinho、 'checkpoint'ファイルを含むディレクトリを指定する必要があります。詳細については、https://www.tensorflow.org/programmers_guide/variables#checkpoint_filesを参照してください。 –

+1

実際には、私は何らかの理由でwrite_state = Falseを持っていました。したがって、チェックポイントファイルを作成していませんでした。 – ryuzakinho

関連する問題