Tensorflow_Basic Usage

来源:互联网 发布:围棋软件免费下载 编辑:程序博客网 时间:2024/06/11 16:06

Basic Usage

https://www.tensorflow.org/versions/r0.12/get_started/basic_usage.html

To use TensorFlow you need to understand how TensorFlow:

  • Represents computations as graphs.//将运算模型视为图
  • Executes graphs in the context of Sessions. 在回话中运行模型,先初始化
  • Represents data as tensors.  将数据作为tensor
  • Maintains state with Variables.  用Variables描述运算状态
  • Uses feeds and fetches to get data into and out of arbitrary operations. 应用feed 和fetch在任意操作中获取或输入数据

    Overview

    TensorFlow is a programming system in which you represent computations as graphs. Nodes in the graph are called ops (short for operations).模型图中的结点node视为一个运算操作

    A TensorFlow graph is a description of computations. To compute anything, a graph must be launched in a Session. A Session places the graph ops onto Devices, such as CPUs or GPUs, and provides methods to execute them. 任何图的运算都要在一个回话中执行,回话会将模型图放在一个设备上运行,比如,CPU或GPU

  • Building the graph

    To build a graph start with ops that do not need any input (source ops), such as Constant, and pass their output to other ops that do computation.

    The ops constructors in the Python library return objects that stand for the output of the constructed ops. You can pass these to other ops constructors to use as inputs.

    The TensorFlow Python library has a default graph to which ops constructors add nodes. The default graph is sufficient for many applications. See the Graph class documentation for how to explicitly manage multiple graphs.

    import tensorflow as tf# Create a Constant op that produces a 1x2 matrix.  The op is# added as a node to the default graph.## The value returned by the constructor represents the output# of the Constant op.matrix1 = tf.constant([[3., 3.]])# Create another Constant that produces a 2x1 matrix.matrix2 = tf.constant([[2.],[2.]])# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.# The returned value, 'product', represents the result of the matrix# multiplication.product = tf.matmul(matrix1, matrix2)

    The default graph now has three nodes: two constant() ops and one matmul() op. To actually multiply the matrices, and get the result of the multiplication, you must launch the graph in a session.默认模型图又3个结点:两个常量矩阵,一个乘积矩阵。

    Launching the graph in a session

    Launching follows construction. To launch a graph, create a Session object. Without arguments the session constructor launches the default graph. 创建一个session回话,自动加载默认图

    See the Session class for the complete session API.

    # Launch the default graph.sess = tf.Session()# To run the matmul op we call the session 'run()' method, passing 'product'# which represents the output of the matmul op.  This indicates to the call# that we want to get the output of the matmul op back.## All inputs needed by the op are run automatically by the session.  They# typically are run in parallel.## The call 'run(product)' thus causes the execution of three ops in the# graph: the two constants and matmul.## The output of the matmul is returned in 'result' as a numpy `ndarray` object.result = sess.run(product)print(result)# ==> [[ 12.]]# Close the Session when we're done.sess.close()

    Sessions should be closed to release resources. You can also enter a Session with a "with" block. The Sessioncloses automatically at the end of the with block.

    with tf.Session() as sess:  result = sess.run([product])  print(result)

    The TensorFlow implementation translates the graph definition into executable operations distributed across available compute resources, such as the CPU or one of your computer's GPU cards. In general you do not have to specify CPUs or GPUs explicitly. TensorFlow uses your first GPU, if you have one, for as many operations as possible.

    If you have more than one GPU available on your machine, to use a GPU beyond the first you must assign ops to it explicitly. Use with...Device statements to specify which CPU or GPU to use for operations:

    with tf.Session() as sess:  with tf.device("/gpu:1"):    matrix1 = tf.constant([[3., 3.]])    matrix2 = tf.constant([[2.],[2.]])    product = tf.matmul(matrix1, matrix2)    ...

    Devices are specified with strings. The currently supported devices are:指定运算的CPU或GPU

    • "/cpu:0": The CPU of your machine.
    • "/gpu:0": The GPU of your machine, if you have one.
    • "/gpu:1": The second GPU of your machine, etc.

    See Using GPUs for more information about GPUs and TensorFlow.



原创粉丝点击