torch学习

张量(tensor)

生成未初始化的向量

import torch
a = torch.empty(3, 5, dtype=torch.float)

numpy和tensor转换

# tensor to numpy
a = torch.ones([5, 3], dtype=torch.float)
b = a.numpy()
# numpy to tensor
a = torch.from_numpy(a)

梯度(autograd)

不想要计算梯度的部分

with torch.no_grad():
    print(x.requires_grad)

数据集(dataset)

from torch.utils.data import Dataset
class MyDataset(Dataset):  
    def __init__(self):
        # something

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        sample = {'image': image, 'label': label}
        return sample

常用import

import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

tensorflow和pytorch异同

  • tf.concat 和 torch.cat
  • tf.reshape 和 view

杂项

  • detach会复制一个tensor的权值,但是不会把数据通路复制
  • F.log_softmax -> softmax + log
  • embedding层:nn.Embeding(vocab_size, vector_dim)

你可能感兴趣的:(torch学习)