在CPU上运行Tensorflow比在GPU上运行要快

在CPU上运行Tensorflow比在GPU上运行要快,tensorflow,keras,windows-10,gpu,cpu,Tensorflow,Keras,Windows 10,Gpu,Cpu,我有一台华硕n552vw笔记本电脑,配有4GB专用Geforce GTX 960M图形卡。我把这些代码行放在我的代码的开头,比较使用GPU或CPU的训练速度,我看到似乎使用CPU赢了 对于GPU: import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' 对于CPU: import os os.environ['CUDA_VISIBLE_DEVICES'] = '-1' 我已经安装了CUDA、cuDNN、tensorflow gpu等来提高我的训练

我有一台华硕n552vw笔记本电脑,配有4GB专用Geforce GTX 960M图形卡。我把这些代码行放在我的代码的开头,比较使用GPU或CPU的训练速度,我看到似乎使用CPU赢了

对于GPU:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
对于CPU:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
我已经安装了CUDA、cuDNN、tensorflow gpu等来提高我的训练速度,但似乎发生了相反的事情

当我尝试第一个代码时,它说(在执行开始之前):

它的速度非常慢,
[在263.2s中完成]
,但当我尝试第二个代码时,它会说:

Train on 2128 samples, validate on 22 samples
Epoch 1/1
2019-08-02 18:51:43.021867: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-08-02 18:51:43.641123: E tensorflow/stream_executor/cuda/cuda_driver.cc:300] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2019-08-02 18:51:43.645072: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:161] retrieving CUDA diagnostic information for host: DESKTOP-UQ8B9FK
2019-08-02 18:51:43.645818: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:168] hostname: DESKTOP-UQ8B9FK
而且它比第一个代码要快得多
[在104.7s中完成]
!这怎么可能

编辑:这是与Tensorflow相关的代码部分:

model = Sequential()
model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = False)))
model.add(Dropout(dp)) 

model.add(RepeatVector(rp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))   

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add(TimeDistributed(Dense(ds))) 

这里有两个相关问题:

  • 模型需要“足够大”才能从GPU加速中获益,因为训练数据需要传输到GPU,新的权重需要从GPU下载,而这种开销会降低效率,使事情变得更慢
  • 对于循环层,并行化它们并不容易,因为它们有很多跨时间步的顺序计算。您可以考虑使用该层而不是普通的LSTM,因为它是为GPU使用而优化的。

一般来说,对于小型型号,GPU上的培训可能不会比CPU上的培训快。

您培训的是哪种型号?请为它添加代码。@MatiasValdenegro:代码很大而且是私有的,但我使用的是
keras
库的LSTM模型。我也尝试了不同的批量大小,从5到50,但没有发生任何变化。当然,然后只描述模型,有多少层?多少个参数?@MatiasValdenegro:我编辑了我的问题。非常感谢你们的评论。你能估计有多少层会被视为大模型,并且适合在GPU上进行训练吗?请提供更多关于“足够大”模型的详细信息。@Ensan3Camel不,我们无法估计,这是你通过实验确定的。太多的因素需要考虑。使用<代码> CUDNNLSTM < /代码>造成了更多的差异!谢谢大家!@记住投票并接受答案。
model = Sequential()
model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = True)))
model.add(Dropout(dp)) 

model.add((LSTM(un , return_sequences = False)))
model.add(Dropout(dp)) 

model.add(RepeatVector(rp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))   

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add((LSTM(un , return_sequences= True))) 
model.add(Dropout(dp))

model.add(TimeDistributed(Dense(ds)))