TensorFlow安装

read more on github

1. 安装

1.1 ubuntu安装

1.2 Mac OS安装

TensorFlow Python API 依赖 Python 2.7 版本

1
2
brew install python2.7
pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

1.3 使用容器VirtualEnv安装

推荐使用 virtualenv 创建一个隔离的容器, 来安装 TensorFlow. 这样做能使排查安装问题变得更容易

1
2
3
# 在 Mac 上:
sudo easy_install pip # 如果还没有安装 pip
sudo pip install --upgrade virtualenv
  • 建立一个全新的 virtualenv 环境,将环境建在 ~/tensorflow 目录下

    1
    2
    virtualenv --system-site-packages ~/tensorflow
    cd ~/tensorflow
  • 激活 virtualenv

    1
    2
    3
    source bin/activate  # 如果使用 bash
    source bin/activate.csh # 如果使用 csh
    (tensorflow)$ # 终端提示符应该发生变化
  • 在 virtualenv 内, 安装 TensorFlow

    1
    2
    3
    4
    5
    6
    7
    8
    9
    (tensorflow)$ pip install --upgrade <$url_to_binary.whl>

    <$url_to_binary.whl>选择:
    # Mac只支持 CPU
    https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl
    # Linux只支持 CPU
    https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
    # 开启 GPU 支持的版本 (安装该版本的前提是已经安装了 CUDA sdk)
    https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
  • 运行 TensorFlow 程序

    1
    2
    (tensorflow)$ cd tensorflow/models/image/mnist
    (tensorflow)$ python convolutional.py

    or

    1
    2
    3
    4
    5
    6
    python
    import tensorflow as tf
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print sess.run(hello)
    Hello, TensorFlow!
  • 当使用完 TensorFlow

    1
    2
    # 停用 virtualenv
    (tensorflow)$ deactivate

1.4 从源码安装

  • clone TensorFlow 仓库

    1
    2
    # --recurse-submodules 参数是必须得, 用于获取 TesorFlow 依赖的 protobuf 库
    git clone --recurse-submodules https://github.com/tensorflow/tensorflow
  • Mac OS安装
    所需依赖:Bazel, SWIG

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
      brew install bazel
    brew install swig
    ```


    ## 2. 栗子
    ### 2.1 For Java
    [example](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java)

    JDK1.7

    * Start Using Apache Maven

    - 创建Maven工程
    - 添加依赖

    org.tensorflow tensorflow 1.1.0-rc1
    1
    2
    - 下载样例代码添加至```src/main/java/org/tensorflow/examples```

    mkdir -p src/main/java/org/tensorflow/examples curl -L "https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java" -o src/main/java/org/tensorflow/examples/LabelImage.java
    1
    2
    3
      
    - 编译

    mvn compile exec:java ```
  • Start Using java and javac

2.2 Python Example