代码之家  ›  专栏  ›  技术社区  ›  Thermodynamix

并行张量流平均小批量梯度

  •  2
  • Thermodynamix  · 技术社区  · 7 年前

    作为一个简单的示例,以该脚本为例,该脚本在抛物线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}))
    

    我想并行化的部分是梯度的计算,然后我想把这些梯度带回到主过程进行平均,然后应用到训练步骤。我想将N个数据点拆分为 x_data

    我认为这就是所谓的“同步训练”,我看过相关的参考资料,但没有人解释过。

    1 回复  |  直到 7 年前
        1
  •  3
  •   toto2    7 年前

    你可能不会发现太多关于同步训练的内容,因为它大多被放弃,转而支持异步训练。

    从我上面写的内容来看,异步下降似乎是错误的,但您必须理解,随机梯度下降是一个草率/不精确的过程,并且从异步更新中添加额外草率并不有害。另一方面,在执行同步更新时,一些GPU经常处于空闲状态,因为它们必须等待所有其他GPU完成。

    paper 来自Jeff Dean,但他们没有分析同步与异步。

    tensorflow官方文档中有一个示例 asynchronous training ,但可能有更好的教程。

    synchronous training example