2017-04-14 14 views
1

私はTensorflowモデルを復元しようとしています。これは線形回帰ネットワークです。私の予測が良くないので、私は間違ったことをしていると確信しています。私が練習するとき、私はテストセットを持っています。私のテストセットの予測は素晴らしいですが、同じモデルを復元しようとすると、予測が悪くなります。ここでTensorflow復元モデル

with tf.Session() as sess: 
    saver = tf.train.Saver() 
    init = tf.global_variables_initializer() 
    sess.run(init) 
    training_data, ground_truth = d.get_training_data() 
    testing_data, testing_ground_truth = d.get_testing_data() 

    for iteration in range(config["training_iterations"]): 
     start_pos = np.random.randint(len(training_data) - config["batch_size"]) 
     batch_x = training_data[start_pos:start_pos+config["batch_size"],:,:] 
     batch_y = ground_truth[start_pos:start_pos+config["batch_size"]] 
     sess.run(optimizer, feed_dict={x: batch_x, y: batch_y}) 
     train_acc, train_loss = sess.run([accuracy, cost], feed_dict={x: batch_x, y: batch_y}) 

     sess.run(optimizer, feed_dict={x: testing_data, y: testing_ground_truth}) 
     test_acc, test_loss = sess.run([accuracy, cost], feed_dict={x: testing_data, y: testing_ground_truth}) 
     samples = sess.run(pred, feed_dict={x: testing_data}) 
     # print samples 
     data.compute_acc(samples, testing_ground_truth) 

     print("Training\tAcc: {}\tLoss: {}".format(train_acc, train_loss)) 
     print("Testing\t\tAcc: {}\tLoss: {}".format(test_acc, test_loss)) 
     print("Iteration: {}".format(iteration)) 

     if iteration % config["save_step"] == 0: 
      saver.save(sess, config["save_model_path"]+str(iteration)+".ckpt") 

は私のテストセットからいくつかの例を示します。ここでは

は、私がモデルを保存する方法です。あなたはMy predictionActualに比較的近い気づくでしょう:

My prediction: -12.705 Actual : -10.0 
My prediction: 0.000 Actual : 8.0 
My prediction: -14.313 Actual : -23.0 
My prediction: 17.879 Actual : 13.0 
My prediction: 17.452 Actual : 24.0 
My prediction: 22.886 Actual : 29.0 
Custom accuracy: 5.0159861487 
Training Acc: 5.63836860657 Loss: 25.6545143127 
Testing  Acc: 4.238052845 Loss: 22.2736053467 
Iteration: 6297 

次に、ここで私はモデルを復元する方法は次のとおりです。

with tf.Session() as sess: 
    saver = tf.train.Saver() 
    saver.restore(sess, config["retore_model_path"]+"3000.ckpt") 

    init = tf.global_variables_initializer() 
    sess.run(init) 

    pred = sess.run(pred, feed_dict={x: predict_data})[0] 
    print("Prediction: {:.3f}\tGround truth: {:.3f}".format(pred, ground_truth)) 

しかし、ここでは、予測がどのように見えるかです。あなたはPredictionが右0の周りに常にあることがわかります。このことができますが、私は配置しようとした場合

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tensorflow as tf 
>>> print(tf.__version__) 
0.12.0-rc1 

わからない:

Prediction: 0.355  Ground truth: -22.000 
Prediction: -0.035  Ground truth: 3.000 
Prediction: -1.005  Ground truth: -3.000 
Prediction: -0.184  Ground truth: 1.000 
Prediction: 1.300  Ground truth: 5.000 
Prediction: 0.133  Ground truth: -5.000 

ここに私のtensorflowバージョン(はい、私は更新する必要がある)でありますsaver.restore()sess.run(init)の後に電話してみると、予測はすべて同じです。これは、sess.run(init)が変数を初期化するためです。

変更するには、このような順序:その後、

sess.run(init) 
saver.restore(sess, config["retore_model_path"]+"6000.ckpt") 

しかし、予測は次のようになります。

Prediction: -15.840  Ground truth: 2.000 
Prediction: -15.840  Ground truth: -7.000 
Prediction: -0.000  Ground truth: 12.000 
Prediction: -15.840  Ground truth: -9.000 
Prediction: -15.175  Ground truth: -27.000 

答えて

2

あなたがチェックポイントから復元すると、あなたは、変数を初期化しません。あなたの質問の終わりに気づいた通り。

init = tf.global_variables_initializer() 
sess.run(init) 

これは、復元したばかりの変数を上書きします。おっとっと! :)

コメントこれらの2行が出て、私はあなたが行くのが良いと思う。

関連する問題