繁体   English   中英

使用多个GPU进行回归

[英]Using multiple gpus for regression

我正在尝试为回归问题编写张量流代码。 我已经编写了代码,并且可以在单个GPU上运行时完美运行。 但是,我想测试2 gpu的速度有多快。 我已经使用tf.device修改了2 gpus的代码。 但是我不确定如何编码cpu上的损失并基于此更新权重? 我什至没有写正确? 另一个问题是批处理大小,如何修改2 gpu的批处理大小?

import numpy as np
import tensorflow as tf
import threading
from sklearn import cross_validation

X_train0 = np.load('XTrain006.npy')
ncol = X_train0.shape[0]

Y_train00 = np.load('YTrain006.npy')
Y_train0 = Y_train00[:,:]
print Y_train0.shape[0]

X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X_train0, Y_train0, test_size=0.1, random_state=42)

XMEAN=np.max(X_train,axis=0)
XSTDD=np.min(X_train,axis=0)
X_train=(X_train-XSTDD)/(XMEAN-XSTDD)
X_test=(X_test-XSTDD)/(XMEAN-XSTDD)
#print X_train[0:10,189:219]

for j in range(3808):
 if((XMEAN[j]-XSTDD[j])== 0):
  X_train[:,j] = 0.0
  X_test[:,j] = 0.0

print XMEAN[189:219]
print XSTDD[189:219]
np.savetxt('xdiff.dat',XMEAN-XSTDD)
np.savetxt('xmin.dat',XSTDD)
np.savetxt('xmax.dat',XMEAN)

YMEAN=np.max(Y_train,axis=0)
YSTDD=np.min(Y_train,axis=0)
Y_trainYY = Y_train
Y_train=(Y_train-YSTDD)/(YMEAN-YSTDD)
Y_test=(Y_test-YSTDD)/(YMEAN-YSTDD)

for j in range(1035):
 if(YMEAN[j]-YSTDD[j]== 0):
  Y_train[:,j] = Y_trainYY[:,j]

np.savetxt('ydiff.dat',YMEAN-YSTDD)
np.savetxt('ymin.dat',YSTDD)
np.savetxt('ymax.dat',YMEAN)


total_len = X_train.shape[0]

learning_rate = 0.001
training_epochs = 50
batch_size = 64
display_step = 1

# Network Parameters
n_hidden_1 = 64 # 1st layer number of features
n_hidden_2 = 64 # 2nd layer number of features
n_hidden_3 = 64
n_input = 3808
n_classes = 1035

for d in ['/device:GPU:0', '/device:GPU:1']:
  with tf.device(d):
# tf Graph input
   x = tf.placeholder("float64", [None, 3808])
   y = tf.placeholder("float64", [None, 1035])

# Create model
  def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.sigmoid(layer_1)

    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.sigmoid(layer_2)

    # Hidden layer with RELU activation
    layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
        layer_3 = tf.nn.sigmoid(layer_3)

    # Output layer with linear activation
    out_layer = tf.matmul(layer_3, weights['out']) + biases['out']
    return out_layer

with tf.device('/cpu:0'):
# Store layers weight & bias
 weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0, 1.0,  seed = None, dtype=tf.float64)),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0, 1.0, seed = None,  dtype=tf.float64)),
    'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3], 0, 1.0, seed = None,  dtype=tf.float64)),
    'out': tf.Variable(tf.random_normal([n_hidden_3, n_classes], 0, 1.0, seed = None,  dtype=tf.float64))
 }
 biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1], 0, 1.0, seed = None, dtype=tf.float64)),
    'b2': tf.Variable(tf.random_normal([n_hidden_2], 0, 1.0, seed = None, dtype=tf.float64)),
    'b3': tf.Variable(tf.random_normal([n_hidden_3], 0, 1.0, seed = None, dtype=tf.float64)),
    'out': tf.Variable(tf.random_normal([n_classes], 0, 1.0, seed = None, dtype=tf.float64))
 }

for d in ['/device:GPU:0', '/device:GPU:1']:
  with tf.device(d):
# Construct model
   pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
   cost = tf.reduce_mean(tf.square(pred-y))
   optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(total_len/batch_size)
        # Loop over all batches
        for i in range(total_batch-1):
            batch_x = X_train[i*batch_size:(i+1)*batch_size]
            batch_y = Y_train[i*batch_size:(i+1)*batch_size]
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c, p = sess.run([optimizer, cost, pred], feed_dict={x: batch_x,
                                                          y: batch_y})
            # Compute average loss
            avg_cost += c / total_batch

        # sample prediction
        label_value = batch_y
        estimate = p
        err = label_value-estimate
        print ("num batch:", total_batch)

        # Display logs per epoch step
        if epoch % display_step == 0:
            print ("Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
            print ("[*]----------------------------")
            for i in xrange(3):
                print ("label value:", label_value[i], \
                    "estimated value:", estimate[i])
            print ("[*]============================")

    print ("Optimization Finished!")

    # Test model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float64"))
    print ("Accuracy:", accuracy.eval({x: X_test, y: Y_test}))

更新:感谢您的答复。 它仍然仅使用一个GPU。 如果在sess中需要进行任何更改,您可以指导我吗? 我的意思是不是所有gpu的成本都需要发送到cpu以更新权重和偏差吗?

+-----------------------------------------------------------------------------

+
| NVIDIA-SMI 390.30                 Driver Version: 390.30                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K40c          Off  | 00000000:03:00.0 Off |                    0 |
| 23%   42C    P0    63W / 235W |  10875MiB / 11441MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla K40c          Off  | 00000000:04:00.0 Off |                    0 |
| 23%   44C    P0    72W / 235W |  10915MiB / 11441MiB |     42%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Quadro K620         Off  | 00000000:84:00.0  On |                  N/A |
| 44%   56C    P0     4W /  30W |    997MiB /  1999MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+ ------------------------------------------------- ----------------------------

+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0    117392      C   python                                     10861MiB |
|    1    117392      C   python                                     10901MiB |
|    2    117392      C   python                                        17MiB |
|    2    135765      G   /usr/lib/xorg/Xorg                           623MiB |
|    2    136476      G   compiz                                       341MiB |
+-----------------------------------------------------------------------------+

您需要会话以获取正确的启用GPU的配置。

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM