バッチ勾配降下を使用してニューラルネットワークを訓練したいが、私はそのプロセスを並列化したい。バッチをミニバッチに分割し、プロセス間でグラディエント計算を分散し、それらを平均プロセスに戻して平均化し、トレーニングに適用したいと考えています。簡単な例としてTensorFlowは、ミニブッチ勾配を平均して平均化する
、放物線Y = X^2用のN個のデータポイントにニューラルネットを訓練するこのスクリプトを取る:
import tensorflow as tf
import numpy as np
def add_layer(inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.random_normal([1, out_size]))
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
# Make up some real data
N = 50
x_data = np.linspace(-2, 2, N)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) # - 0.5 + noise
# Define placeholder for x_data and y_data
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
""" Build the network"""
# Add hidden layer
l1 = add_layer(xs, 1, 5, activation_function=tf.tanh)
# Add output layer
prediction = add_layer(l1, 5, 1, activation_function=None)
# Define loss
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
# Define optimizer
opt = tf.train.GradientDescentOptimizer(learning_rate=1e-2)
# Compute the gradients for a list of variables.
grads_and_vars = opt.compute_gradients(loss)
# Ask the optimizer to apply the gradients
train_opt = opt.apply_gradients(grads_and_vars)
# Initialize global variables
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(2000):
# training
sess.run(train_opt, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
私は並列化したい部分は、勾配の計算でありますこれらのグラジエントをマスタープロセスに戻して平均化し、トレーニングステップに適用したいと考えています。私はx_data
のN個のデータポイントをP個のプロセスに分けたいと思っています。
私はこれを「同期トレーニング」と呼んでいますが、これはリソースを見てきましたが、誰もそれを説明していないと思います。
この単純な例を同期的に並列化するにはどうすればよいですか?
なぜこれをやりたいですか?その悪い考えを言っているのではない、私はちょうど興味がある – user3684792
私の実際のアプリケーションでは、ノードに多くのレイヤーを持つ深い神経網を使って、約30,000の例があります。ミニバッチトレーニングを並列化できれば、私はこれを恥ずかしそうに並行させることができます。たとえば、30個のプロセスで実行すると、1,000個の例に適合する速度を達成できます。 – Drew
テンソルフローに並列化を処理させるだけの利点は何ですか? – user3684792