1. 程式人生 > >The TensorFlow library wasn't compiled to use SSE4.1/SSE4.2/AVX/AVX2/FMA instructions, but these are

The TensorFlow library wasn't compiled to use SSE4.1/SSE4.2/AVX/AVX2/FMA instructions, but these are

執行如下命令是報錯:
# python -c "import tensorflow as tf; print(tf.Session().run(tf.constant('Hello, TensorFlow')))"
The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Hello, TensorFlow

解決辦法:
使用pip install 安裝的tensorflow就存在這個問題,要想解決這個問題,需要原始碼安裝:

1,首先解除安裝tensorflow
pip uninstall tensorflow

2,安裝bazel
編譯tensorflow 需要bazel 編譯器

3,下載最新的tensorflow (現在是1.3版本)
git clone https://github.com/tensorflow/tensorflow

4, 進入tensorflow資料夾下,執行configure指令碼
cd tensorflow && ./configure

注:執行命令後出現一些選項供選擇,按需選擇即可。
若安裝支援cpu的tensorflow時,建議選擇全部預設配置即可,即每步都直接回車。
若安裝支援GPU的tensorflow時,選好cuda或者cudnn的版本即可

5,bazel build 
bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package

注:這步操作比較耗時。結束後,會在路徑tensorflow/tools/pip_package下產生一個指令碼build_pip_package。
這個指令碼是用與生產“.whl”檔案的。

6,執行上面產生的指令碼,生產.whl檔案
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

注:上面命令執行結束後,會在目錄/tmp/tensorflow_pkg下產生檔案:tensorflow-1.3.0-cp27-cp27mu-linux_x86_64.whl

7,安裝tensorflow
pip install /tmp/tensorflow_pkg/tensorflow-1.3.0-cp27-cp27mu-linux_x86_64.whl

8,再次驗證
# python -c "import tensorflow as tf; print(tf.Session().run(tf.constant('Hello, TensorFlow')))"
Hello, TensorFlow

9,手工打完。