Linux 16.04下安装tensorflow(CPU)以及pycharm配置

一、安装tensorflow

1.安装编译工具Bazel

    首先要安装JDK8。

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

    最后一步巨慢,保持网络畅通。

    然后安装Bazel的其他依赖的工具包。

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python3

    在Bazel的GitHub发布页面下载安装包(https://github.com/bazelbuild/bazel/releases),选择bazel-0.15.0-installer-linux-x86_64.sh,然后运行:   

chmod +x bazel-0.15.0-installer-linux-x86_64.sh
./bazel-0.15.0-installer-linux-x86_64.sh -user
export PATH="$PATH:$HOME/bin"

2.安装TensorFlow依赖的其他工具包。

sudo apt-get install python3-numpy python3-pip python3-dev python3-wheel

3.配置TensorFlow编译环境

    (1)从GitHub下载源码:

git clone https://github.com/tensorflow/tensorflow

    (2)源码下载完成之后,需要运行configure脚本来配置环境信息:

cd tensorflow/
./configure 
Please specify the location of python. [Default is /home/gz/anaconda3/bin/python]: 
Found possible Python library paths:
  /home/gz/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/home/gz/anaconda3/lib/python3.6/site-packages]

Using python library path: /home/gz/anaconda3/lib/python3.6/site-packages
Do you wish to build TensorFlow with MKL support? [y/N] 
No MKL support will be enabled for TensorFlow
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 
Do you wish to use jemalloc as the malloc implementation? [Y/n] 
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] 
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N] 
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] 
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N] 
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N] 
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] 
No CUDA support will be enabled for TensorFlow
Extracting Bazel installation...
.................
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
Configuration finished

    (3)通过Bazel来编译pip的安装包(比较耗时):

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

    (4)上述命令生成一个build_pip_package文件,运行以下命令,在/tmp/tensorflow_pkg 文件夹中创建pip的安装包:

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

    (5)用pip命令安装:

pip install /tmp/tensorflow_pkg/tensorflow-1.9.0rc0-cp36-cp36m-linux_x86_64.whl

4.TensorFlow测试

gz@Storm:~$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
/home/gz/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
>>> hello = tf.constant('hello tensorflow')
>>> sess = tf.Session()
2018-06-30 10:19:07.320775: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
>>> print(sess.run(hello))
b'hello tensorflow'

    安装成功!

二、pycharm配置

    (1)打开Pycharm软件,点击File->Default Settings->Project Interpreter

    右边设置按钮选择 Add local ->Virtualenv Environment->Existing Environment,将python3.6的python.exe路径选择即可。

    如果不知道路径,可以输入以下命令得到路径:

gz@Storm:~$ source activate tensorflow
(tensorflow) gz@Storm:~$ which python
/home/gz/anaconda3/envs/tensorflow/bin/python

    点击OK,即完成配置。

    运行成功!

猜你喜欢

转载自blog.csdn.net/shuaigezhou123/article/details/80864008
今日推荐