PyTorch_note
https://docs.pytorch.org/docs/stable/torch.html
一些 API
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Datasets & DataLoaders
Dataset:将磁盘数据转为 “一行行的” x-y pairs (单个样本),自定义的需要至少自定义 __init__, __len__, and __getitem__,可能需要对特征、标签进行预处理。
DataLoader: 将Dataset转为可以用于训练的 mini-batch,并具有shuffle、sampler、collate_fn的功能或自定义函数。
| |
class torch.utils.data.DataLoader
dataset (Dataset) – dataset from which to load the data.
batch_size (int, optional) – how many samples per batch to load (default: 1).
shuffle (bool, optional) – set to True to have the data reshuffled at every epoch (default: False).
sampler (Sampler or Iterable, optional) – 采样者,与shuffle互斥.
batch_sampler (Sampler or Iterable, optional) – 批次采样者,如果传入了,则不可传入batch_size, shuffle, sampler, and drop_last.
num_workers (int, optional) – 线程数. (default: 0)
collate_fn (Callable, optional) – sample后的操作,也就是对batch的后处理.
drop_last (bool, optional) – 是否丢弃最后那个不完整的batch(除去刚好完整的情况). (default: False)
generator (torch.Generator, optional) – If not None, this RNG will be used by RandomSampler to generate random indexes and multiprocessing to generate base_seed for workers. (default: None)
Transforms
Transforms — PyTorch Tutorials 2.9.0+cu128 documentation
可以在前面的CustomImageDataset中看到 transform 和 target_transform 的 应用
transform:对特征
target_transform:对标签
nn.Module
Module — PyTorch 2.9 documentation
一个__init__, 一个forward。
| |
net.apply(),net是nn.Module实例,作用为应用 参数处理函数
| |