tensorflow开始

安装

使用Anaconda

在Anaconda环境中使用Tensorflow

  • 下载 Anacoda

  • 创建环境 conda create python=3.6 -n tensorflow; 删除环境conda env remove -n tensorflow

  • 激活环境 source activate tensorflow

  • 安装Tensorflow

    # 升级pip,这里安装python3.x版本tensorflow,可以使用pip -V命令查看pip版本
    # 确认是python3.x版本的pip,或者用pip3命令代替pip
    pip install --upgrade pip    
    
    # tfBinaryURL: tensorflow 二进制文件URL地址
    # 这里找对应版本的url:
    # https://www.tensorflow.org/install/install_linux#the_url_of_the_tensorflow_python_package
    pip install --ignore-installed --upgrade tfBinaryURL    
    # 以安装python3.6 cpu版本为例
    pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.2.1-cp36-cp36m-linux_x86_64.whl
  • 验证安装

    # python程序:
    
    #!/usr/bin/python3
    # File: t1.py
    import tensorflow as tf
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))
    
    # 运行
    python3 t1.py
    
    # 输出
    Hello, TensorFlow!

基本用法

  • 使用图(graph)来表示计算任务;

  • 在被称之为回话(Session)的上下文(context)中执行图;

  • 使用tensor表示数据;

  • 使用变量(Variable)维护状态;

  • 使用feed和fetch可以为任意的操作(arbitrary operation)赋值或者从其中获取数据。

Tensors

  • The central unit of data in TensorFlow is the tensor.

  • A tensor consists of a set of primitive values shaped into an array of any number of dimensions.

  • A tensor's rank is its number of dimensions.

  • Examples:

Tutorial

导入Tensorflow

import tensorflow as tf

Tensorflow程序包括两部分:

  • Building the computational graph

  • Running the computational graph

A computational graph is a series of TensorFlow operations arranged into a graph of nodes.

Example:

最后更新于

这有帮助吗?