2017-08-03 13 views
1

私はモデルを訓練し、チェックポイントを保存しました。私のモデルのコードは次のとおりです。Tensorflow、いくつかの新しいレイヤーが追加されたらモデルを復元するにはどうしたらいいですか?

with tf.variable_scope(scope): 
    self.inputs = tf.placeholder(shape=[None, 80, 80, 1], dtype=tf.float32) 
    self.conv_1 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.inputs, num_outputs=32, 
           kernel_size=[8, 8], stride=4, padding='SAME') 
    self.conv_2 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_1, num_outputs=64, 
           kernel_size=[4, 4], stride=2, padding='SAME') 
    self.conv_3 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_2, num_outputs=64, 
           kernel_size=[3, 3], stride=1, padding='SAME') 
    self.fc = slim.fully_connected(slim.flatten(self.conv_3), 512, activation_fn=tf.nn.elu) 

    # Output layers for policy and value estimations 
    self.policy = slim.fully_connected(self.fc, 
             cfg.ACTION_DIM, 
             activation_fn=tf.nn.softmax, 
             biases_initializer=None) 
    self.value = slim.fully_connected(self.fc, 
             1, 
             activation_fn=None, 
             biases_initializer=None) 

あり、同時に実行されている約32のプロセスがあり、それぞれが上記のコードで定義されたグローバルネットワークのコピーを持って、scopeは、各プロセスのIDです。グローバルネットワークのscopeglobalです。

その後、self.fcレイヤー以降にレイヤーを追加したいと思います。

with tf.variable_scope(scope): 
    self.inputs = tf.placeholder(shape=[None, 80, 80, 1], dtype=tf.float32) 
    self.conv_1 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.inputs, num_outputs=32, 
           kernel_size=[8, 8], stride=4, padding='SAME') 
    self.conv_2 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_1, num_outputs=64, 
           kernel_size=[4, 4], stride=2, padding='SAME') 
    self.conv_3 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_2, num_outputs=64, 
           kernel_size=[3, 3], stride=1, padding='SAME') 
    self.fc = slim.fully_connected(slim.flatten(self.conv_3), 512, activation_fn=tf.nn.elu) 

    # Output layers for policy and value estimations 
    self.policy = slim.fully_connected(self.fc, 
             cfg.ACTION_DIM, 
             activation_fn=tf.nn.softmax, 
             biases_initializer=None) 
    self.value = slim.fully_connected(self.fc, 
             1, 
             activation_fn=None, 
             biases_initializer=None) 

    self.new_fc_1 = slim.fully_connected(self.fc, 512, activation_fn=tf.nn.elu) 

はしかし、私はモデルを復元するとき、それは次のようなエラー報告:

2017-08-03 22:23:43.473157: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
2017-08-03 22:23:43.477197: W tensorflow/core/framework/op_kernel.cc:1158] Data loss: Checksum does not match: stored 379803423 vs. calculated on the restored bytes 2648422677 
2017-08-03 22:23:43.477210: W tensorflow/core/framework/op_kernel.cc:1158] Data loss: Checksum does not match: stored 3963326522 vs. calculated on the restored bytes 3154501583 
2017-08-03 22:23:43.477200: W tensorflow/core/framework/op_kernel.cc:1158] Data loss: Checksum does not match: stored 3893236466 vs. calculated on the restored bytes 1767411214 
2017-08-03 22:23:43.478276: W tensorflow/core/framework/op_kernel.cc:1158] Data loss: Checksum does not match: stored 4239176201 vs. calculated on the restored bytes 3213118706 
2017-08-03 22:23:43.480438: W tensorflow/core/framework/op_kernel.cc:1158] Data loss: Checksum does not match: stored 442335910 vs. calculated on the restored bytes 4248164641 
2017-08-03 22:23:43.483885: W tensorflow/core/framework/op_kernel.cc:1158] Data loss: Checksum does not match: stored 3105262865 vs. calculated on the restored bytes 2648422677 
2017-08-03 22:23:43.483953: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
    [[Node: save/RestoreV2_128 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_128/tensor_names, save/RestoreV2_128/shape_and_slices)]] 
2017-08-03 22:23:43.486987: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
    [[Node: save/RestoreV2_128 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_128/tensor_names, save/RestoreV2_128/shape_and_slices)]] 
2017-08-03 22:23:43.490616: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
    [[Node: save/RestoreV2_128 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_128/tensor_names, save/RestoreV2_128/shape_and_slices)]] 
2017-08-03 22:23:43.491951: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
    [[Node: save/RestoreV2_128 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_128/tensor_names, save/RestoreV2_128/shape_and_slices)]] 
2017-08-03 22:23:43.491957: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
    [[Node: save/RestoreV2_128 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_128/tensor_names, save/RestoreV2_128/shape_and_slices)]] 
2017-08-03 22:23:43.494310: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Key worker_15/fully_connected_3/weights not found in checkpoint 
    [[Node: save/RestoreV2_128 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_128/tensor_names, save/RestoreV2_128/shape_and_slices)]] 
.... .... 

を私はモデル

saver.save(sess, self.model_path+'/model-'+str(episode_count)+'.ckpt') 

を保存するには、次のコードを使用し、ここで定義するためのコードですセーバー

value_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/old_scope') 
value_list.extend(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/actor_critic')) 
saver = tf.train.Saver(value_list, max_to_keep=100) 

with tf.Session(config=tf_configs) as sess: 
    coord = tf.train.Coordinator() 
    if load_model: 
     print('Loading Model...') 
     ckpt = tf.train.get_checkpoint_state(model_path) 
     saver.restore(sess, ckpt.model_checkpoint_path) 
    else: 
     sess.run(tf.global_variables_initializer()) 

ランダムに初期化されたパラメータを持つ新しいレイヤーが現在のニューラルネットワークに追加されたときに、事前に訓練されたモデルを復元するにはどうすればよいですか?

+0

古いモデルを使用してチェックポイントを復元し、後で新しいテンソルを追加してください – aseipel

答えて

0

が長時間と@BlueSunの助けを借りてグーグルの後、私は次のような方法をするのを助けることができるが見つかりました:

は、その後あなただけの最初のスコープからの変数で動作するようにセーバーを指定することができますこの問題を解決します。

新しいスコープを追加する前に、現在のスコープの変数を最初に使用してモデルを保存します。

value_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/old_scope') 
value_list.extend(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/actor_critic')) 
saver = tf.train.Saver(value_list, max_to_keep=100) 

新車を訓練する。

その後、新しいスコープを追加し、この

value_list = [] 
value_list.extend(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/old_scope')) 
value_list.extend(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/actor_critic')) 
value_list.extend(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='global/added_layer')) 
saver = tf.train.Saver(value_list, max_to_keep=100) 

with tf.Session(config=tf_configs) as sess: 
    coord = tf.train.Coordinator() 
    if load_model: 
     print('Loading Model...') 
     ckpt = tf.train.get_checkpoint_state(model_path) 
     saver.restore(sess, ckpt.model_checkpoint_path) 
    else: 
     sess.run(tf.global_variables_initializer()) 

    saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="global"), max_to_keep=100) 

ようなモデル、コードを実行する前に、新しいsaverを定義し、ネットワークコードはあるが、それは今、正常に動作します。この

with tf.variable_scope(scope): 
    with tf.variable_scope('old_scope'): 
     self.inputs = tf.placeholder(shape=[None, 80, 80, 1], dtype=tf.float32) 
     self.conv_1 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.inputs, num_outputs=32, 
            kernel_size=[8, 8], stride=4, padding='SAME') 
     self.conv_2 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_1, num_outputs=64, 
            kernel_size=[4, 4], stride=2, padding='SAME') 
     self.conv_3 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_2, num_outputs=64, 
            kernel_size=[3, 3], stride=1, padding='SAME') 
     self.fc = slim.fully_connected(slim.flatten(self.conv_3), 512, activation_fn=tf.nn.elu) 

    with tf.variable_scope('added_layer'): 
     self.fc_1 = slim.fully_connected(self.fc, 512, activation_fn=tf.nn.elu) 

    with tf.variable_scope('actor_critic'): 
     # Output layers for policy and value estimations 
     self.policy = slim.fully_connected(self.fc_1, 
             cfg.ACTION_DIM, 
             activation_fn=tf.nn.softmax, 
             biases_initializer=None) 
     self.value = slim.fully_connected(self.fc_1, 
              1, 
              activation_fn=None, 
              biases_initializer=None) 

のように見えますコードはちょっと違って見えます。

1

2つの独立した変数スコープを使用できます。 1つは保存と読み込み、もう1つは新しいレイヤーです。

saver = tf.train.Saver(
    tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="save_scope") 
) 
+0

もっと多くの例を提供できますか? –

+0

'VALUE_LIST = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES、範囲= 'グローバル/ old_scope') value_list.extend(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES、範囲= 'グローバル/ actor_critic')) value_list.extendsaver = tf.train.Saver(value_list、max_to_keep = 100) '私は新しいレイヤーを追加し、後でモデルを復元すると、そのレイヤーを復元します。報告されました**鍵グローバル/追加レイヤー/完全接続/ウェイトがチェックポイントに見つかりません** –

+0

新しいレイヤーは 'global/added_layer'スコープの一部ですか?はいの場合、なぜあなたはvalue_listをadded_layerスコープで拡張しますか?あなたは 'セーバー= tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES、スコープ=「old_scope」)、max_to_keep = 100)のように、あなたがロードしようと古い層を有する唯一のスコープを使用しセーバーを初期化する必要があります'。そうでなければ、Saverは前に存在しなかったレイヤーをロードしようとします。 – BlueSun

関連する問題