pytorch小知识(动手学深度学习)

1.查阅文档

1.1为了知道模块中可以调用哪些函数和类,我们调用dir函数。

import torch

print(dir(torch.ones))

结果如下:通常,我们可以忽略以“__”(双下划线)开始和结束的函数(它们是Python中的特殊对象), 或以单个“_”(单下划线)开始的函数(它们通常是内部函数)。

['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']

1.2有关如何使用给定函数或类的更具体说明,我们可以调用help函数。 例如,我们来查看张量ones函数的用法。

help (torch.ones)

结果如下: 

Help on built-in function ones:

ones(...)
    ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
    
    Returns a tensor filled with the scalar value `1`, with the shape defined
    by the variable argument :attr:`size`.
    
    Args:
        size (int...): a sequence of integers defining the shape of the output tensor.
            Can be a variable number of arguments or a collection like a list or tuple.
    
    Keyword arguments:
        out (Tensor, optional): the output tensor.
        dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
            Default: if ``None``, uses a global default (see :func:`torch.set_default_tensor_type`).
        layout (:class:`torch.layout`, optional): the desired layout of returned Tensor.
            Default: ``torch.strided``.
        device (:class:`torch.device`, optional): the desired device of returned tensor.
            Default: if ``None``, uses the current device for the default tensor type
            (see :func:`torch.set_default_tensor_type`). :attr:`device` will be the CPU
            for CPU tensor types and the current CUDA device for CUDA tensor types.
        requires_grad (bool, optional): If autograd should record operations on the
            returned tensor. Default: ``False``.
    
    Example::
    
        >>> torch.ones(2, 3)
        tensor([[ 1.,  1.,  1.],
                [ 1.,  1.,  1.]])
    
        >>> torch.ones(5)
        tensor([ 1.,  1.,  1.,  1.,  1.])

或者在Jupyter记事本中,我们可以使用?指令在另一个浏览器窗口中显示文档。 例如,torch.ones?指令将创建与help(torch。ones)指令几乎相同的内容,并在新的浏览器窗口中显示它。 此外,如果我们使用两个问号,如torch.ones??,将显示实现该函数的Python代码。

2.下载数据集

"""通过框架中的内置函数将Fashion-MNIST数据集下载并读取到内存中。"""
# 通过ToTensor实例将图像数据从PIL类型变换成32位浮点数格式,
# 并除以255使得所有像素的数值均在0到1之间
trans = transforms.ToTensor()
mnist_train = torchvision.datasets.FashionMNIST(
    root="../data", train=True, transform=trans, download=True)
mnist_test = torchvision.datasets.FashionMNIST(
    root="../data", train=False, transform=trans, download=True)

从dataset中拿到数据并且下载到根目录中的data文件架当中。train=True说明下载的是训练数据集;transform=trans说明拿到的是pytorch的数据而不是一堆图片;download=True说明默认从网上下载(当然也可以事先下载好);train=Flase,即只下载测试数据集。

 训练集有60000张图片;测试集有10000张图片。每张图片大小为1*28*28,因为是黑白图片,所以RGB通道数为1

3.查看运行时间

timer = d2l.Timer()
f'{timer.stop():.2f} sec'
start = time.time()
"""
运行的程序
"""
end = time.time()
start = time.perf_counter()
"""
运行的程序
"""
end = time.perf_counter()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值