tensorflow 常见函数过一遍之常数与随机数

常数与随机数

import tensorflow as tf 
import numpy as np

常数

help(tf.constant)
Help on function constant in module tensorflow.python.framework.constant_op:

constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
    Creates a constant tensor.

    The resulting tensor is populated with values of type `dtype`, as
    specified by arguments `value` and (optionally) `shape` (see examples
    below).

    The argument `value` can be a constant value, or a list of values of type
    `dtype`. If `value` is a list, then the length of the list must be less
    than or equal to the number of elements implied by the `shape` argument (if
    specified). In the case where the list length is less than the number of
    elements specified by `shape`, the last element in the list will be used
    to fill the remaining entries.

    The argument `shape` is optional. If present, it specifies the dimensions of
    the resulting tensor. If not present, the shape of `value` is used.

    If the argument `dtype` is not specified, then the type is inferred from
    the type of `value`.

    For example:

    ```python
    # Constant 1-D Tensor populated with value list.
    tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

    # Constant 2-D tensor populated with scalar value -1.
    tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                                 [-1. -1. -1.]]
    ```

    Args:
      value:          A constant value (or list) of output type `dtype`.

      dtype:          The type of the elements of the resulting tensor.

      shape:          Optional dimensions of resulting tensor.

      name:           Optional name for the tensor.

      verify_shape:   Boolean that enables verification of a shape of values.

    Returns:
      A Constant Tensor.

    Raises:
      TypeError: if shape is incorrectly specified or unsupported.
tensor = tf.constant([1,2,3,4])
with tf.Session() as sess:
    print sess.run(tensor)
[1 2 3 4]
tensor = tf.constant(-1,shape=[2,2])
with tf.Session() as sess:
    print sess.run(tensor)
[[-1 -1]
 [-1 -1]]
tensor = tf.zeros([2,3],tf.int32)
with tf.Session() as sess:
    print sess.run(tensor)
[[0 0 0]
 [0 0 0]]
tensor = tf.ones([2,3],tf.int32)
with tf.Session() as sess:
    print sess.run(tensor)
[[1 1 1]
 [1 1 1]]
tensor = tf.fill([2,3],7)
with tf.Session() as sess:
    print sess.run(tensor)
[[7 7 7]
 [7 7 7]]

随机数

tensor = tf.random_normal([2,3],0,1)
with tf.Session() as sess:
    print sess.run(tensor)
[[-0.6859797  -1.1438121   1.5810659 ]
 [ 0.977541    0.31416038  1.5148275 ]]
tensor = tf.random_uniform([2,3],-1,1)
with tf.Session() as sess:
    print sess.run(tensor)
[[-0.00949144 -0.26346707 -0.9575894 ]
 [-0.83355093  0.02976918  0.17575979]]
tensor = tf.truncated_normal([2,3],0,1)
with tf.Session() as sess:
    print sess.run(tensor)
[[-1.7953236   1.321937   -0.6290948 ]
 [-0.3074989   0.80894065  0.2000714 ]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值