title: Why Tensorflow not running on GPU while GPU devices are identified in python?
body: <p>I installed <code>TensorFlow 2.2.0</code> and <code>TensorFlow-gpu 2.2.0</code> in <code>windows 10</code>. Also, I installed <code>CUDA Toolkit v10.1</code> and copy <code>cuDNN 7.6.5</code> files in <code>CUDA directories</code>. My GPU is <code>NVIDIA GeForce 940 MX</code>.
  In addition, I set CUDA Path on windows. When I test devices through the below code, both <code>CPU</code> and <code>GPU</code> are recognized:</p>
  <pre><code>from tensorflow.python.client import device_lib
  device_lib.list_local_devices()
  </code></pre>
  <p>The output is:</p>
  <pre><code>[name: &quot;/device:CPU:0&quot;
   device_type: &quot;CPU&quot;
   memory_limit: 268435456
   locality {
   }
   incarnation: 13265748925766868529,
   name: &quot;/device:XLA_CPU:0&quot;
   device_type: &quot;XLA_CPU&quot;
   memory_limit: 17179869184
   locality {
   }
   incarnation: 14569071601529958377
   physical_device_desc: &quot;device: XLA_CPU device&quot;,
   name: &quot;/device:XLA_GPU:0&quot;
   device_type: &quot;XLA_GPU&quot;
   memory_limit: 17179869184
   locality {
   }
   incarnation: 15045400394346252324
   physical_device_desc: &quot;device: XLA_GPU device&quot;]
  </code></pre>
  <p>But, when I run my code, it seems the codes are run just on CPU. In addition, when I test GPU availability with <code>tf.test.is_gpu_available()</code>, GPU devices cannot be recognized and <code>False value</code> is shown.<br />
  Or when we run <code>tf.config.list_physical_devices('GPU')</code>, an <code>empty list</code> or <code>[]</code> is printed. And when I run <code>tf.config.experimental.list_physical_devices()</code>, these three <code>physical devices</code> are shown in a list:</p>
  <pre><code>[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
   PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU'),
   PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU')]
  </code></pre>
  <p>It is important that when I run <code>tf.config.list_physical_devices('XLA_GPU')</code>, this will be printed:
  <code>[PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU')]</code></p>
  <p>Also, when we run the code, task manager show that <strong>CPU use 96% of its capability and GPU use only 1% of its capability.</strong></p>
  <p>The code we run is as follow:</p>
  <pre><code>import tensorflow as tf
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import LSTM, Dense, Dropout, Bidirectional
  from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard
  from sklearn import preprocessing
  from sklearn.model_selection import train_test_split
  from sklearn.metrics import accuracy_score
  from yahoo_fin import stock_info as si
  from collections import deque
  
  import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  import time
  import os
  import random
  
  
  # set seed, so we can get the same results after rerunning several times
  np.random.seed(314)
  tf.random.set_seed(314)
  random.seed(314)
  
  
  def load_data(ticker, n_steps=50, scale=True, shuffle=True, lookup_step=1, 
                  test_size=0.2, feature_columns=['adjclose', 'volume', 'open', 'high', 'low']):
      # see if ticker is already a loaded stock from yahoo finance
      if isinstance(ticker, str):
          # load it from yahoo_fin library
          df = si.get_data(ticker)
      elif isinstance(ticker, pd.DataFrame):
          # already loaded, use it directly
          df = ticker
      # this will contain all the elements we want to return from this function
      result = {}
      # we will also return the original dataframe itself
      result['df'] = df.copy()
      # make sure that the passed feature_columns exist in the dataframe
      for col in feature_columns:
          assert col in df.columns, f&quot;'{col}' does not exist in the dataframe.&quot;
      if scale:
          column_scaler = {}
          # scale the data (prices) from 0 to 1
          for column in feature_columns:
              scaler = preprocessing.MinMaxScaler()
              df[column] = scaler.fit_transform(np.expand_dims(df[column].values, axis=1))
              column_scaler[column] = scaler
  
          # add the MinMaxScaler instances to the result returned
          result[&quot;column_scaler&quot;] = column_scaler
      # add the target column (label) by shifting by `lookup_step`
      df['future'] = df['adjclose'].shift(-lookup_step)
      # last `lookup_step` columns contains NaN in future column
      # get them before droping NaNs
      last_sequence = np.array(df[feature_columns].tail(lookup_step))
      # drop NaNs
      df.dropna(inplace=True)
      sequence_data = []
      sequences = deque(maxlen=n_steps)
      for entry, target in zip(df[feature_columns].values, df['future'].values):
          sequences.append(entry)
          if len(sequences) == n_steps:
              sequence_data.append([np.array(sequences), target])
      # get the last sequence by appending the last `n_step` sequence with `lookup_step` sequence
      # for instance, if n_steps=50 and lookup_step=10, last_sequence should be of 59 (that is 50+10-1) length
      # this last_sequence will be used to predict in future dates that are not available in the dataset
      last_sequence = list(sequences) + list(last_sequence)
      # shift the last sequence by -1
      last_sequence = np.array(pd.DataFrame(last_sequence).shift(-1).dropna())
      # add to result
      result['last_sequence'] = last_sequence
      # construct the X's and y's
      X, y = [], []
      for seq, target in sequence_data:
          X.append(seq)
          y.append(target)
      # convert to numpy arrays
      X = np.array(X)
      y = np.array(y)
      # reshape X to fit the neural network
      X = X.reshape((X.shape[0], X.shape[2], X.shape[1]))
      # split the dataset
      result[&quot;X_train&quot;], result[&quot;X_test&quot;], result[&quot;y_train&quot;], result[&quot;y_test&quot;] = train_test_split(X, y, test_size=test_size, shuffle=shuffle)
      # return the result
      return result
  
  
  def create_model(sequence_length, units=256, cell=LSTM, n_layers=2, dropout=0.3,
                  loss=&quot;mean_absolute_error&quot;, optimizer=&quot;rmsprop&quot;, bidirectional=False):
      model = Sequential()
      for i in range(n_layers):
          if i == 0:
              # first layer
              if bidirectional:
                  model.add(Bidirectional(cell(units, return_sequences=True), input_shape=(None, sequence_length)))
              else:
                  model.add(cell(units, return_sequences=True, input_shape=(None, sequence_length)))
          elif i == n_layers - 1:
              # last layer
              if bidirectional:
                  model.add(Bidirectional(cell(units, return_sequences=False)))
              else:
                  model.add(cell(units, return_sequences=False))
          else:
              # hidden layers
              if bidirectional:
                  model.add(Bidirectional(cell(units, return_sequences=True)))
              else:
                  model.add(cell(units, return_sequences=True))
          # add dropout after each layer
          model.add(Dropout(dropout))
      model.add(Dense(1, activation=&quot;linear&quot;))
      model.compile(loss=loss, metrics=[&quot;mean_absolute_error&quot;], optimizer=optimizer)
      return model
  
  # Window size or the sequence length
  N_STEPS = 100
  # Lookup step, 1 is the next day
  LOOKUP_STEP = 1
  # test ratio size, 0.2 is 20%
  TEST_SIZE = 0.2
  # features to use
  FEATURE_COLUMNS = [&quot;adjclose&quot;, &quot;volume&quot;, &quot;open&quot;, &quot;high&quot;, &quot;low&quot;]
  # date now
  date_now = time.strftime(&quot;%Y-%m-%d&quot;)
  ### model parameters
  N_LAYERS = 3
  # LSTM cell
  CELL = LSTM
  # 256 LSTM neurons
  UNITS = 256
  # 40% dropout
  DROPOUT = 0.4
  # whether to use bidirectional RNNs
  BIDIRECTIONAL = False
  ### training parameters
  # mean absolute error loss
  # LOSS = &quot;mae&quot;
  # huber loss
  LOSS = &quot;huber_loss&quot;
  OPTIMIZER = &quot;adam&quot;
  BATCH_SIZE = 64
  EPOCHS = 400
  # Apple stock market
  ticker = &quot;AAPL&quot;
  ticker_data_filename = os.path.join(&quot;data&quot;, f&quot;{ticker}_{date_now}.csv&quot;)
  # model name to save, making it as unique as possible based on parameters
  model_name = f&quot;{date_now}_{ticker}-{LOSS}-{OPTIMIZER}-{CELL.__name__}-seq-{N_STEPS}-step-{LOOKUP_STEP}-layers-{N_LAYERS}-units-{UNITS}&quot;
  if BIDIRECTIONAL:
      model_name += &quot;-b&quot;
      
  # create these folders if they does not exist
  if not os.path.isdir(&quot;results&quot;):
      os.mkdir(&quot;results&quot;)
  if not os.path.isdir(&quot;logs&quot;):
      os.mkdir(&quot;logs&quot;)
  if not os.path.isdir(&quot;data&quot;):
      os.mkdir(&quot;data&quot;)
  
  
  # load the data
  data = load_data(ticker, N_STEPS, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE, feature_columns=FEATURE_COLUMNS)
  
  # save the dataframe
  data[&quot;df&quot;].to_csv(ticker_data_filename)
  
  # construct the model
  model = create_model(N_STEPS, loss=LOSS, units=UNITS, cell=CELL, n_layers=N_LAYERS,
                      dropout=DROPOUT, optimizer=OPTIMIZER, bidirectional=BIDIRECTIONAL)
  
  # some tensorflow callbacks
  checkpointer = ModelCheckpoint(os.path.join(&quot;results&quot;, model_name + &quot;.h5&quot;), save_weights_only=True, save_best_only=True, verbose=1)
  tensorboard = TensorBoard(log_dir=os.path.join(&quot;logs&quot;, model_name))
  
  history = model.fit(data[&quot;X_train&quot;], data[&quot;y_train&quot;],
                      batch_size=BATCH_SIZE,
                      epochs=EPOCHS,
                      validation_data=(data[&quot;X_test&quot;], data[&quot;y_test&quot;]),
                      callbacks=[checkpointer, tensorboard],
                      verbose=1)
  
  model.save(os.path.join(&quot;results&quot;, model_name) + &quot;.h5&quot;)
  </code></pre>
  <p><code>**GPU and CPU performance**</code> are as follow:</p>
  <p><a href="https://i.stack.imgur.com/rx5lr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rx5lr.png" alt="enter image description here" /></a></p>
  <p>Could you help me, please?</p>