前言
- 对于不支持AVX指令集的CPU服务器,在python中使用 import tensorflow as tf 命令时,若tensorflow版本高于1.5.0会进行如下报错。
- ubuntu 16.04:非法指令(核心转储);
- Win 10:ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
解决方案
1.将tensorflow版本调至1.5.0及以下
pip3 install tensorflow==1.5.0
2.使用源码安装tensorflow
- windows系统可下载 https://github.com/fo40225/tensorflow-windows-wheel(fo40255)提供的预构建二进制文件。
- liunx系统(ubuntu 16.04)见下。
使用源码安装Tensorflow
- 安装bazel
- 安装JDK8
$ sudo apt-get install openjdk-8-jdk
- 添加bazel包发布源
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list $ curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add
- 安装bazel,更新与修改
$ sudo apt-get update && sudo apt-get install bazel $ sudo apt-get upgrade bazel $ chmod +x ./bazel-0.13.0-installer-linux-x86_64.sh $ ./bazel-0.13.0-installer-linux-x86_64.sh
- 安装JDK8
-
安装依赖包
$ sudo apt install python-numpy swig python-dev python-wheel
- ubuntu 16.04默认为 python2.7,若想要运行在python3环境下将命令修改成:
$ sudo apt install python3-numpy swig python3-dev python3-wheel
-
下载Tensorflow源码
- 克隆tensorflow源码:
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
- 配置,进入tensoeflow所在文件夹执行以下命令:
$ ./configure
- 注意点:
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 # 填写系统中python安装位置,python2.7默认回车,python3需对其手动输入以作修改 Please input the desired Python library path to use. Default is [/usr/local/lib/python2.7/dist-packages]: /usr/local/lib/python3/dist-packages # 同上,2.7可回车默认 # 其他选项无默认需求,我都选了n,具体对应的意思补充在最后以免日后用到。
- 克隆tensorflow源码:
-
由源码编译Tensoeflow
- 编译:
$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package #若cpu不支持avx而支持sse指令集,使用如下命令 $ bazel build -c opt --copt=-msse4.1 --copt=-msse4.2 //tensorflow/tools/pip_package:build_pip_package
- 可能碰到的问题:
ModuleNotFoundError: No module named 'keras_applications' Target //tensorflow/tools/pip_package:build_pip_package failed to build INFO: Elapsed time: 695.098s, Critical Path: 152.03s INFO: 7029 processes: 7029 local. FAILED: Build did NOT complete successfully
- 造成原因[1]
This appears to be a problem with Tensorflow 1.10 build. I recommend you check out the r1.9 branch as it builds totally fine. Either the dependency list needs to be updated or Tensorflow will fix this. If you are determined to run the r.1.10 api then run the following in terminal:'
· 以下在python3环境中则改为pip3
pip install keras_applications==1.0.4 --no-deps
pip install keras_preprocessing==1.0.2 --no-deps
pip install h5py==2.8.0
将Tensorflow转为.whl文件
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/
pip安装Tensorflow包(注意改为对应生成包名
$ pip3 install ~/tensorflow-1.11.0-cp35-cp35mu-linux_x86_64.whl
参考
[1] https://stackoverflow.com/questions/51771039/error-compiling-tensorflow-from-source-no-module-named-keras-applications
[2] https://www.cnblogs.com/billux/p/9040700.html # [TensorFlow源码安装]
[3] https://blog.csdn.net/nicholas_wong/article/details/76474751 # [bazel安装]
[4] http://www.cnblogs.com/mazhiyong/p/9466988.html # ./configure中问题的释义