【Pytorch】学习记录

一、basic

pytorch训练基本结构.png

基本点

1.autograd - 1

  1. 变量:torch.tensor(1, requires_grad=True)
  2. 示例表达式:y = w * x + b
  3. y.backward()
  4. y.backward()

2.autograd - 2
print ('dL/dw: ', linear.weight.grad)

3.Loading data from numpy
y = torch.from_numpy(x)

4.Input pipline
dataset => dataloader => iter

5.Input pipline for custom dataset

class CustomDataset(torch.utils.data.Dataset):
    def __init__(self):
        # TODO
        # 1. Initialize file paths or a list of file names. 
        pass
    def __getitem__(self, index):
        # TODO
        # 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
        # 2. Preprocess the data (e.g. torchvision.Transform).
        # 3. Return a data pair (e.g. image and label).
        pass
    def __len__(self):
        # You should change 0 to the total size of your dataset.
        return 0 

# You can then use the prebuilt data loader. 
custom_dataset = CustomDataset()
train_loader = torch.utils.data.DataLoader(dataset=custom_dataset,
                                           batch_size=64, 
                                           shuffle=True)

6.Pretrained model

# Download and load the pretrained ResNet-18.
resnet = torchvision.models.resnet18(pretrained=True)

# If you want to finetune only the top layer of the model, set as below.
for param in resnet.parameters():
    param.requires_grad = False

# Replace the top layer for finetuning.
resnet.fc = nn.Linear(resnet.fc.in_features, 100)  # 100 is an example.

# Forward pass.
images = torch.randn(64, 3, 224, 224)
outputs = resnet(images)
print (outputs.size())     # (64, 100)

7.Save and load the model
常规保存载入

torch.save(resnet, 'model.ckpt')
model = torch.load('model.ckpt')

仅保存加载模型参数

torch.save(resnet.state_dict(), 'params.ckpt')
resnet.load_state_dict(torch.load('params.ckpt'))

二、intermediate

三、advanced

四、04-utils

你可能感兴趣的:(【Pytorch】学习记录)