Preliminary
Tensor用法
- tensor初始化及属性
rand_tensor = torch.rand(2,3)
ones_tensor = torch.ones(2,3)
zeros_tensor = torch.zeros(2,3)
print(tensor.shape, tensor.dtype, tensor.device)
p.s.tensor默认是创建在CPU上的,需要显式地移动到GPU。
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
- tensor操作
1.索引和切片
tensor = torch.ones(4, 4)
print('First row: ',tensor[0])
print('First column: ', tensor[:, 0])
print('Last column:', tensor[..., -1])
2.拼接
t1 = torch.cat([tensor, tensor, tensor], dim=1)
# 还有torch.stack
3.算术运算
# 矩阵乘法. y1, y2, y3 will have the same value
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(tensor)
torch.matmul(tensor, tensor.T, out=y3)
# element-wise乘法. z1, z2, z3 will have the same value
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)
# in-place operations: 带下划线
tensor.add_(5)
print(tensor)
# 常见操作:sum()和item()!
agg = tensor.sum() # 所有位置上的值求和后aggregate为单值tensor
agg_item = agg.item() # 获取单值tensor的数值
print(agg_item, type(agg_item))
Datasets & DataLoaders
torch.utils.data.DataLoader
和torch.utils.data.Dataset
两个模块负责数据相关操作, Dataset
存储数据和标签,DataLoader
把Dataset
变成可迭代对象。
官方数据集使用方法:
1.使用Dataset
类得到data
2.使用DataLoader
类创建可迭代对象dataloader
3.使用dataloader遍历数据集:每一轮迭代返回一批也就是batch_size
个样本,如果shuffle=True
则每当所有数据被迭代一遍,就会shuffle一次
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
# Loading a Dataset
training_data = datasets.FashionMNIST(
root="data", # the path where the train/test data is stored
train=True, # specifies training or test dataset
download=True, # downloads the data from the internet if it’s not available at root
transform=ToTensor() # specify the feature and label transformations
)
# Preparing your data for training with DataLoaders
train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
# Iterate through the DataLoader
# iter()函数获取可迭代对象的迭代器,本质是调用__iter__方法
# next()函数获取该对象的下一条数据
train_features, train_labels = next(iter(train_dataloader))
print(f"Feature batch shape: {
train_features.size()}")
print(f"Labels batch shape: {
train_labels.size()}")
img = train_features[0].squeeze()
label = train_labels[0]
自定义数据集使用方法:
定义自己的数据集类(继承Dataset类)并实现三个接口:
(1)__init__
, 实例化类的时候运行一次
(2)__len__
, 返回数据集大小
(3)__getitem__
, 返回一个指定index处的样本
# 前提:图片存在img_dir文件夹下,标签在annotations_file表格中
import os
import pandas as pd
from torchvision.io import read_image
class CustomImageDataset(Dataset):
def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
self.img_labels = pd.read_csv(annotations_file)
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform
def __len__(self):
return len(self.img_labels