Keras LSTM在CPU上比GPU快吗? [英] Keras LSTM on CPU faster than GPU?
问题描述
我正在Keras上测试LSTM网络,并且在CPU(i2600k 16GB上为5秒/纪元)上的训练比在GPU(Nvidia 1060 6GB上为35秒)上的训练要快得多. GPU利用率大约为15%,而尝试包括Keras示例在内的其他LSTM网络时,我从来没有看到超过30%的利用率.当我运行其他类型的网络MLP和CNN时,GPU更快.我正在使用最新的theano 0.9.0dev4和keras 1.2.0
I am testing LSTM networks on Keras and I am getting much faster training on CPU (5 seconds/epoch on i2600k 16GB) than on GPU (35secs on Nvidia 1060 6GB). GPU utilisation runs at around 15%, and I never see it over 30% when trying other LSTM networks including the Keras examples. When I run other types of networks MLP and CNN the GPU is much faster. I am using the latest theano 0.9.0dev4 and keras 1.2.0
该序列具有3个输入(整数)的50,000个时间步长.
The sequence has 50,000 timesteps with 3 inputs (ints).
如果输入是降序(3,2,1),则输出为0,如果升序,则输出为1,除非最后两个为也上升,则输出为0而不是1.
If the inputs are descending (3,2,1) the output is 0, and 1 if ascending, except if the last two were also ascending, then the output is 0 instead of 1.
250个纪元后,我获得了99.97%的准确度,但是为什么GPU这么慢?我在模型中做错了吗?我尝试了各种批处理设置,但仍然遇到相同的问题.
After 250 epochs I get 99.97% accuracy, but why is the GPU so much slower? am I doing something wrong in the model? I tried various batch settings and still had the same issue.
def generate_data():
X=[]
Y=[]
for i in range(50000):
start=random.randint(1,100)
d=random.randrange(-1,2,2) #-1 or 1
param=[(start),(start+d),(start+d+d)]
X.append(np.array(param))
if d<0:
Y.append([1,0])
elif len(Y)>2 and d>0 and Y[-1][1]==1 and Y[-2][1]==1:
Y.append([1,0])
elif d>0:
Y.append([0,1])
X=np.array(X)
Y=np.array(Y)
return X,Y
X,Y = generate_data()
X=np.asarray(X,'float32')
Y=np.asarray(Y,'float32')
X=np.reshape(X,(1,len(X),3))
Y=np.reshape(Y,(1,len(Y),2))
model=Sequential()
model.add(LSTM(20, input_shape=(50000,3), return_sequences=True))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])
history = model.fit(X, Y,batch_size=100, nb_epoch=250, verbose=2)
有什么想法吗?谢谢!
推荐答案
使用Keras的CuDNNLSTM
单元在Nvidia GPU上加速计算:
Use Keras' CuDNNLSTM
cells for accelerated compute on Nvidia GPUs: https://keras.io/layers/recurrent/#cudnnlstm
只需将LSTM
行更改为:
model.add(CuDNNLSTM(20, input_shape=(50000,3), return_sequences=True))
这篇关于Keras LSTM在CPU上比GPU快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!