什么是tensor

1.什么是tensor?
class Tensor(_TensorLike)
 |  Represents one of the outputs of an `Operation`.
 |  
 |  A `Tensor` is a symbolic handle to one of the outputs of an
 |  `Operation`. It does not hold the values of that operation's output,
 |  but instead provides a means of computing those values in a
 |  TensorFlow @{tf.Session}.
 |  
 |  This class has two primary purposes:
 |  
 |  1. A `Tensor` can be passed as an input to another `Operation`.
 |     This builds a dataflow connection between operations, which
 |     enables TensorFlow to execute an entire `Graph` that represents a
 |     large, multi-step computation.
 |  
 |  2. After the graph has been launched in a session, the value of the
 |     `Tensor` can be computed by passing it to
 |     @{tf.Session.run}.
 |     `t.eval()` is a shortcut for calling
 |     `tf.get_default_session().run(t)`.
 |  
 |  In the following example, `c`, `d`, and `e` are symbolic `Tensor`
 |  objects, whereas `result` is a numpy array that stores a concrete
 |  value:
 |  
 |  ```python
 |  # Build a dataflow graph.
 |  c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
 |  d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
 |  e = tf.matmul(c, d)
 |  
 |  # Construct a `Session` to execute the graph.
 |  sess = tf.Session()
 |  
 |  # Execute the graph and store the value that `e` represents in `result`.
 |  result = sess.run(e)
 |  ```


2. tensor的属性
 |  Data descriptors defined here:
 |  
 |  device
 |      The name of the device on which this tensor will be produced, or None.
 |  
 |  dtype
 |      The `DType` of elements in this tensor.
 |  
 |  graph
 |      The `Graph` that contains this tensor.
 |  
 |  name
 |      The string name of this tensor.
 |  
 |  op
 |      The `Operation` that produces this tensor as an output.
 |  
 |  shape
 |      Returns the `TensorShape` that represents the shape of this tensor.




 |  consumers(self)
 |      Returns a list of `Operation`s that consume this tensor.
 |      
 |      Returns:
 |        A list of `Operation`s.
 |  
 |  eval(self, feed_dict=None, session=None)
 |      Evaluates this tensor in a `Session`.
 |      
 |      Calling this method will execute all preceding operations that
 |      produce the inputs needed for the operation that produces this
 |      tensor.
 |      
 |      *N.B.* Before invoking `Tensor.eval()`, its graph must have been
 |      launched in a session, and either a default session must be
 |      available, or `session` must be specified explicitly.
 |      
 |      Args:
 |        feed_dict: A dictionary that maps `Tensor` objects to feed values.
 |          See @{tf.Session.run} for a
 |          description of the valid feed values.
 |        session: (Optional.) The `Session` to be used to evaluate this tensor. If
 |          none, the default session will be used.
 |      
 |      Returns:
 |        A numpy array corresponding to the value of this tensor.
 |  
 |  get_shape(self)
 |      Alias of Tensor.shape.


 |  set_shape(self, shape)
 |      Updates the shape of this tensor.
 |      
 |      This method can be called multiple times, and will merge the given
 |      `shape` with the current shape of this tensor. It can be used to
 |      provide additional information about the shape of this tensor that
 |      cannot be inferred from the graph alone. For example, this can be used
 |      to provide additional information about the shapes of images:
 |      
 |      ```python
 |      _, image_data = tf.TFRecordReader(...).read(...)
 |      image = tf.image.decode_png(image_data, channels=3)
 |      
 |      # The height and width dimensions of `image` are data dependent, and
 |      # cannot be computed without executing the op.
 |      print(image.shape)
 |      ==> TensorShape([Dimension(None), Dimension(None), Dimension(3)])
 |      
 |      # We know that each image in this dataset is 28 x 28 pixels.
 |      image.set_shape([28, 28, 3])
 |      print(image.shape)
 |      ==> TensorShape([Dimension(28), Dimension(28), Dimension(3)])
 |      ```
 |      
 |      Args:
 |        shape: A `TensorShape` representing the shape of this tensor.
 |      
 |      Raises:
 |        ValueError: If `shape` is not compatible with the current shape of
 |          this tensor.




3. 矩阵的描述:2D, 3D
 |      For example:
 |      
 |      ```python
 |      # 2-D tensor `a`
 |      a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) => [[1. 2. 3.]
 |                                                            [4. 5. 6.]]
 |      # 2-D tensor `b`
 |      b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]
 |                                                               [9. 10.]
 |                                                               [11. 12.]]
 |      c = tf.matmul(a, b) => [[58 64]
 |                              [139 154]]
 |      
 |      
 |      # 3-D tensor `a`
 |      a = tf.constant(np.arange(1, 13, dtype=np.int32),
 |                      shape=[2, 2, 3])                  => [[[ 1.  2.  3.]
 |                                                             [ 4.  5.  6.]],
 |                                                            [[ 7.  8.  9.]
 |                                                             [10. 11. 12.]]]
 |      
 |      # 3-D tensor `b`
 |      b = tf.constant(np.arange(13, 25, dtype=np.int32),
 |                      shape=[2, 3, 2])                   => [[[13. 14.]
 |                                                              [15. 16.]
 |                                                              [17. 18.]],
 |                                                             [[19. 20.]
 |                                                              [21. 22.]
 |                                                              [23. 24.]]]
 |      c = tf.matmul(a, b) => [[[ 94 100]
 |                               [229 244]],
 |                              [[508 532]
 |                               [697 730]]]


4. 初始化矩阵
>>> b = tf.ones([3,2])
>>> type(b)
<class 'tensorflow.python.framework.ops.Tensor'>
>>> help(tf.ones)


Help on function ones in module tensorflow.python.ops.array_ops:


ones(shape, dtype=tf.float32, name=None)
    Creates a tensor with all elements set to 1.
    
    This operation returns a tensor of type `dtype` with shape `shape` and all
    elements set to 1.
    
    For example:
    
    ```python
    tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]
    ```
    
    Args:
      shape: Either a list of integers, or a 1-D `Tensor` of type `int32`.
      dtype: The type of an element in the resulting `Tensor`.
      name: A name for the operation (optional).
    
    Returns:
      A `Tensor` with all elements set to 1.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值