Mixture Usage Of Cpu And Gpu In Keras
I am building a neural network on Keras, including multiple layers of LSTM, Permute and Dense. It seems LSTM is GPU-unfriendly. So I did research and use With tf.device('/cpu:0')
Solution 1:
As you may read here - tf.device
is a context manager which switches a default device to this passed as its argument in a context (block) created by it. So this code should run all '/cpu:0'
device at CPU
and rest on GPU
.
The question will it speed up your training is really hard to answer because it depends on the machine you use - but I don't expect computations to be faster as each change of a device makes data to be copied between GPU RAM
and machine RAM
. This could even slow down your computations.
Solution 2:
I have created a model using 2 LSTM and 1 dense layers and trained it in my GPU (NVidia GTX 10150Ti) Here is my observations.
- use CUDA LSTM https://keras.io/layers/recurrent/#cudnnlstm
- Use a bath size which helps more GPU parallelism, if I use a very small batch size(2-10) GPU multi cores are not utilized; so I used 100 as batch size
- If I train my network on GPU and try to use it for predictions on CPU, it works in-terms of compiling and running but the predictions are weird. In my case I have the luxury to use a GPU for prediction as well.
- for multi layer LSTM, need to use
here is some sample snippet
model = keras.Sequential()
model.add(keras.layers.cudnn_recurrent.CuDNNLSTM(neurons
, batch_input_shape=(nbatch_size, reshapedX.shape[1], reshapedX.shape[2])
, return_sequences=True
, stateful=True))
Post a Comment for "Mixture Usage Of Cpu And Gpu In Keras"