tensorflow基本概念

来源:互联网 发布:linux java dlog.path 编辑:程序博客网 时间:2024/05/16 09:02

1. tensor: 看上一篇什么是tensor:


2. DType:

class DType(__builtin__.object)
 |  Represents the type of the elements in a `Tensor`.
 |  
 |  The following `DType` objects are defined:
 |  
 |  * `tf.float16`: 16-bit half-precision floating-point.
 |  * `tf.float32`: 32-bit single-precision floating-point.
 |  * `tf.float64`: 64-bit double-precision floating-point.
 |  * `tf.bfloat16`: 16-bit truncated floating-point.
 |  * `tf.complex64`: 64-bit single-precision complex.
 |  * `tf.complex128`: 128-bit double-precision complex.
 |  * `tf.int8`: 8-bit signed integer.
 |  * `tf.uint8`: 8-bit unsigned integer.
 |  * `tf.uint16`: 16-bit unsigned integer.
 |  * `tf.int16`: 16-bit signed integer.
 |  * `tf.int32`: 32-bit signed integer.
 |  * `tf.int64`: 64-bit signed integer.
 |  * `tf.bool`: Boolean.
 |  * `tf.string`: String.
 |  * `tf.qint8`: Quantized 8-bit signed integer.
 |  * `tf.quint8`: Quantized 8-bit unsigned integer.
 |  * `tf.qint16`: Quantized 16-bit signed integer.
 |  * `tf.quint16`: Quantized 16-bit unsigned integer.
 |  * `tf.qint32`: Quantized 32-bit signed integer.
 |  * `tf.resource`: Handle to a mutable resource.

 |  In addition, variants of these types with the `_ref` suffix are
 |  defined for reference-typed tensors.
 |  
 |  The `tf.as_dtype()` function converts numpy types and string type
 |  names to a `DType` object.


3. placeholder

placeholder(dtype, shape=None, name=None)
    Inserts a placeholder for a tensor that will be always fed.
    
    **Important**: This tensor will produce an error if evaluated. Its value must
    be fed using the `feed_dict` optional argument to `Session.run()`,
    `Tensor.eval()`, or `Operation.run()`.
    
    For example:
    
    ```python
    x = tf.placeholder(tf.float32, shape=(1024, 1024))
    y = tf.matmul(x, x)
    
    with tf.Session() as sess:
      print(sess.run(y))  # ERROR: will fail because x was not fed.
    
      rand_array = np.random.rand(1024, 1024)
      print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.
    ```
    
    Args:
      dtype: The type of elements in the tensor to be fed.
      shape: The shape of the tensor to be fed (optional). If the shape is not
        specified, you can feed a tensor of any shape.
      name: A name for the operation (optional).
    
    Returns:
      A `Tensor` that may be used as a handle for feeding a value, but not
      evaluated directly.


3. Variable

class Variable(__builtin__.object)
 |  See the @{$variables$Variables How To} for a high
 |  level overview.
 |  
 |  A variable maintains state in the graph across calls to `run()`. You add a
 |  variable to the graph by constructing an instance of the class `Variable`.
 |  
 |  The `Variable()` constructor requires an initial value for the variable,
 |  which can be a `Tensor` of any type and shape. The initial value defines the
 |  type and shape of the variable. After construction, the type and shape of
 |  the variable are fixed. The value can be changed using one of the assign
 |  methods.
 |  
 |  If you want to change the shape of a variable later you have to use an
 |  `assign` Op with `validate_shape=False`.
 |  
 |  Just like any `Tensor`, variables created with `Variable()` can be used as
 |  inputs for other Ops in the graph. Additionally, all the operators
 |  overloaded for the `Tensor` class are carried over to variables, so you can
 |  also add nodes to the graph by just doing arithmetic on variables.
 |  
 |  ```python
 |  import tensorflow as tf
 |  
 |  # Create a variable.
 |  w = tf.Variable(<initial-value>, name=<optional-name>)
 |  
 |  # Use the variable in the graph like any Tensor.
 |  y = tf.matmul(w, ...another variable or tensor...)
 |  
 |  # The overloaded operators are available too.
 |  z = tf.sigmoid(w + y)
 |  
 |  # Assign a new value to the variable with `assign()` or a related method.
 |  w.assign(w + 1.0)
 |  w.assign_add(1.0)
 |  ```
 |  
 |  When you launch the graph, variables have to be explicitly initialized before
 |  you can run Ops that use their value. You can initialize a variable by
 |  running its *initializer op*, restoring the variable from a save file, or
 |  simply running an `assign` Op that assigns a value to the variable. In fact,
 |  the variable *initializer op* is just an `assign` Op that assigns the
 |  variable's initial value to the variable itself.


4. Session:

class Session(BaseSession)
 |  A class for running TensorFlow operations.
 |  
 |  A `Session` object encapsulates the environment in which `Operation`
 |  objects are executed, and `Tensor` objects are evaluated. For
 |  example:
 |  
 |  ```python
 |  # Build a graph.
 |  a = tf.constant(5.0)
 |  b = tf.constant(6.0)
 |  c = a * b
 |  
 |  # Launch the graph in a session.
 |  sess = tf.Session()
 |  
 |  # Evaluate the tensor `c`.
 |  print(sess.run(c))
 |  ```
 |  
 |  A session may own resources, such as
 |  @{tf.Variable}, @{tf.QueueBase},
 |  and @{tf.ReaderBase}. It is important to release
 |  these resources when they are no longer required. To do this, either
 |  invoke the @{tf.Session.close} method on the session, or use
 |  the session as a context manager. The following two examples are
 |  equivalent:
 |  
 |  ```python
 |  # Using the `close()` method.
 |  sess = tf.Session()
 |  sess.run(...)
 |  sess.close()
 |  
 |  # Using the context manager.
 |  with tf.Session() as sess:
 |    sess.run(...)

原创粉丝点击