2017-03-07 4 views
1

にGPUおよびテストフェーズにトレーニングフェーズを実行します。Tensorflow:私は終了し、私が作成したモデルをロードし、CPUにそのテストフェーズを実行するために、結果を保存した後にしながら、私のGPU上で私のtensorflowコードのトレーニングフェーズを実行したいCPU

私は(それはそう巨大だから、私はちょうど参考のために、その一部を入れている、私はルールが完全に機能するコードが含まれるようにしていることを知っていると私はそのことについて謝罪)このコードを作成しました。その後

import pandas as pd 
import matplotlib.pyplot as plt 
import numpy as np 
import tensorflow as tf 
from tensorflow.contrib.rnn.python.ops import rnn_cell, rnn 

# Import MNIST data http://yann.lecun.com/exdb/mnist/ 
from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 
x_train = mnist.train.images 
# Check that the dataset contains 55,000 rows and 784 columns 
N,D = x_train.shape 

tf.reset_default_graph() 
sess = tf.InteractiveSession() 

x = tf.placeholder("float", [None, n_steps,n_input]) 
y_true = tf.placeholder("float", [None, n_classes]) 
keep_prob = tf.placeholder(tf.float32,shape=[]) 
learning_rate = tf.placeholder(tf.float32,shape=[]) 

#[............Build the RNN graph model.............] 

sess.run(tf.global_variables_initializer()) 
# Because I am using my GPU for the training, I avoid allocating the whole 
# mnist.validation set because of memory error, so I gragment it to 
# small batches (100) 
x_validation_bin, y_validation_bin = mnist.validation.next_batch(batch_size) 
x_validation_bin = binarize(x_validation_bin, threshold=0.1) 
x_validation_bin = x_validation_bin.reshape((-1,n_steps,n_input)) 

for k in range(epochs): 

    steps = 0 

    for i in range(training_iters): 
     #Stochastic descent 
     batch_x, batch_y = mnist.train.next_batch(batch_size) 
     batch_x = binarize(batch_x, threshold=0.1) 
     batch_x = batch_x.reshape((-1,n_steps,n_input)) 
     sess.run(train_step, feed_dict={x: batch_x, y_true: batch_y,keep_prob: keep_prob,eta:learning_rate}) 

     if do_report_err == 1: 
      if steps % display_step == 0: 
       # Calculate batch accuracy 
       acc = sess.run(accuracy, feed_dict={x: batch_x, y_true: batch_y,keep_prob: 1.0}) 
       # Calculate batch loss 
       loss = sess.run(total_loss, feed_dict={x: batch_x, y_true: batch_y,keep_prob: 1.0}) 
       print("Iter " + str(i) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy = " + "{:.5f}".format(acc)) 
     steps += 1 




    # Validation Accuracy and Cost 
    validation_accuracy = sess.run(accuracy,feed_dict={x:x_validation_bin, y_true:y_validation_bin, keep_prob:1.0}) 
    validation_cost = sess.run(total_loss,feed_dict={x:x_validation_bin, y_true:y_validation_bin, keep_prob:1.0}) 

    validation_loss_array.append(final_validation_cost) 
    validation_accuracy_array.append(final_validation_accuracy) 
    saver.save(sess, savefilename) 
    total_epochs = total_epochs + 1 

    np.savez(datasavefilename,epochs_saved = total_epochs,learning_rate_saved = learning_rate,keep_prob_saved = best_keep_prob, validation_loss_array_saved = validation_loss_array,validation_accuracy_array_saved = validation_accuracy_array,modelsavefilename = savefilename) 

、私のモデルが正常に訓練され、関連するデータを保存したので、私は、この時間は、ファイルをロードし、モデルに最終電車やテストの一部を行うことを望むが、私のCPUを使用しています。その理由は、GPUがmnist.train.imagesとmnist.train.labelsのデータセット全体を処理できないためです。だから、

、手動で私はこの部分を選択して、私はそれを実行します。

with tf.device('/cpu:0'): 
# Initialise variables 
    sess.run(tf.global_variables_initializer()) 

    # Accuracy and Cost 
    saver.restore(sess, savefilename) 
    x_train_bin = binarize(mnist.train.images, threshold=0.1) 
    x_train_bin = x_train_bin.reshape((-1,n_steps,n_input)) 
    final_train_accuracy = sess.run(accuracy,feed_dict={x:x_train_bin, y_true:mnist.train.labels, keep_prob:1.0}) 
    final_train_cost = sess.run(total_loss,feed_dict={x:x_train_bin, y_true:mnist.train.labels, keep_prob:1.0}) 

    x_test_bin = binarize(mnist.test.images, threshold=0.1) 
    x_test_bin = x_test_bin.reshape((-1,n_steps,n_input)) 
    final_test_accuracy = sess.run(accuracy,feed_dict={x:x_test_bin, y_true:mnist.test.labels, keep_prob:1.0}) 
    final_test_cost = sess.run(total_loss,feed_dict={x:x_test_bin, y_true:mnist.test.labels, keep_prob:1.0}) 

しかし、私は私がプログラムを強制していると思うので、私はそれが私には意味がありませんOMMのGPUのメモリエラーを取得CPUに依存します。私は最初の(バッチを使ってトレーニングする)コードにsess.close()コマンドを入れなかったが、これが本当に背後にあるのかどうかはわからない。私は唯一のCPU上で最後の部分を実行する方法この記事に実際にfor the CPU 任意の提案に従っ?

答えて

2

with tf.device()文はグラフ作成にのみ適用され、実行されないため、デバイスブロック内にあるsess.runはデバイスをまったく持たないことと同等です。あなたがあなたのやりたいために

は、変数を共有個別のトレーニングとテストのグラフを構築する必要があります。

関連する問題