2.7. 查阅文档¶
由于篇幅限制,本书不可能介绍每一个MindSpore函数和类。 API文档、其他教程和示例提供了本书之外的大量文档。 本节提供了一些查看MindSpore API的指导。
由于篇幅限制,本书不可能介绍每一个PyTorch函数和类。 API文档、其他教程和示例提供了本书之外的大量文档。 本节提供了一些查看PyTorch API的指导。
2.7.1. 查找模块中的所有函数和类¶
为了知道模块中可以调用哪些函数和类,可以调用dir函数。
例如,我们可以查询随机数生成模块中的所有属性:
import mindspore.ops as ops
public_ops = [name for name in dir(ops) if not name.startswith('_')]
print(public_ops[:40])
['ACos', 'Abs', 'AccumulateNV2', 'Acosh', 'Adam', 'AdamNoUpdateParam', 'AdamWeightDecay', 'AdaptiveAvgPool2D', 'AdaptiveAvgPool3D', 'AdaptiveMaxPool2D', 'AdaptiveMaxPool3D', 'Add', 'AddN', 'Addcdiv', 'Addcmul', 'AdjustHue', 'AdjustSaturation', 'AffineGrid', 'AiCPURegOp', 'AkgAscendRegOp', 'AkgGpuRegOp', 'AllGather', 'AllReduce', 'AlltoAll', 'AlltoAllV', 'Angle', 'ApplyAdaMax', 'ApplyAdadelta', 'ApplyAdagrad', 'ApplyAdagradDA', 'ApplyAdagradV2', 'ApplyAdamWithAmsgrad', 'ApplyAdamWithAmsgradV2', 'ApplyAddSign', 'ApplyCenteredRMSProp', 'ApplyFtrl', 'ApplyGradientDescent', 'ApplyKerasMomentum', 'ApplyMomentum', 'ApplyPowerSign']
import torch
print(dir(torch.distributions))
['AbsTransform', 'AffineTransform', 'Bernoulli', 'Beta', 'Binomial', 'CatTransform', 'Categorical', 'Cauchy', 'Chi2', 'ComposeTransform', 'ContinuousBernoulli', 'CorrCholeskyTransform', 'CumulativeDistributionTransform', 'Dirichlet', 'Distribution', 'ExpTransform', 'Exponential', 'ExponentialFamily', 'FisherSnedecor', 'Gamma', 'Geometric', 'Gumbel', 'HalfCauchy', 'HalfNormal', 'Independent', 'IndependentTransform', 'InverseGamma', 'Kumaraswamy', 'LKJCholesky', 'Laplace', 'LogNormal', 'LogisticNormal', 'LowRankMultivariateNormal', 'LowerCholeskyTransform', 'MixtureSameFamily', 'Multinomial', 'MultivariateNormal', 'NegativeBinomial', 'Normal', 'OneHotCategorical', 'OneHotCategoricalStraightThrough', 'Pareto', 'Poisson', 'PositiveDefiniteTransform', 'PowerTransform', 'RelaxedBernoulli', 'RelaxedOneHotCategorical', 'ReshapeTransform', 'SigmoidTransform', 'SoftmaxTransform', 'SoftplusTransform', 'StackTransform', 'StickBreakingTransform', 'StudentT', 'TanhTransform', 'Transform', 'TransformedDistribution', 'Uniform', 'VonMises', 'Weibull', 'Wishart', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'bernoulli', 'beta', 'biject_to', 'binomial', 'categorical', 'cauchy', 'chi2', 'constraint_registry', 'constraints', 'continuous_bernoulli', 'dirichlet', 'distribution', 'exp_family', 'exponential', 'fishersnedecor', 'gamma', 'geometric', 'gumbel', 'half_cauchy', 'half_normal', 'identity_transform', 'independent', 'inverse_gamma', 'kl', 'kl_divergence', 'kumaraswamy', 'laplace', 'lkj_cholesky', 'log_normal', 'logistic_normal', 'lowrank_multivariate_normal', 'mixture_same_family', 'multinomial', 'multivariate_normal', 'negative_binomial', 'normal', 'one_hot_categorical', 'pareto', 'poisson', 'register_kl', 'relaxed_bernoulli', 'relaxed_categorical', 'studentT', 'transform_to', 'transformed_distribution', 'transforms', 'uniform', 'utils', 'von_mises', 'weibull', 'wishart']
通常可以忽略以“__”(双下划线)开始和结束的函数,它们是Python中的特殊对象,
或以单个“_”(单下划线)开始的函数,它们通常是内部函数。
根据剩余的函数名或属性名,我们可能会猜测这个模块提供了各种生成随机数的方法,
包括从均匀分布(uniform)、正态分布(normal)和多项分布(multinomial)中采样。
2.7.2. 查找特定函数和类的用法¶
有关如何使用给定函数或类的更具体说明,可以调用help函数。
例如,我们来查看张量ones函数的用法。
help(ops.ones)
Help on function ones in module mindspore.ops.auto_generate.gen_ops_def:
ones(shape, dtype=None)
Creates a tensor filled with value ones, whose shape and type are described by the first argument size and second argument dtype respectively.
.. warning::
For argument shape, Tensor type input will be deprecated in the future version.
Args:
shape (Union[tuple[int], list[int], int, Tensor]): The specified shape of output tensor. Only positive integer or
tuple or Tensor containing positive integers are allowed. If it is a Tensor,
it must be a 0-D or 1-D Tensor with int32 or int64 dtypes.
dtype (mindspore.dtype): The specified type of output tensor. If dtype is None ,
mindspore.float32 will be used. Default: None .
Returns:
Tensor, whose dtype and size are defined by input.
Raises:
TypeError: If shape is neither an int nor an tuple/list/Tensor of int.
Supported Platforms:
Ascend GPU CPU
Examples:
>>> import mindspore
>>> from mindspore import ops
>>> output = ops.ones((2, 2), mindspore.float32)
>>> print(output)
[[1. 1.]
[1. 1.]]
print('torch.ones(size, dtype=None, device=None) -> Tensor')
print('Returns a tensor filled with scalar value 1.')
torch.ones(size, dtype=None, device=None) -> Tensor
Returns a tensor filled with scalar value 1.
从文档中,我们可以看到ones函数创建一个具有指定形状的新张量,并将所有元素值设置为1。
下面来运行一个快速测试来确认这一解释:
ops.ones(4)
Tensor(shape=[4], dtype=Float32, value= [ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00])
torch.ones(4)
tensor([1., 1., 1., 1.])
在Jupyter记事本中,我们可以使用?指令在另一个浏览器窗口中显示文档。
例如,list?指令将创建与help(list)指令几乎相同的内容,并在新的浏览器窗口中显示它。
此外,如果我们使用两个问号,如list??,将显示实现该函数的Python代码。
2.7.3. 小结¶
官方文档提供了本书之外的大量描述和示例。
可以通过调用
dir和help函数或在Jupyter记事本中使用?和??查看API的用法文档。
2.7.4. 练习¶
在深度学习框架中查找任何函数或类的文档。请尝试在这个框架的官方网站上找到文档。