Pytorch 使用知识点
PyTorch tensors are mutable, which means that they can be modified in place. tensor 是 mutable object,和 list 一样,因此在函数传参后,在函数里面时,需要注意是修改原来的 tensor,还是返回新的。
Varible and tensor: Varible 是旧版的 torch 的变量, 等价于 tensor
b = a if torch.is_tensor(a) else torch.tensor(a)
Pytorch 与 numpy 的 Tensor 与 arrary 类型转换
1
2torch.from_numpy(a_array)
a_tenosr.numpy()numpy 与 torch 的浮点数默认位数不同
numpy 浮点数默认使用 64 位, torch 的浮点数默认使用 32 位
Default dtype:
PyTorch torch.float32
torch.set_default_dtype(torch.float64)
to compatible with tensor.numpy() defauly np.float64numpy
np.float64
int_
: Default integer type (same as C long; normally either int64 or int32)
shuffle tensor
1
2indexes = torch.randperm(data.shape[0])
data = data[indexes]random tensor
torch.tensor 默认初始化类型 torch.float/torch.FloatTensor: float 类型, 后者是 CPU float 类型
torch.float(*(a, b, c)).uniform_(0, 100)
is 和 ==
identity operator (is) and the equality operator (==)
is 是用来检查是否是一个地址的数据
is not
不能当成!=
用Python '!=' Is Not 'is not': Comparing Objects in Python – Real Python
Reproducibility fix randomness
1
2
3
4
5
6
7import torch
import random
import numpy as np
random.seed(0)
torch.manual_seed(0)
np.random.seed(0)pytorch 和 numpy 中最大和最小值
pytorch 中 torch.iinfo
, torch.finfo
,
np.iinfo
, np.finfo
:
1 | In [93]: np.iinfo(np.int64) |