(Github搬砖)动手学深度学习(TF2.0版)-2.4 查阅文档

查阅文档

受篇幅所限,本书无法对所有用到的tensorflow2.0函数和类一一详细介绍。读者可以查阅相关文档来做更深入的了解。

import tensorflow as tf
print(tf.__version__)
2.0.0

2.4.1 search for functions and classes

当我们想知道一个模块里面提供了哪些可以调用的函数和类的时候,可以使用dir函数。下面我们打印dtypesrandom模块中所有的成员或属性。

dir(tf.dtypes)
['DType',
 'QUANTIZED_DTYPES',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '_sys',
 'as_dtype',
 'bfloat16',
 'bool',
 'cast',
 'complex',
 'complex128',
 'complex64',
 'double',
 'float16',
 'float32',
 'float64',
 'half',
 'int16',
 'int32',
 'int64',
 'int8',
 'qint16',
 'qint32',
 'qint8',
 'quint16',
 'quint8',
 'resource',
 'saturate_cast',
 'string',
 'uint16',
 'uint32',
 'uint64',
 'uint8',
 'variant']
dir(tf.random)
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '_sys',
 'all_candidate_sampler',
 'categorical',
 'experimental',
 'fixed_unigram_candidate_sampler',
 'gamma',
 'learned_unigram_candidate_sampler',
 'log_uniform_candidate_sampler',
 'normal',
 'poisson',
 'set_seed',
 'shuffle',
 'stateless_categorical',
 'stateless_normal',
 'stateless_truncated_normal',
 'stateless_uniform',
 'truncated_normal',
 'uniform',
 'uniform_candidate_sampler']

通常我们可以忽略掉由__开头和结尾的函数(Python的特别对象)或者由_开头的函数(一般为内部函数)。通过其余成员的名字我们大致猜测出这个模块提供了各种随机数的生成方法,包括从均匀分布采样(uniform)、从正态分布采样(normal)、从泊松分布采样(poisson)等。

2.4.2 use of functions

想了解某个函数或者类的具体用法时,可以使用help函数。让我们以ones函数为例,查阅它的用法。更详细的信息,可以通过Tensorflow的API文档版本选择页,选择与自己环境中的 tensorflow 版本一致的 API 版本进行查询。

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: A list of integers, a tuple 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.

从文档信息我们了解到,ones函数会创建和输入tensor形状相同且元素为1的新tensor。我们可以验证一下:

tf.ones([2,3], tf.int32)
<tf.Tensor: id=2, shape=(2, 3), dtype=int32, numpy=
array([[1, 1, 1],
       [1, 1, 1]])>

抵制白嫖从我做起,所以,可以给个赏金吗嘻嘻嘻!!!money

猜你喜欢

转载自www.cnblogs.com/upcwsh/p/12363892.html