Explore Courses Blog Tutorials Interview Questions
+1 vote
in Machine Learning by (230 points)
Currently, I have Keras with TensorFlow and CUDA at the backend. But, I want to force Keras to use the CPU, at times. So here my question is, whether it can be done on a virtual environment without installing a separate CPU-only TensorFlow. If yes, I would like to know how? I have heard that the flags could be set if Theano is at the backend. But, I have no idea if TensorFlow flags are accessible via Keras.

1 Answer

+3 votes
by (10.9k points)
edited by

@malika , There are various ways of doing this some are as follows:

1. import the following before Keras or Tensorflow is imported:

import os

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"   

os.environ["CUDA_VISIBLE_DEVICES"] = ""

2. Another way is to run your script using:


3.You can also try using tf.device():

with tf.device('/gpu:0'):

    a = tf.placeholder(tf.float32, shape=(None, 20, 64))

    b = LSTM(32)(a)

with tf.device('/cpu:0'):

    a = tf.placeholder(tf.float32, shape=(None, 20, 64))

    b = LSTM(32)(a) 

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.1k users

Browse Categories