2017-05-01 4 views
0

私は多くの同様の質問を読んでおり、これを正しく動作させることはできません。テンソルフローの前のセッションからint変数をロードできません。

私のモデルは訓練されており、チェックポイントファイルはすべてのエポックで作成されています。私はそれを持っているので、プログラムは一度再ロードされたエポックから継続することができます。また、すべての反復でそのエポックに印刷することもできます。私は単純にチェックポイントファイルの外にデータを保存することができましたが、他のすべてが適切に格納されていることを確信させるためにこれを実行したいと思っていました。

私は再起動すると、残念ながらepoch/global_step変数の値は常に0です。

import tensorflow as tf 
import numpy as np 
import tensorflow as tf 
import numpy as np 
# more imports 


def extract_number(f): # used to get latest checkpint file 
    s = re.findall("epoch(\d+).ckpt",f) 
    return (int(s[0]) if s else -1,f) 

def restore(init_op, sess, saver): # called to restore or just initialise model 
    list = glob(os.path.join("./params/e*")) 

    if list: 

     file = max(list,key=extract_number) 

     saver.restore(sess, file[:-5]) 


    sess.run(init_op) 
    return 


with tf.Graph().as_default() as g: 

    # build models 


    total_batch = data.train.num_examples/batch_size 

    epochLimit = 51 

    saver = tf.train.Saver() 

    init_op = tf.global_variables_initializer() 


    with tf.Session() as sess: 


     saver = tf.train.Saver() 

     init_op = tf.global_variables_initializer() 

     restore(init_op, sess, saver) 


     epoch = global_step.eval() 


     while epoch < epochLimit: 

      total_batch = data.train.num_examples/batch_size 

      for i in range(int(total_batch)): 

       sys.stdout.flush() 

       voxels = newData.eval() 

       batch_z = np.random.uniform(-1, 1, [batch_size, z_size]).astype(np.float32) 

       sess.run(opt_G, feed_dict={z:batch_z, train:True}) 
       sess.run(opt_D, feed_dict={input:voxels, z:batch_z, train:True}) 


       with open("out/loss.csv", 'a') as f: 
        batch_loss_G = sess.run(loss_G, feed_dict={z:batch_z, train:False}) 
        batch_loss_D = sess.run(loss_D, feed_dict={input:voxels, z:batch_z, train:False}) 
        msgOut = "Epoch: [{0}], i: [{1}], G_Loss[{2:.8f}], D_Loss[{3:.8f}]".format(epoch, i, batch_loss_G, batch_loss_D) 

        print(msgOut) 

      epoch=epoch+1 
      sess.run(global_step.assign(epoch)) 
      saver.save(sess, "params/epoch{0}.ckpt".format(epoch)) 

      batch_z = np.random.uniform(-1, 1, [batch_size, z_size]).astype(np.float32) 
      voxels = sess.run(x_, feed_dict={z:batch_z}) 

      v = voxels[0].reshape([32, 32, 32]) > 0 
      util.save_binvox(v, "out/epoch{0}.vox".format(epoch), 32) 

また、グローバルなステップ変数を下のassignを使用して更新します。何か案は?どんな助けでも大歓迎です。

答えて

0

私が非常に多くのことをしようとしていたため、私の元のコードはいくつかの理由で間違っていました。最初のレスポンダAlexandre Passosが有効なポイントを与えるが、私はゲームがスコープの使用(多分?)であったと考えている。以下は

それが誰を助けている場合取り組ん更新されたコードです:

import tensorflow as tf 
import numpy as np 
# more imports 


def extract_number(f): # used to get latest checkpint file 
    s = re.findall("epoch(\d+).ckpt",f) 
    return (int(s[0]) if s else -1,f) 

def restore(sess, saver): # called to restore or just initialise model 


    list = glob(os.path.join("./params/e*")) 

    if list: 

     file = max(list,key=extract_number) 

     saver.restore(sess, file[:-5]) 
     return saver, True, sess 

    saver = tf.train.Saver() 
    init_op = tf.global_variables_initializer() 
    sess.run(init_op) 

    return saver, False , sess 


batch_size = 100 
learning_rate = 0.0001 
beta1 = 0.5 
z_size = 100 
save_interval = 1 

data = dataset.read() 

total_batch = data.train.num_examples/batch_size 

def fill_queue(): 
    for i in range(int(total_batch*epochLimit)): 
     sess.run(enqueue_op, feed_dict={batch: data.train.next_batch(batch_size)}) # runnig in seperate thread to feed a FIFOqueue 



with tf.variable_scope("glob"): 
    global_step = tf.get_variable(name='global_step', initializer=0,trainable=False) 

# build models 

epochLimit = 51 

saver = tf.train.Saver() 

with tf.Session() as sess: 

    saver,rstr,sess = restore(sess, saver) 



    with tf.variable_scope("glob", reuse=True): 
     epocht = tf.get_variable(name='global_step', trainable=False, dtype=tf.int32) 

    epoch = epocht.eval() 


    while epoch < epochLimit: 

     total_batch = data.train.num_examples/batch_size 

     for i in range(int(total_batch)): 

      sys.stdout.flush() 

      voxels = newData.eval() 

      batch_z = np.random.uniform(-1, 1, [batch_size, z_size]).astype(np.float32) 

      sess.run(opt_G, feed_dict={z:batch_z, train:True}) 
      sess.run(opt_D, feed_dict={input:voxels, z:batch_z, train:True}) 


      with open("out/loss.csv", 'a') as f: 
       batch_loss_G = sess.run(loss_G, feed_dict={z:batch_z, train:False}) 
       batch_loss_D = sess.run(loss_D, feed_dict={input:voxels, z:batch_z, train:False}) 
       msgOut = "Epoch: [{0}], i: [{1}], G_Loss[{2:.8f}], D_Loss[{3:.8f}]".format(epoch, i, batch_loss_G, batch_loss_D) 

       print(msgOut) 

     epoch=epoch+1 
     sess.run(global_step.assign(epoch)) 
     saver.save(sess, "params/epoch{0}.ckpt".format(epoch)) 

     batch_z = np.random.uniform(-1, 1, [batch_size, z_size]).astype(np.float32) 
     voxels = sess.run(x_, feed_dict={z:batch_z}) 

     v = voxels[0].reshape([32, 32, 32]) > 0 
     util.save_binvox(v, "out/epoch{0}.vox".format(epoch), 32) 
1

復元後にsess.run(init_op)を呼び出すと、すべての変数が初期値にリセットされます。ラインアウトと物事がうまくいくとコメントしてください。

関連する問題