如何在GPU中的Tensorflow上并行运行独立循环

时间:2017-10-26 19:19:59

标签: python tensorflow gpu parallels logistics

我想通过仿真实现该算法来重建受限玻尔兹曼机器。 M是可见变量的数量,N是隐藏变量的数量。为了在GPU上并行运行,我想首先使用python在tensorflow上编写它。

  1. 在我的主要功能RBMIC()中,我需要运行具有L1惩罚的M独立物流回归并更新我的权重和偏差矩阵:(w和b)然后使用它们来估算隐藏变量的值。所以我写了一个独立的for循环。我想知道tensorflow是否可以识别独立的for循环并在GPU上有效地运行它(每个核心有一次迭代)?

  2. 代码也非常慢,特别是对于运行物流回归,因为它需要运行epochs = 1000次以最小化损失函数。但是如果我使用sklearn.linear_model.LogisticRegression,我发现它非常快。为什么这是一个巨大的差异?但是为了使用GPU,我仍然想用tensorflow来写物流回归。谁能给我一些关于如何更有效地写它的建议?

  3. 当我编写物流回归函数:LogisticsReg()时,我需要得到权重和偏差,我还需要它们作为张量流变量保留以供我进一步计算。但是根据我的函数:LogisticsReg(),它在sess.run()之后返回非张量变量。所以我再次将它们转换为张量变量。这部分是否合理?或者是否有任何有效的方法将其保持在张量变量中,然后可以用来更新权重和偏置矩阵(w和b)?谢谢你的建议!

  4. 我对tensorflow和python很新。抱歉中断,谢谢你的时间!!

    
    
    import numpy as np
    import tensorflow as tf
    
    
    n = 200
    M = 4
    N = 2
    mu, sigma = 0, 0.1
    beta = 0.001
    lr=0.05
    epochs = 1000
    Maxepochs = 10
    iteration = 100
    
    
    visible = np.array([[1,0,1,0],[0,1,1,0],[1,0,0,1],[0,1,0,1]])
    vis = np.tile(visible,50).reshape(n,M)
    vis = tf.cast(vis, tf.float32)
    err_hat = np.zeros([iteration])
    
    
    def Bionimal(x):
        sample = tf.where(tf.random_uniform(shape=x.shape) - x < 0,
                          tf.ones(shape=x.shape), tf.zeros(shape=x.shape))
        return sample
    
    def LogisticsReg(X, Y, wj, bj, beta, lr, epochs):
        logitj = tf.add(tf.matmul(X, wj), bj)
        loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=Y, logits=logitj))
        l1_regularizer = tf.reduce_sum(tf.abs(wj))
        loss = tf.reduce_mean(loss + beta * l1_regularizer)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(loss, var_list=[wj, bj])
        with tf.Session() as sess:
            tf.global_variables_initializer().run()
            for k in range(epochs):  # train the model n_epochs times
                _, bf, wf = sess.run([optimizer, bj, wj])
        # bf = tf.Variable(bf,name="bf")
        # wf = tf.Variable(wf,name="wf")
        return [bf, wf]
    
    def UpdateC(wi, hi, c_opt, Maxepochs):
        ph = tf.sigmoid(tf.add(tf.matmul(vis, tf.transpose(wi)), c_opt))
        lik = tf.add(tf.multiply(hi, tf.log(ph)), tf.multiply((1. - hi), tf.log(1. - ph)))
        loss2 = -tf.reduce_sum(lik)
        optimizer = tf.contrib.opt.ScipyOptimizerInterface(loss2, var_to_bounds={c_opt: (-1,1)},options={'maxiter': Maxepochs}, var_list=[c_opt])
        with tf.Session() as sess:
            tf.global_variables_initializer().run()
            optimizer.minimize(sess)
            return sess.run(c_opt)
    
    
    
    # initial
    w = tf.Variable(tf.random_normal(shape=(N, M), stddev=0.1), name="weights")
    c = tf.Variable(tf.random_normal(shape=(1, N), stddev=0.1), name="hbias")
    b = tf.Variable(tf.random_normal(shape=(1, M), stddev=0.1), name="vbias")
    
    
    def RBMIC(w,c,vis):
        # calculate hidden variables
        logits = tf.add(tf.matmul(vis, tf.transpose(w)), tf.tile(c, [n, 1]))
        prob = tf.sigmoid(logits)
        hids = Bionimal(prob)
        # estimate bias, weight by logistics regression with l1 penalty and also bias c for visible variables.
        bs = np.zeros([1, M])
        ws = np.zeros([N, M])
        X = hids
        for j in range(M):
            Y = tf.reshape(vis[:, j], [n, 1])
            wj = tf.Variable(tf.reshape(w[:, j], [N, 1]), name="wj")
            bj = tf.Variable(tf.random_normal(shape=[1, 1], stddev=0.1), name="bj")
            bf, wf = LogisticsReg(X, Y, wj, bj, beta, lr, epochs)
            bs[0, j] = bf
            ws[:, [j]] = wf
        b = tf.cast(tf.Variable(bs, name="vbias"), tf.float32)
        w = tf.cast(tf.Variable(ws, name="weights"), tf.float32)
        cs = np.zeros([1, N])
        for i in range(N):
            wi = tf.reshape(w[i, :], [1, M])
            hi = tf.reshape(hids[:, i], [n, 1])
            c_opt = tf.Variable(c[0, i], name="c_opt")
            cs[0, i] = UpdateC(wi, hi, c_opt, Maxepochs)
        c = tf.cast(tf.Variable(cs, name="hbias"), tf.float32)
        # evaluate performance
        vis_pred = tf.sigmoid(tf.add(tf.matmul(hids, w), tf.tile(b, [n, 1])))
        err = tf.reduce_sum((vis_pred - vis) ** 2)
        return err
    
    
    
    for step in range(iteration):  # train the model iteration times
            err = RBMIC(w,c,vis)
            init = tf.global_variables_initializer()
            sess = tf.Session()
            sess.run(init)
            print 'reconstruct at step %d = \n' % (step)
            print sess.run(err)
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:1)

要回答帖子标题中的问题,控制流构造tf.while_loop支持并行执行迭代(通过关键字参数parallel_iterations公开)。

关于第二和第三个问题,您可能不希望创建多个会话。例如,如果您使用单个会话,则不必将张量转换为变量。我强烈建议您查阅教程和文档,以获取有关TensorFlow graphs and sessions语义的更多信息。