Keras tensorflow backend does not detect GPU - keras

I am running keras with tensorflow backend on linux.
First, I installed tensorflow GPU version by itself, and run the following code to check and found out that it's running on GPU and shows the GPU it's running on, device mapping, etc. The tensorflow I use was from https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-
0.11.0-cp27-none-linux_x86_64.whl
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
Then, I installed keras using conda install keras. I checked conda list and now I have 2 versions of tensorflow (1.1.0 and 0.11.0). I tried import tensorflow as tf which results in:
2017-07-18 16:35:59.569535: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569629: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569690: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569707: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569731: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Device mapping: no known devices.
2017-07-18 16:35:59.579959: I tensorflow/core/common_runtime/direct_session.cc:257] Device mapping:
MatMul: (MatMul): /job:localhost/replica:0/task:0/cpu:0
2017-07-18 16:36:14.369948: I tensorflow/core/common_runtime/simple_placer.cc:841] MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-07-18 16:36:14.370051: I tensorflow/core/common_runtime/simple_placer.cc:841] b: (Const)/job:localhost/replica:0/task:0/cpu:0
a: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-07-18 16:36:14.370109: I tensorflow/core/common_runtime/simple_placer.cc:841] a: (Const)/job:localhost/replica:0/task:0/cpu:0
I already set CUDA_VISIBLE_DEVICES, which works before keras was installed.
Is this because of the tensorflow version? Can I choose to install 0.11.0 instead of 1.1.0 when installing keras?
If the problem is due to tensorflow not detecting a GPU, how can I solve this issue? I read in this link and it says that tensorflow will automatically run on GPU is it detects one.

Chances are that Keras, depending on a newer version of TensorFlow, has caused the installation of a CPU-only TensorFlow package (tensorflow) that is hiding the older, GPU-enabled version (tensorflow-gpu).
I would upgrade the GPU-enabled version first. Usually you can just do pip install --upgrade tensorflow-gpu, but you have Anaconda-specific instructions in the TensorFlow installation page. Then you can uninstall the CPU-only TensorFlow package with pip uninstall tensorflow. Now import tensorflow as tf should actually import the GPU-enabled package which, as you suggest, should in turn detect your GPU automatically.

Related

RuntimeError: cuDNN version incompatibility

I wrote an LSTM NLP classifier with PyTorch, in google colab and it worked well. Now, I run it on google colab pro, but I get this error:
RuntimeError: cuDNN version incompatibility: PyTorch was compiled against (8, 3, 2) but found runtime version (8, 0, 5). PyTorch already comes bundled with cuDNN. One option to resolving this error is to ensure PyTorch can find the bundled cuDNN.one possibility is that there is a conflicting cuDNN in LD_LIBRARY_PATH.
I have no idea how to fix this. I'm using GPU on colab pro.
I've tried this link and it didn't work.
How I declared device:
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
Fixed via upgrading cuDNN to 8.4
reference (https://github.com/JaidedAI/EasyOCR/issues/716)
if you are using google colab uae this command
!pip install --upgrade torch torchvision

Uable to train model using gpu on M1 Mac

How to reproduce the behaviour
install space for apple , select model training option and follow the on screen instructions.
generate config files for model training.
Declare your training and testing corpus in base_config file then auto fill the to generate the final config file.
python -m spacy init fill-config base_config.cfg config.cfg
python -m spacy train config/config.cfg -g 0 --output trf_model-2
Your Environment
Operating System: MacOS 12.4
Python Version Used: 3.10
spaCy Version Used: 3.4.0
Environment Information: -
spaCy version: 3.4.0
Platform: macOS-12.4-arm64-arm-64bit
Python version: 3.10.5
Pipelines: en_core_web_trf (3.4.0)
While I'm trying to train the model using gpu on m1 Mac got this error
RuntimeError: invalid gradient at index 0 - expected device cpu but got mps:0
The motel GPU support for Apple silicon M1 Mac processor is not there in spacy yet,
We have to wait till the spacy team provides proper support for apple silicon based hardware.
As you noted, there's no GPU support for training on Apple Silicon at this time. However, spaCy can leverage some accelerations on Apple Silicon if the thinc-apple-ops module is installed. You can specify it as an option when installing spaCy.
pip install spacy[apple]

Problems getting GPU to work with older version of Tensorflow and Keras

I have a project I'm trying to work on but it's based on code that's a few years old and for whatever reason this code tends to fail if Tensorflow or NumPy aren't the correct versions (which means everything I'm using has to be old). This has meant that I've needed to dual-install an older version of Python to then be able to install the correct versions of the dependencies.
I'm running:
Python 3.7.5
NumPy 1.17.4
Pandas 0.25.3
pyyaml 5.1.2
more_itertools 7.2.0
keras 2.3.1
tensorflow 2.0.1
CUDA 10.0
CuDNN 7.4.1
I'm particularly interested in the keras and tensorflow versions. From my research, it seems they should work with GPU (as is?) according to this:
https://www.tensorflow.org/install/source (towards the bottom under tested build configurations for GPU).
However, when I try to detect GPU devices on my build with
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
I get
2022-03-22 19:34:53.410102: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 1722158755601489749
]
Which seems like it isn't recognising my GPU.
Is there something I'm missing in the setup process that I need to enable GPU support? As far as I can tell my versions of Tensorflow and Keras are compatible with GPU processing and have compatible versions of CUDA and CuDNN installed.

After downgrading Tensorflow 2.0 to 1.5 results changed and results reproduction is not available

Would you help me to achieve reproducible results with Tensorflow 1.15 without restarting Python kernel. And why the output results in TF 2.0 and TF 1.5 are different with absolutely identical parameters and dataset? Is it possible to achieve identical output?
More details:
I tried to interpret model results in TF 2.0 by:
import shap
background = df3.iloc[np.random.choice(df3.shape[0], 100, replace=False)]
explainer = shap.DeepExplainer(model, background)
I recieved an error:
`get_session` is not available when using TensorFlow 2.0.`get_session` is not available when using TensorFlow 2.0.
According to the SO topic, I tried to setup TF 2.0 compatibility with TF 1 by using in the front of my code:
import tensorflow.compat.v1 as tf
But the error appeared again.
Following advice by many users, I downgraded TF2 to TF 1.15 it solved the problem, and shap module interprets the results but:
1) to make results reproducible now I have to change tf.random.set_seed(7) on tf.random.set_random_seed(7) and restart the Python kernel every time! In TF2 I didn't have to restart the kernel.
2) prediction results has been changed, especially, Economical efficiency (that is, TF1.5. wrongly classifies more important samples than TF2.0).
TF 2:
Accuracy: 94.95%, Economical efficiency = 64%
TF 1:
Accuracy: 94.85%, Economical efficiency = 56%
The code of the model is here
First, results differ from each other not only in TF1 and TF2 versions, but also in TF2.0 and TF2.2 versions. Probably, it depends on diffenent internal parameters in the packages.
Second, TensorFlow2 works with DeepExplainer in the following versions:
import tensorflow
import pandas as pd
import keras
import xgboost
import numpy
import shap
print(tensorflow.__version__)
print(pd.__version__)
print(keras.__version__)
print(xgboost.__version__)
print(numpy.__version__)
print(shap.__version__)
output:
2.2.0
0.24.2
2.3.1
0.90
1.17.5
0.35.0
But you will face some difficulties in updating the libraries.
In Python 3.5, running TF2.2, you will face the error 'DLL load failed: The specified module could not be found'.
It 100% can be solved by installing newer C++ package. See this:https://github.com/tensorflow/tensorflow/issues/22794#issuecomment-573297027
Link to download the package:https://support.microsoft.com/ru-ru/help/2977003/the-latest-supported-visual-c-downloads
In Python 3.7 you will not find the shap 0.35.0 version with whl extention. Only tar.gz extension which gives the error: "Install visual c++ package". But installation doesn't help.
Then download shap 0.35.0 for Python 3.7 here: https://anaconda.org/conda-forge/shap/files. Run Anaconda shell. Type: conda install -c conda-forge C:\shap-0.35.0-py37h3bbf574_0.tar.bz2.

Running python code on CUDA

While trying to run this code https://wltrimbl.github.io/2014-06-10-spelman/intermediate/python/04-multiprocessing.html on my GPU system which has 300 cores, i used the comment with tf.device('/GPU:0') on the beginning of code. But found that it does not run on GPU. Then i tried
import tensorflow as tf
tf.device('/GPU:0'): # for run in GPU
init = tf.initialize_all_variables()
# initializing all variables
sess = tf.Session(
config=tf.ConfigProto(
intra_op_parallelism_threads=1))
Does this code run in GPU? or is there any method for run a python code on GPU.
No, it won't run without a GPU optimized version of TensorFlow.
Python multiprocesing is for CPU only.
TensorFlow GPU is available (see here https://www.nvidia.com/en-us/data-center/gpu-accelerated-applications/tensorflow/). The Tensorflow GPU implementation is using CUDA with cuDNN under the hood.
To run your own Python script on GPU, you need to use a library like PyCUDA or Cupy which use the CUDA API under the hood as well.

Resources