pytorch_basics

BUG

  • BUG1

    Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 ‘weight’

是因为我的x, y两个Tensor是.cuda()的矩阵,而Linear不是.cuda()的。所以得给linear也加上.cuda()

  • BUG2

    TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

看错误提示就知道了。

Tutorial

    1. Basic autograd example 1 (Line 25 to 39)
      torch.tensor(.cuda())
      y.backward()
      x.grad
    1. Basic autograd example 2 (Line 46 to 83)
      torch.randn
      torch.nn.Linear(3,2)
      (No used)linear.weight.data.sub_(0.01 * linear.weight.grad.data)

      Build loss function and optimizer.

      nn.MSELoss()
      torch.optim.SGD(linear.parameters(), lr = 0.01)

      详细不写了,写出了步骤

      Forward pass

      Compute loss

      Backward pass.

      loss.backward()

      Print out the gradients.

      1-step gradient descent.

      optimizer.step()

    1. Loading data from numpy (Line 90 to 97)
      torch.from_numpy().cuda()
      y.cpu().numpy()
    1. Input pipline (Line 104 to 129)
      torchvision.datasets.CIFAR10(...)
      # Data loader
      torch.utils.data.DataLoader(..)
      # Actual usage of the data loader is as below.
      for images, labels in train_loader:
      # Training code should be written here.
      pass
      
    1. Input pipline for custom dataset (Line 136 to 156)
      build your custom dataset.
      _init__存放文件名称列表
      _getitem_读取单个用例,预处理,返回一个data和一个label
      _len_# you should change 0 to the total size of your dataset
    1. Pretrained model (Line 163 to 176)
      resnet = torchvision.models.resnet18(pretrained=True)
      for param in resnet.parameters():
      param.requires_grad = False
      #Replace the top layer for funetuning
      resnet.fc = nn.Linear(resnet.fc.in_features, 100)
    1. Save and load model (Line 183 to 189)
      torch.save(model,'_.ckpt') / torch.save(model.state_dict(),'_.ckpt')
      torch.load() / model.load_state_dict()