CentOS7环境下Keras(TensorFlow为后端)无法调用GPU及运行报错求助
Hey there, let's work through this issue step by step. The core problem here is that Keras is defaulting to the Theano backend instead of TensorFlow (which is what you intended to use). On top of that, there's a missing MKL dependency and a BLAS configuration error for Theano. Here's how to fix everything:
Step 1: Force Keras to Use TensorFlow as Backend
Keras relies on a configuration file to pick its backend. Let's create or update this file to prioritize TensorFlow:
- First, make sure the Keras configuration directory exists. If not, create it with:
mkdir -p ~/.keras - Create or edit the
~/.keras/keras.jsonfile with the following content:{ "image_data_format": "channels_last", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" } - Save the file. The next time you launch Keras, it should automatically use TensorFlow as the backend.
Step 2: Resolve Theano's MKL Warning (if you need Theano later)
If you ever plan to use Theano again down the line, install the missing MKL dependencies using your conda environment:
conda activate tensorgpu conda install mkl-service mkl
This will eliminate the "No module named 'mkl'" warning.
Step 3: Fix Theano's BLAS Config Error (optional)
The KeyError: 'blas' comes from a missing section in Theano's configuration file. If you want to keep Theano properly configured, create or edit the ~/.theanorc file and add this section:
[blas] ldflags = -lmkl_rt -lpthread -lm -ldl
Note: If you're using OpenBLAS instead of MKL, adjust the ldflags to match your setup. That said, since your goal is to use TensorFlow, this step might not be necessary once you switch the backend.
Verify the Fix
To confirm everything is working, run a quick test in Python:
import keras print(keras.backend.backend())
You should see tensorflow printed out, confirming the backend switch was successful.
内容的提问来源于stack exchange,提问作者Jose

