为什么使用GPU而不是CPU的Tensorflow速度更慢? [英] Why tensorflow is slower with GPU instead of CPU?

查看:773
本文介绍了为什么使用GPU而不是CPU的Tensorflow速度更慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的神经网络:

This is a really simple neural network:

n_pts = 500000
np.random.seed(0)
Xa = np.array([np.random.normal(13, 2, n_pts),
           np.random.normal(12, 2, n_pts)]).T
Xb = np.array([np.random.normal(8, 2, n_pts),
           np.random.normal(6, 2, n_pts)]).T

X = np.vstack((Xa, Xb))
y = np.matrix(np.append(np.zeros(n_pts), np.ones(n_pts))).T


# Create a new Keras model
model = Sequential()
model.add(Dense(units=1, input_shape=(2,), activation='sigmoid'))
adam = Adam(lr=0.1)
model.compile(adam, loss='binary_crossentropy', metrics=['accuracy'])
h = model.fit(x=X, y=y, verbose=1, batch_size=100000, epochs=15, shuffle='true')

我将批次大小增加到100k,但是cpu比gpu快(9秒钟vs 12(高批次大小),而4x更快,较小批次大小) CPU是Intel i7-8850H,GPU是Nvidia Quadro p600 4gb. 我安装了tensorflow 1.14.0. 使用像这样的更复杂的网络:

I increase the batch size up to 100k but the cpu is faster than the gpu (9 second vs 12 with high batch size and more than 4x faster with smaller batch size) The cpu is the intel i7-8850H and the GPU is the Nvidia Quadro p600 4gb. I installed tensorflow 1.14.0. With a more complex network like this one:

model = Sequential()
model.add(Convolution2D(24, 5, 5, subsample=(2, 2), input_shape=(66, 200, 
3), activation='elu'))
model.add(Convolution2D(36, 5, 5, subsample=(2, 2), activation='elu'))
model.add(Convolution2D(48, 5, 5, subsample=(2, 2), activation='elu'))
model.add(Convolution2D(64, 3, 3, activation='elu'))

model.add(Convolution2D(64, 3, 3, activation='elu'))
# model.add(Dropout(0.5))

model.add(Flatten())

model.add(Dense(100, activation = 'elu'))
#   model.add(Dropout(0.5))

 model.add(Dense(50, activation = 'elu'))
#   model.add(Dropout(0.5))

model.add(Dense(10, activation = 'elu'))
#   model.add(Dropout(0.5))

model.add(Dense(1))

optimizer = Adam(lr=1e-3)
model.compile(loss='mse', optimizer=optimizer)

GPU是否会比CPU更快? 要利用gpu功能需要做什么?

will a GPU be faster than the cpu? What is necessary to do to take advantage of the gpu power?

推荐答案

GPU最适合大规模并行工作负载,而您的简单模型无法实现.数据需要在CPU和GPU之间传输,因此,如果此开销大于实际计算量,则CPU很有可能会更快,因为不会发生传输开销.

GPUs work best with massively parallel workloads, your simple model is not able to achieve that. Data needs to be transfered between CPU and GPU, so if this overhead is bigger than the actual computation, then the CPU will most likely be faster, as no transfer overhead happens.

只有更大的模型才能从GPU加速中获利.

Only a much bigger model would be able to profit from GPU acceleration.

这篇关于为什么使用GPU而不是CPU的Tensorflow速度更慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆