私はテンソルフローチュートリアルに従って、私の回帰問題のためにテンソルフローのニューラルネットワークを設計しました。しかし、私の問題(〜300.000データポイントと高価なFTRLOptimizerの使用)の構造のために、私の問題は、私の32 CPUマシン(私はGPUを持っていない)でも実行するには時間がかかりすぎました。列を使用した列車モデルTensorflow
this commentと、htopでの確認では、シングルスレッドの操作があり、feed_dictになっているようです。
したがって、hereとアドバイスしたので、プログラムをマルチスレッドするためにキューを使用しようとしました。
私は次のようにモデルを訓練するためにキューを持つ単純なコードファイルを書いた:
import numpy as np
import tensorflow as tf
import threading
#Function for enqueueing in parallel my data
def enqueue_thread():
sess.run(enqueue_op, feed_dict={x_batch_enqueue: x, y_batch_enqueue: y})
#Set the number of couples (x, y) I use for "training" my model
BATCH_SIZE = 5
#Generate my data where y=x+1+little_noise
x = np.random.randn(10, 1).astype('float32')
y = x+1+np.random.randn(10, 1)/100
#Create the variables for my model y = x*W+b, then W and b should both converge to 1.
W = tf.get_variable('W', shape=[1, 1], dtype='float32')
b = tf.get_variable('b', shape=[1, 1], dtype='float32')
#Prepare the placeholdeers for enqueueing
x_batch_enqueue = tf.placeholder(tf.float32, shape=[None, 1])
y_batch_enqueue = tf.placeholder(tf.float32, shape=[None, 1])
#Create the queue
q = tf.RandomShuffleQueue(capacity=2**20, min_after_dequeue=BATCH_SIZE, dtypes=[tf.float32, tf.float32], seed=12, shapes=[[1], [1]])
#Enqueue operation
enqueue_op = q.enqueue_many([x_batch_enqueue, y_batch_enqueue])
#Dequeue operation
x_batch, y_batch = q.dequeue_many(BATCH_SIZE)
#Prediction with linear model + bias
y_pred=tf.add(tf.mul(x_batch, W), b)
#MAE cost function
cost = tf.reduce_mean(tf.abs(y_batch-y_pred))
learning_rate = 1e-3
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
available_threads = 1024
#Feed the queue
for i in range(available_threads):
threading.Thread(target=enqueue_thread).start()
#Train the model
for step in range(1000):
_, cost_step = sess.run([train_op, cost])
print(cost_step)
Wf=sess.run(W)
bf=sess.run(b)
私はx_batchを呼び出すたびに、1 y_batchもデキューされ、その逆されているため、このコードは動作しません。次に、フィーチャを対応する「結果」と比較しません。
この問題を回避する簡単な方法はありますか?