import torch
print(dir(torch.cuda))
['BFloat16Storage', 'BFloat16Tensor', 'BoolStorage', 'BoolTensor', 'ByteStorage', 'ByteTensor', 'CharStorage', 'CharTensor', 'CudaError', 'DeferredCudaCallError', 'DoubleStorage', 'DoubleTensor', 'Event', 'FloatStorage', 'FloatTensor', 'HalfStorage', 'HalfTensor', 'IntStorage', 'IntTensor', 'LongStorage', 'LongTensor', 'PIPE', 'Popen', 'ShortStorage', 'ShortTensor', 'Stream', '_CudaBase', '_StorageBase', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_after_fork', '_check_capability', '_check_driver', '_cudart', '_dummy_type', '_free_mutex', '_get_device_index', '_host_allocator', '_in_bad_fork', '_initialization_lock', '_initialized', '_lazy_call', '_lazy_init', '_lazy_new', '_load_cudart', '_original_pid', '_queued_calls', '_register_after_fork', '_sleep', '_tls', '_utils', 'check_error', 'comm', 'contextlib', 'ctypes', 'cudaStatus', 'cudart', 'current_blas_handle', 'current_device', 'current_stream', 'default_stream', 'device', 'device_count', 'device_of', 'empty_cache', 'find_cuda_windows_lib', 'get_device_capability', 'get_device_name', 'get_device_properties', 'get_rng_state', 'get_rng_state_all', 'init', 'initial_seed', 'ipc_collect', 'is_available', 'manual_seed', 'manual_seed_all', 'max_memory_allocated', 'max_memory_cached', 'memory_allocated', 'memory_cached', 'nccl', 'nvtx', 'os', 'platform', 'profiler', 'raise_from', 'random', 'reset_max_memory_allocated', 'reset_max_memory_cached', 'seed', 'seed_all', 'set_device', 'set_rng_state', 'set_rng_state_all', 'sparse', 'stream', 'streams', 'synchronize', 'sys', 'threading', 'torch', 'traceback', 'warnings']
通常我们可以忽略掉由__
开头和结尾的函数(Python的特别对象)或者由_
开头的函数(一般为内部函数)。通过其余成员的名字我们大致猜测出这个模块提供了各种对GPU的控制方法,包括可用设备数量(device_count
)、GPU是否可用(is_available
)、清空显存(empty_cache
)等。
想了解某个函数或者类的具体用法时,可以使用help
函数。让我们以torch
中的ones_like
函数为例,查阅它的用法。
help(torch.ones_like)
Help on built-in function ones_like: ones_like(...) ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) -> Tensor Returns a tensor filled with the scalar value `1`, with the same size as :attr:`input`. ``torch.ones_like(input)`` is equivalent to ``torch.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``. .. warning:: As of 0.4, this function does not support an :attr:`out` keyword. As an alternative, the old ``torch.ones_like(input, out=output)`` is equivalent to ``torch.ones(input.size(), out=output)``. Args: input (Tensor): the size of :attr:`input` will determine size of the output tensor. dtype (:class:`torch.dtype`, optional): the desired data type of returned Tensor. Default: if ``None``, defaults to the dtype of :attr:`input`. layout (:class:`torch.layout`, optional): the desired layout of returned tensor. Default: if ``None``, defaults to the layout of :attr:`input`. device (:class:`torch.device`, optional): the desired device of returned tensor. Default: if ``None``, defaults to the device of :attr:`input`. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. Example:: >>> input = torch.empty(2, 3) >>> torch.ones_like(input) tensor([[ 1., 1., 1.], [ 1., 1., 1.]])
从文档信息我们了解到,ones_like
函数会创建和输入Tensor
形状相同且元素为1的新Tensor
。我们可以验证一下:
x = torch.Tensor([[0, 0, 0], [2, 2, 2]])
y = torch.ones_like(x)
y
tensor([[1., 1., 1.], [1., 1., 1.]])
在Jupyter记事本里,我们可以使用?
来将文档显示在另外一个窗口中。例如,使用torch.rand?
将得到与help(torch.rand)
几乎一样的内容,但会显示在额外窗口里。此外,如果使用torch.rand??
,那么会额外显示该函数实现的代码。
读者也可以在PyTorch的网站上查阅相关文档。访问PyTorch网站 https://pytorch.org/ (如图2.1所示),点击网页顶部的下拉菜单“Docs”可查阅各个前端语言的接口。此外,也可以在点击网页右上方的搜索图标直接搜索函数或类名称。
图2.2展示了PyTorch网站上有关ones_like
函数的文档。
dir
和help
函数,或访问PyTorch官方网站。PyTorch
支持的其他操作。