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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?